-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: avoided edit/deletion of pre-configured services
- Loading branch information
1 parent
19fab0a
commit 2ec4fb6
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Service; | ||
|
||
use Demo\Domain\Model\Service\Service; | ||
use Demo\Domain\Model\Service\ServiceInterface; | ||
|
||
class AvoidUpdateDelete implements ServiceLifecycleEventHandlerInterface | ||
{ | ||
const PRE_PERSIST_PRIORITY = self::PRIORITY_HIGH; | ||
const PRE_REMOVE_PRIORITY = self::PRIORITY_HIGH; | ||
|
||
/** | ||
* @return array<string, int> | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
self::EVENT_PRE_PERSIST => self::PRE_PERSIST_PRIORITY, | ||
self::EVENT_PRE_REMOVE => self::PRE_REMOVE_PRIORITY | ||
]; | ||
} | ||
|
||
public function execute(ServiceInterface $service): void | ||
{ | ||
if ($service->isNew()) { | ||
return; | ||
} | ||
|
||
$iden = $service->getInitialValue('iden'); | ||
|
||
if (in_array($iden, Service::BUILTIN_SERVICES)) { | ||
$msg = $service->hasBeenDeleted() | ||
? 'Service ' . $iden . 'can’t be deleted' | ||
: 'Service ' . $iden . 'can’t be edited'; | ||
|
||
throw new \DomainException($msg); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/Demo/Domain/Service/Service/ServiceLifecycleEventHandlerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Service; | ||
|
||
use Demo\Domain\Model\Service\ServiceInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface; | ||
|
||
interface ServiceLifecycleEventHandlerInterface extends LifecycleEventHandlerInterface | ||
{ | ||
public function execute(ServiceInterface $service): void; | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/Demo/Domain/Service/Service/ServiceLifecycleServiceCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Demo\Domain\Service\Service; | ||
|
||
use Ivoz\Core\Domain\Assert\Assertion;; | ||
use Ivoz\Core\Domain\Service\DomainEventSubscriberInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionInterface; | ||
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionTrait; | ||
|
||
class ServiceLifecycleServiceCollection implements LifecycleServiceCollectionInterface | ||
{ | ||
use LifecycleServiceCollectionTrait; | ||
|
||
/** @var array<array-key, array> $bindedBaseServices */ | ||
public static $bindedBaseServices = [ | ||
"pre_persist" => | ||
[ | ||
\Demo\Domain\Service\Service\AvoidUpdateDelete::class => 100, | ||
], | ||
"pre_remove" => | ||
[ | ||
\Demo\Domain\Service\Service\AvoidUpdateDelete::class => 100, | ||
], | ||
]; | ||
|
||
protected function addService(string $event, LifecycleEventHandlerInterface|DomainEventSubscriberInterface $service): void | ||
{ | ||
Assertion::isInstanceOf($service, ServiceLifecycleEventHandlerInterface::class); | ||
$this->services[$event][] = $service; | ||
} | ||
} |