Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
DeleteEventCommand | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
deleteIfAllowed | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
authorizeDeletion | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
deleteUnsafe | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace MediaWiki\Extension\CampaignEvents\Event; |
6 | |
7 | use MediaWiki\Extension\CampaignEvents\Event\Store\IEventStore; |
8 | use MediaWiki\Extension\CampaignEvents\EventPage\EventPageCacheUpdater; |
9 | use MediaWiki\Extension\CampaignEvents\MWEntity\ICampaignsAuthority; |
10 | use MediaWiki\Extension\CampaignEvents\Permissions\PermissionChecker; |
11 | use MediaWiki\Extension\CampaignEvents\TrackingTool\TrackingToolEventWatcher; |
12 | use MediaWiki\Permissions\PermissionStatus; |
13 | use StatusValue; |
14 | |
15 | /** |
16 | * Command object used for the deletion of event registrations. |
17 | */ |
18 | class DeleteEventCommand { |
19 | public const SERVICE_NAME = 'CampaignEventsDeleteEventCommand'; |
20 | |
21 | public const VALIDATE_TRACKING_TOOLS = true; |
22 | public const SKIP_TRACKING_TOOL_VALIDATION = false; |
23 | |
24 | private IEventStore $eventStore; |
25 | private PermissionChecker $permissionChecker; |
26 | private TrackingToolEventWatcher $trackingToolEventWatcher; |
27 | private EventPageCacheUpdater $eventPageCacheUpdater; |
28 | |
29 | public function __construct( |
30 | IEventStore $eventStore, |
31 | PermissionChecker $permissionChecker, |
32 | TrackingToolEventWatcher $trackingToolEventWatcher, |
33 | EventPageCacheUpdater $eventPageCacheUpdater |
34 | ) { |
35 | $this->eventStore = $eventStore; |
36 | $this->permissionChecker = $permissionChecker; |
37 | $this->trackingToolEventWatcher = $trackingToolEventWatcher; |
38 | $this->eventPageCacheUpdater = $eventPageCacheUpdater; |
39 | } |
40 | |
41 | /** |
42 | * @param ExistingEventRegistration $registration |
43 | * @param ICampaignsAuthority $performer |
44 | * @return StatusValue If good, the value is true if the registration was deleted, false if it was already deleted. |
45 | * Will be a PermissionStatus for permissions-related errors. |
46 | */ |
47 | public function deleteIfAllowed( |
48 | ExistingEventRegistration $registration, |
49 | ICampaignsAuthority $performer |
50 | ): StatusValue { |
51 | $permStatus = $this->authorizeDeletion( $registration, $performer ); |
52 | if ( !$permStatus->isGood() ) { |
53 | return $permStatus; |
54 | } |
55 | return $this->deleteUnsafe( $registration ); |
56 | } |
57 | |
58 | /** |
59 | * @param ExistingEventRegistration $registration |
60 | * @param ICampaignsAuthority $performer |
61 | * @return PermissionStatus |
62 | */ |
63 | private function authorizeDeletion( |
64 | ExistingEventRegistration $registration, |
65 | ICampaignsAuthority $performer |
66 | ): PermissionStatus { |
67 | if ( !$this->permissionChecker->userCanDeleteRegistration( $performer, $registration ) ) { |
68 | return PermissionStatus::newFatal( 'campaignevents-delete-not-allowed-registration' ); |
69 | } |
70 | return PermissionStatus::newGood(); |
71 | } |
72 | |
73 | /** |
74 | * @param ExistingEventRegistration $registration |
75 | * @param bool $trackingToolsValidation self::VALIDATE_TRACKING_TOOLS or self::SKIP_TRACKING_TOOL_VALIDATION |
76 | * @return StatusValue If good, the value is true if the registration was deleted, false if it was already deleted. |
77 | */ |
78 | public function deleteUnsafe( |
79 | ExistingEventRegistration $registration, |
80 | bool $trackingToolsValidation = self::VALIDATE_TRACKING_TOOLS |
81 | ): StatusValue { |
82 | if ( $trackingToolsValidation === self::VALIDATE_TRACKING_TOOLS ) { |
83 | $trackingToolValidationStatus = $this->trackingToolEventWatcher->validateEventDeletion( $registration ); |
84 | if ( !$trackingToolValidationStatus->isGood() ) { |
85 | return $trackingToolValidationStatus; |
86 | } |
87 | } |
88 | $effectivelyDeleted = $this->eventStore->deleteRegistration( $registration ); |
89 | |
90 | if ( $effectivelyDeleted ) { |
91 | $this->trackingToolEventWatcher->onEventDeleted( $registration ); |
92 | $this->eventPageCacheUpdater->purgeEventPageCache( $registration ); |
93 | } |
94 | return StatusValue::newGood( $effectivelyDeleted ); |
95 | } |
96 | } |