Completed
Pull Request — master (#379)
by Kristof
07:33
created

applyOrganizerCreatedWithUniqueWebsite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
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\CreatedByToUserIdResolverInterface;
8
use CultuurNet\UDB3\Cdb\ActorItemFactory;
9
use CultuurNet\UDB3\Event\Events\EventCopied;
10
use CultuurNet\UDB3\Event\Events\EventCreated;
11
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
12
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
13
use CultuurNet\UDB3\Offer\Events\AbstractEvent;
14
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface;
15
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
16
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
17
use ValueObjects\StringLiteral\StringLiteral;
18
19 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...
20
{
21
    use DelegateEventHandlingToSpecificMethodTrait;
22
23
    /**
24
     * @var CreatedByToUserIdResolverInterface
25
     */
26
    private $userIdResolver;
27
28
    /**
29
     * @var PermissionRepositoryInterface
30
     */
31
    private $permissionRepository;
32
33
    public function __construct(
34
        PermissionRepositoryInterface $permissionRepository,
35
        CreatedByToUserIdResolverInterface $createdByToUserIdResolver
36
    ) {
37
        $this->userIdResolver = $createdByToUserIdResolver;
38
        $this->permissionRepository = $permissionRepository;
39
    }
40
41
    protected function applyOrganizerImportedFromUDB2(
42
        OrganizerImportedFromUDB2 $organizerImportedFromUDB2
43
    ) {
44
        $cdbEvent = ActorItemFactory::createActorFromCdbXml(
45
            $organizerImportedFromUDB2->getCdbXmlNamespaceUri(),
46
            $organizerImportedFromUDB2->getCdbXml()
47
        );
48
49
        $createdByIdentifier = $cdbEvent->getCreatedBy();
50
51
        if ($createdByIdentifier) {
52
            $ownerId = $this->userIdResolver->resolveCreatedByToUserId(
53
                new StringLiteral($createdByIdentifier)
54
            );
55
56
            if (!$ownerId) {
57
                return;
58
            }
59
60
            $this->permissionRepository->markOfferEditableByUser(
61
                new StringLiteral($organizerImportedFromUDB2->getEventId()),
62
                $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...
63
            );
64
        }
65
    }
66
67
    protected function applyOrganizerCreated(
68
        OrganizerCreated $organizerCreated,
69
        DomainMessage $domainMessage
70
    ) {
71
        $this->makeOrganizerEditableByUser(
72
            $organizerCreated->getOrganizerId(),
73
            $domainMessage
74
        );
75
    }
76
77
    protected function applyOrganizerCreatedWithUniqueWebsite(
78
        OrganizerCreatedWithUniqueWebsite $organizerCreatedWithUniqueWebsite,
79
        DomainMessage $domainMessage
80
    ) {
81
        $this->makeOrganizerEditableByUser(
82
            $organizerCreatedWithUniqueWebsite->getOrganizerId(),
83
            $domainMessage
84
        );
85
    }
86
87
    /**
88
     * @param string $organizerId
89
     * @param DomainMessage $domainMessage
90
     */
91
    private function makeOrganizerEditableByUser(
92
        string $organizerId,
93
        DomainMessage $domainMessage
94
    ) {
95
        $metadata = $domainMessage->getMetadata()->serialize();
96
        $ownerId = new StringLiteral($metadata['user_id']);
97
98
        $this->permissionRepository->markOfferEditableByUser(
99
            new StringLiteral($organizerId),
100
            $ownerId
101
        );
102
    }
103
}
104