Completed
Pull Request — master (#142)
by Beñat
02:52
created

EditOrganizationHandler::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Application\Organization;
16
17
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationName;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
22
23
class EditOrganizationHandler
24
{
25
    private $repository;
26
27
    public function __construct(OrganizationRepository $repository)
28
    {
29
        $this->repository = $repository;
30
    }
31
32
    public function __invoke(EditOrganizationCommand $command)
33
    {
34
        $organization = $this->repository->organizationOfId(
35
            OrganizationId::generate(
36
                $command->id()
37
            )
38
        );
39
        if (null === $organization) {
40
            throw new OrganizationDoesNotExistException();
41
        }
42
        $organization->edit(
43
            new OrganizationName(
44
                $command->name()
45
            ),
46
            new Slug(
47
                null === $command->slug() ? $command->name() : $command->slug()
48
            )
49
        );
50
        $this->repository->persist($organization);
51
    }
52
}
53