File size: 1,330 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Voice\Application\CommandHandlers;

use Voice\Application\Commands\UpdateVoiceCommand;
use Voice\Domain\Entities\VoiceEntity;
use Voice\Domain\Exceptions\VoiceNotFoundException;
use Voice\Domain\VoiceRepositoyInterface;

class UpdateVoiceCommandHandler
{
    public function __construct(
        private VoiceRepositoyInterface $repo
    ) {
    }

    /**
     * @throws VoiceNotFoundException
     */
    public function handle(UpdateVoiceCommand $cmd): VoiceEntity
    {
        $voice = $cmd->voice instanceof VoiceEntity
            ? $cmd->voice : $this->repo->ofId($cmd->voice);

        if ($cmd->status) {
            $voice->setStatus($cmd->status);
        }

        if ($cmd->name) {
            $voice->setName($cmd->name);
        }

        if ($cmd->sampleUrl) {
            $voice->setSampleUrl($cmd->sampleUrl);
        }

        if ($cmd->tones) {
            $voice->setTones(...$cmd->tones);
        }

        if ($cmd->useCases) {
            $voice->setUseCases(...$cmd->useCases);
        }

        if ($cmd->gender !== false) {
            $voice->setGender($cmd->gender);
        }

        if ($cmd->accent) {
            $voice->setAccent($cmd->accent);
        }

        if ($cmd->age !== false) {
            $voice->setAge($cmd->age);
        }

        return $voice;
    }
}