1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Event\ReadModel\Permission; |
4
|
|
|
|
5
|
|
|
use Broadway\Domain\DomainMessage; |
6
|
|
|
use Broadway\EventHandling\EventListenerInterface; |
7
|
|
|
use CultuurNet\UDB3\Cdb\CreatedByToUserIdResolverInterface; |
8
|
|
|
use CultuurNet\UDB3\Cdb\EventItemFactory; |
9
|
|
|
use CultuurNet\UDB3\Event\Events\EventCreated; |
10
|
|
|
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2; |
11
|
|
|
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait; |
12
|
|
|
use CultuurNet\UDB3\Offer\ReadModel\Permission\PermissionRepositoryInterface; |
13
|
|
|
use ValueObjects\String\String; |
14
|
|
|
|
15
|
|
|
class Projector implements EventListenerInterface |
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
|
|
View Code Duplication |
protected function applyEventImportedFromUDB2( |
|
|
|
|
38
|
|
|
EventImportedFromUDB2 $eventImportedFromUDB2 |
39
|
|
|
) { |
40
|
|
|
$cdbEvent = EventItemFactory::createEventFromCdbXml( |
41
|
|
|
$eventImportedFromUDB2->getCdbXmlNamespaceUri(), |
42
|
|
|
$eventImportedFromUDB2->getCdbXml() |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$createdByIdentifier = $cdbEvent->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($eventImportedFromUDB2->getEventId()), |
58
|
|
|
$ownerId |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
protected function applyEventCreated( |
|
|
|
|
64
|
|
|
EventCreated $eventCreated, |
65
|
|
|
DomainMessage $domainMessage |
66
|
|
|
) { |
67
|
|
|
$metadata = $domainMessage->getMetadata()->serialize(); |
68
|
|
|
$ownerId = new String($metadata['user_id']); |
69
|
|
|
|
70
|
|
|
$this->permissionRepository->markOfferEditableByUser( |
71
|
|
|
new String($eventCreated->getEventId()), |
72
|
|
|
$ownerId |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.