Completed
Pull Request — master (#272)
by Kristof
12:29
created

Projector::applyPlaceImportedFromUDB2Event()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Place\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\Place\Events\PlaceCreated;
12
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
13
use ValueObjects\String\String;
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 applyPlaceImportedFromUDB2(
38
        PlaceImportedFromUDB2 $placeImportedFromUDB2
39
    ) {
40
        $cdbActor = ActorItemFactory::createActorFromCdbXml(
41
            $placeImportedFromUDB2->getCdbXmlNamespaceUri(),
42
            $placeImportedFromUDB2->getCdbXml()
43
        );
44
45
        $createdByIdentifier = $cdbActor->getCreatedBy();
46
47
        if ($createdByIdentifier) {
48
            $ownerId = $this->userIdResolver->resolveCreatedByToUserId(
49
                new String($createdByIdentifier)
50
            );
51
52
            if (!$ownerId) {
53
                return;
54
            }
55
56
            $this->permissionRepository->markOfferEditableByUser(
57
                new String($placeImportedFromUDB2->getActorId()),
58
                $ownerId
59
            );
60
        }
61
    }
62
63
    protected function applyPlaceCreated(
64
        PlaceCreated $placeCreated,
65
        DomainMessage $domainMessage
66
    ) {
67
        $metadata = $domainMessage->getMetadata()->serialize();
68
        $ownerId = new String($metadata['user_id']);
69
70
        $this->permissionRepository->markOfferEditableByUser(
71
            new String($placeCreated->getPlaceId()),
72
            $ownerId
73
        );
74
    }
75
}
76