Completed
Push — master ( 73f449...670c74 )
by Kristof
04:33
created

Projector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 71.26 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 13
dl 62
loc 87
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B applyPlaceImportedFromUDB2() 25 25 3
B applyPlaceImportedFromUDB2Event() 25 25 3
A applyPlaceCreated() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Cdb\EventItemFactory;
10
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface;
11
use CultuurNet\UDB3\Place\Events\PlaceCreated;
12
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
13
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
14
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2Event;
15
use ValueObjects\String\String;
16
17
class Projector implements EventListenerInterface
18
{
19
    use DelegateEventHandlingToSpecificMethodTrait;
20
21
    /**
22
     * @var CreatedByToUserIdResolverInterface
23
     */
24
    private $userIdResolver;
25
26
    /**
27
     * @var PermissionRepositoryInterface
28
     */
29
    private $permissionRepository;
30
31
    public function __construct(
32
        PermissionRepositoryInterface $permissionRepository,
33
        CreatedByToUserIdResolverInterface $createdByToUserIdResolver
34
    ) {
35
        $this->userIdResolver = $createdByToUserIdResolver;
36
        $this->permissionRepository = $permissionRepository;
37
    }
38
39 View Code Duplication
    protected function applyPlaceImportedFromUDB2(
0 ignored issues
show
Duplication introduced by
This method 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...
40
        PlaceImportedFromUDB2 $placeImportedFromUDB2
41
    ) {
42
        $cdbActor = ActorItemFactory::createActorFromCdbXml(
43
            $placeImportedFromUDB2->getCdbXmlNamespaceUri(),
44
            $placeImportedFromUDB2->getCdbXml()
45
        );
46
47
        $createdByIdentifier = $cdbActor->getCreatedBy();
48
49
        if ($createdByIdentifier) {
50
            $ownerId = $this->userIdResolver->resolveCreatedByToUserId(
51
                new String($createdByIdentifier)
52
            );
53
54
            if (!$ownerId) {
55
                return;
56
            }
57
58
            $this->permissionRepository->markOfferEditableByUser(
59
                new String($placeImportedFromUDB2->getActorId()),
60
                $ownerId
61
            );
62
        }
63
    }
64
65 View Code Duplication
    protected function applyPlaceImportedFromUDB2Event(
0 ignored issues
show
Duplication introduced by
This method 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...
66
        PlaceImportedFromUDB2Event $placeImportedFromUDB2
67
    ) {
68
        $cdbEvent = EventItemFactory::createEventFromCdbXml(
69
            $placeImportedFromUDB2->getCdbXmlNamespaceUri(),
70
            $placeImportedFromUDB2->getCdbXml()
71
        );
72
73
        $createdByIdentifier = $cdbEvent->getCreatedBy();
74
75
        if ($createdByIdentifier) {
76
            $ownerId = $this->userIdResolver->resolveCreatedByToUserId(
77
                new String($createdByIdentifier)
78
            );
79
80
            if (!$ownerId) {
81
                return;
82
            }
83
84
            $this->permissionRepository->markOfferEditableByUser(
85
                new String($placeImportedFromUDB2->getActorId()),
86
                $ownerId
87
            );
88
        }
89
    }
90
91 View Code Duplication
    protected function applyPlaceCreated(
0 ignored issues
show
Duplication introduced by
This method 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...
92
        PlaceCreated $placeCreated,
93
        DomainMessage $domainMessage
94
    ) {
95
        $metadata = $domainMessage->getMetadata()->serialize();
96
        $ownerId = new String($metadata['user_id']);
97
98
        $this->permissionRepository->markOfferEditableByUser(
99
            new String($placeCreated->getPlaceId()),
100
            $ownerId
101
        );
102
    }
103
}
104