Completed
Pull Request — master (#158)
by Gorka
03:03
created

RemoveOwnerToOrganizationHandler::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
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\TaskManager\Domain\Model\Organization\Organization;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
21
use Kreta\TaskManager\Domain\Model\Organization\Owner;
22
use Kreta\TaskManager\Domain\Model\Organization\OwnerId;
23
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException;
24
use Kreta\TaskManager\Domain\Model\User\UserId;
25
26 View Code Duplication
class RemoveOwnerToOrganizationHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
{
28
    private $repository;
29
30
    public function __construct(OrganizationRepository $repository)
31
    {
32
        $this->repository = $repository;
33
    }
34
35
    public function __invoke(RemoveOwnerToOrganizationCommand $command)
36
    {
37
        $organization = $this->repository->organizationOfId(
38
            OrganizationId::generate(
39
                $command->organizationId()
40
            )
41
        );
42
        if (!$organization instanceof Organization) {
43
            throw new OrganizationDoesNotExistException();
44
        }
45
46
        if (!$organization->isOwner(OwnerId::generate(UserId::generate($command->removerId()), $organization->id()))) {
47
            throw new UnauthorizedOrganizationActionException();
48
        }
49
50
        $organization->removeOwner(
51
            new Owner(
52
                OwnerId::generate(
53
                    UserId::generate(
54
                        $command->userId()
55
                    ),
56
                    $organization->id()
57
                )
58
            )
59
        );
60
61
        $this->repository->persist($organization);
62
    }
63
}
64