Completed
Push — master ( cc88f5...694b48 )
by Kristof
22s queued 10s
created

RelatedEventLDProjector::updateJSONLD()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use Broadway\EventHandling\EventListenerInterface;
6
use CultuurNet\UDB3\Event\EventServiceInterface;
7
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
8
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
9
use CultuurNet\UDB3\Offer\IriOfferIdentifierFactoryInterface;
10
use CultuurNet\UDB3\Organizer\OrganizerProjectedToJSONLD;
11
use CultuurNet\UDB3\OrganizerService;
12
use CultuurNet\UDB3\Place\Events\PlaceProjectedToJSONLD;
13
use CultuurNet\UDB3\PlaceService;
14
use ValueObjects\Web\Url;
15
16
class RelatedEventLDProjector implements EventListenerInterface
17
{
18
    use DelegateEventHandlingToSpecificMethodTrait;
19
20
    /**
21
     * @var PlaceService
22
     */
23
    protected $placeService;
24
25
    /**
26
     * @var OrganizerServiceInterface
27
     */
28
    protected $organizerService;
29
30
    /**
31
     * @var EventServiceInterface
32
     */
33
    protected $eventService;
34
35
    /**
36
     * @var IriOfferIdentifierFactoryInterface
37
     */
38
    protected $iriOfferIdentifierFactory;
39
40
    /**
41
     * @param DocumentRepositoryInterface $repository
42
     * @param EventServiceInterface $eventService
43
     * @param PlaceService $placeService
44
     * @param OrganizerService $organizerService
45
     */
46
    public function __construct(
47
        DocumentRepositoryInterface $repository,
48
        EventServiceInterface $eventService,
49
        PlaceService $placeService,
50
        OrganizerService $organizerService,
51
        IriOfferIdentifierFactoryInterface $iriOfferIdentifierFactory
52
    ) {
53
        $this->repository = $repository;
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        $this->eventService = $eventService;
55
        $this->placeService = $placeService;
56
        $this->organizerService = $organizerService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $organizerService of type object<CultuurNet\UDB3\OrganizerService> is incompatible with the declared type object<CultuurNet\UDB3\E...anizerServiceInterface> of property $organizerService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
58
59
        $this->iriOfferIdentifierFactory = $iriOfferIdentifierFactory;
60
    }
61
62 View Code Duplication
    protected function applyOrganizerProjectedToJSONLD(
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...
63
        OrganizerProjectedToJSONLD $organizerProjectedToJSONLD
64
    ) {
65
        $eventIds = $this->eventService->eventsOrganizedByOrganizer(
66
            $organizerProjectedToJSONLD->getId()
67
        );
68
69
        $organizer = $this->organizerService->getEntity(
0 ignored issues
show
Bug introduced by
The method getEntity() does not seem to exist on object<CultuurNet\UDB3\E...anizerServiceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            $organizerProjectedToJSONLD->getId()
71
        );
72
73
        $organizerJSONLD = json_decode($organizer);
74
75
        foreach ($eventIds as $eventId) {
76
            $this->updateEmbeddedOrganizer($eventId, $organizerJSONLD);
77
        }
78
    }
79
80
    protected function applyPlaceProjectedToJSONLD(
81
        PlaceProjectedToJSONLD $placeProjectedToJSONLD
82
    ) {
83
        $identifier = $this->iriOfferIdentifierFactory->fromIri(
84
            Url::fromNative($placeProjectedToJSONLD->getIri())
85
        );
86
87
        $eventsLocatedAtPlace = $this->eventService->eventsLocatedAtPlace(
88
            $placeProjectedToJSONLD->getItemId()
89
        );
90
91
        $placeJSONLDString = $this->placeService->getEntity(
92
            $identifier->getId()
93
        );
94
        $placeJSONLD = json_decode($placeJSONLDString);
95
96
        foreach ($eventsLocatedAtPlace as $eventId) {
97
            $this->updatedEmbeddedLocation($eventId, $placeJSONLD);
98
        }
99
    }
100
101
    private function updateEmbeddedOrganizer($eventId, $organizerJSONLD)
102
    {
103
        $this->updateJSONLD(
104
            $eventId,
105
            function ($eventLd) use ($organizerJSONLD) {
106
                $eventLd->organizer = $organizerJSONLD;
107
            }
108
        );
109
    }
110
111
    private function updatedEmbeddedLocation($eventId, $placeJSONLD)
112
    {
113
        $this->updateJSONLD(
114
            $eventId,
115
            function ($eventLd) use ($placeJSONLD) {
116
                $eventLd->location = $placeJSONLD;
117
            }
118
        );
119
    }
120
121 View Code Duplication
    private function updateJSONLD($eventId, $callback)
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...
122
    {
123
        $document = $this->repository->get($eventId);
124
125
        if (!$document) {
126
            return;
127
        }
128
129
        $eventLD = $document->getBody();
130
131
        $newEventLD = clone $eventLD;
132
133
        $callback($newEventLD);
134
135
        if ($newEventLD == $eventLD) {
136
            return;
137
        }
138
139
        $document = $document->withBody($newEventLD);
140
141
        $this->repository->save($document);
142
    }
143
}
144