1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Place\ReadModel\JSONLD; |
4
|
|
|
|
5
|
|
|
use Broadway\EventHandling\EventListenerInterface; |
6
|
|
|
use CultuurNet\UDB3\EntityServiceInterface; |
7
|
|
|
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface; |
8
|
|
|
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait; |
9
|
|
|
use CultuurNet\UDB3\Organizer\OrganizerProjectedToJSONLD; |
10
|
|
|
use CultuurNet\UDB3\Place\ReadModel\Relations\RepositoryInterface; |
11
|
|
|
|
12
|
|
|
class RelatedPlaceLDProjector implements EventListenerInterface |
13
|
|
|
{ |
14
|
|
|
use DelegateEventHandlingToSpecificMethodTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var RepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $placeRelations; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DocumentRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EntityServiceInterface |
28
|
|
|
*/ |
29
|
|
|
private $organizerService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DocumentRepositoryInterface $repository |
33
|
|
|
* @param EntityServiceInterface $organizerService |
34
|
|
|
* @param RepositoryInterface $placeRelations |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
DocumentRepositoryInterface $repository, |
38
|
|
|
EntityServiceInterface $organizerService, |
39
|
|
|
RepositoryInterface $placeRelations |
40
|
|
|
) { |
41
|
|
|
$this->repository = $repository; |
42
|
|
|
$this->organizerService = $organizerService; |
43
|
|
|
$this->placeRelations = $placeRelations; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param OrganizerProjectedToJSONLD $organizerProjectedToJSONLD |
48
|
|
|
* |
49
|
|
|
* @throws \CultuurNet\UDB3\EntityNotFoundException |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
protected function applyOrganizerProjectedToJSONLD( |
|
|
|
|
52
|
|
|
OrganizerProjectedToJSONLD $organizerProjectedToJSONLD |
53
|
|
|
) { |
54
|
|
|
$placeIds = $this->placeRelations->getPlacesOrganizedByOrganizer( |
55
|
|
|
$organizerProjectedToJSONLD->getId() |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$organizer = $this->organizerService->getEntity( |
59
|
|
|
$organizerProjectedToJSONLD->getId() |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$organizerJSONLD = json_decode($organizer); |
63
|
|
|
|
64
|
|
|
foreach ($placeIds as $placeId) { |
65
|
|
|
$this->updateEmbeddedOrganizer($placeId, $organizerJSONLD); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
private function updateEmbeddedOrganizer(string $placeId, $organizerJSONLD) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$document = $this->repository->get($placeId); |
72
|
|
|
|
73
|
|
|
if (!$document) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$placeLD = $document->getBody(); |
78
|
|
|
|
79
|
|
|
$newPlaceLD = clone $placeLD; |
80
|
|
|
$newPlaceLD->organizer = $organizerJSONLD; |
81
|
|
|
|
82
|
|
|
if ($newPlaceLD == $placeLD) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->repository->save($document->withBody($newPlaceLD)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.