Skip to content

Commit 4d17c9b

Browse files
committed
core: avoided edit/deletion of pre-configured services
1 parent 6e4097d commit 4d17c9b

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

app/src/Demo/Domain/Model/Service/Service.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Service extends ServiceAbstract implements ServiceInterface
99
{
1010
use ServiceTrait;
1111

12+
const BUILTIN_SERVICES = ['Recording', 'Voicemail', 'Queues'];
13+
1214
/**
1315
* @codeCoverageIgnore
1416
* @return array<string, mixed>
@@ -22,7 +24,7 @@ public function getChangeSet(): array
2224
* Get id
2325
* @codeCoverageIgnore
2426
*/
25-
public function getId(): int|string
27+
public function getId(): null|int
2628
{
2729
return $this->id;
2830
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Demo\Domain\Service\Service;
4+
5+
use Demo\Domain\Model\Service\Service;
6+
use Demo\Domain\Model\Service\ServiceInterface;
7+
8+
class AvoidUpdateDelete implements ServiceLifecycleEventHandlerInterface
9+
{
10+
const PRE_PERSIST_PRIORITY = self::PRIORITY_HIGH;
11+
const PRE_REMOVE_PRIORITY = self::PRIORITY_HIGH;
12+
13+
/**
14+
* @return array<string, int>
15+
*/
16+
public static function getSubscribedEvents(): array
17+
{
18+
return [
19+
self::EVENT_PRE_PERSIST => self::PRE_PERSIST_PRIORITY,
20+
self::EVENT_PRE_REMOVE => self::PRE_REMOVE_PRIORITY
21+
];
22+
}
23+
24+
public function execute(ServiceInterface $service): void
25+
{
26+
if ($service->isNew()) {
27+
return;
28+
}
29+
30+
$iden = $service->getInitialValue('iden');
31+
32+
if (in_array($iden, Service::BUILTIN_SERVICES)) {
33+
$msg = $service->hasBeenDeleted()
34+
? 'Service ' . $iden . 'can’t be deleted'
35+
: 'Service ' . $iden . 'can’t be edited';
36+
37+
throw new \DomainException($msg);
38+
}
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Demo\Domain\Service\Service;
4+
5+
use Demo\Domain\Model\Service\ServiceInterface;
6+
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface;
7+
8+
interface ServiceLifecycleEventHandlerInterface extends LifecycleEventHandlerInterface
9+
{
10+
public function execute(ServiceInterface $service): void;
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Demo\Domain\Service\Service;
4+
5+
use Ivoz\Core\Domain\Assert\Assertion;;
6+
use Ivoz\Core\Domain\Service\DomainEventSubscriberInterface;
7+
use Ivoz\Core\Domain\Service\LifecycleEventHandlerInterface;
8+
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionInterface;
9+
use Ivoz\Core\Domain\Service\LifecycleServiceCollectionTrait;
10+
11+
class ServiceLifecycleServiceCollection implements LifecycleServiceCollectionInterface
12+
{
13+
use LifecycleServiceCollectionTrait;
14+
15+
/** @var array<array-key, array> $bindedBaseServices */
16+
public static $bindedBaseServices = [
17+
"pre_persist" =>
18+
[
19+
\Demo\Domain\Service\Service\AvoidUpdateDelete::class => 100,
20+
],
21+
"pre_remove" =>
22+
[
23+
\Demo\Domain\Service\Service\AvoidUpdateDelete::class => 100,
24+
],
25+
];
26+
27+
protected function addService(string $event, LifecycleEventHandlerInterface|DomainEventSubscriberInterface $service): void
28+
{
29+
Assertion::isInstanceOf($service, ServiceLifecycleEventHandlerInterface::class);
30+
$this->services[$event][] = $service;
31+
}
32+
}

0 commit comments

Comments
 (0)