Completed
Pull Request — master (#379)
by Kristof
05:38
created

Projector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Organizer\ReadModel\Permission;
4
5
use Broadway\Domain\DomainMessage;
6
use Broadway\EventHandling\EventListenerInterface;
7
use CultuurNet\UDB3\Cdb\ActorItemFactory;
8
use CultuurNet\UDB3\Cdb\CreatedByToUserIdResolverInterface;
9
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
10
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface;
11
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
12
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
13
use ValueObjects\StringLiteral\StringLiteral;
14
15 View Code Duplication
class Projector implements EventListenerInterface
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...
16
{
17
    use DelegateEventHandlingToSpecificMethodTrait;
18
19
    /**
20
     * @var CreatedByToUserIdResolverInterface
21
     */
22
    private $userIdResolver;
23
24
    /**
25
     * @var PermissionRepositoryInterface
26
     */
27
    private $permissionRepository;
28
29
    public function __construct(
30
        PermissionRepositoryInterface $permissionRepository,
31
        CreatedByToUserIdResolverInterface $createdByToUserIdResolver
32
    ) {
33
        $this->userIdResolver = $createdByToUserIdResolver;
34
        $this->permissionRepository = $permissionRepository;
35
    }
36
37
    protected function applyOrganizerImportedFromUDB2(
38
        OrganizerImportedFromUDB2 $organizerImportedFromUDB2
39
    ) {
40
        $cdbEvent = ActorItemFactory::createActorFromCdbXml(
41
            $organizerImportedFromUDB2->getCdbXmlNamespaceUri(),
42
            $organizerImportedFromUDB2->getCdbXml()
43
        );
44
45
        $createdByIdentifier = $cdbEvent->getCreatedBy();
46
47
        if ($createdByIdentifier) {
48
            $ownerId = $this->userIdResolver->resolveCreatedByToUserId(
49
                new StringLiteral($createdByIdentifier)
50
            );
51
52
            if (!$ownerId) {
53
                return;
54
            }
55
56
            $this->permissionRepository->markOfferEditableByUser(
57
                new StringLiteral($organizerImportedFromUDB2->getEventId()),
58
                $ownerId
0 ignored issues
show
Documentation introduced by
$ownerId is of type string, but the function expects a object<ValueObjects\StringLiteral\StringLiteral>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            );
60
        }
61
    }
62
63
    protected function applyOrganizerCreated(
64
        OrganizerCreated $organizerCreated,
65
        DomainMessage $domainMessage
66
    ) {
67
        $this->makeOrganizerEditableByUser(
68
            $organizerCreated->getOrganizerId(),
69
            $domainMessage
70
        );
71
    }
72
73
    protected function applyOrganizerCreatedWithUniqueWebsite(
74
        OrganizerCreatedWithUniqueWebsite $organizerCreatedWithUniqueWebsite,
75
        DomainMessage $domainMessage
76
    ) {
77
        $this->makeOrganizerEditableByUser(
78
            $organizerCreatedWithUniqueWebsite->getOrganizerId(),
79
            $domainMessage
80
        );
81
    }
82
83
    /**
84
     * @param string $organizerId
85
     * @param DomainMessage $domainMessage
86
     */
87
    private function makeOrganizerEditableByUser(
88
        string $organizerId,
89
        DomainMessage $domainMessage
90
    ) {
91
        $metadata = $domainMessage->getMetadata()->serialize();
92
        $ownerId = new StringLiteral($metadata['user_id']);
93
94
        $this->permissionRepository->markOfferEditableByUser(
95
            new StringLiteral($organizerId),
96
            $ownerId
97
        );
98
    }
99
}
100