Skip to content

Commit

Permalink
core: avoided edit/deletion of pre-configured services
Browse files Browse the repository at this point in the history
  • Loading branch information
danigargar committed Feb 9, 2024
1 parent 19fab0a commit 2ec4fb6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/src/Demo/Domain/Model/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Service extends ServiceAbstract implements ServiceInterface
{
use ServiceTrait;

const BUILTIN_SERVICES = ['Recording', 'Voicemail', 'Queues'];

/**
* @codeCoverageIgnore
* @return array<string, mixed>
Expand All @@ -22,7 +24,7 @@ public function getChangeSet(): array
* Get id
* @codeCoverageIgnore
*/
public function getId(): int|string
public function getId(): null|int
{
return $this->id;
}
Expand Down
40 changes: 40 additions & 0 deletions app/src/Demo/Domain/Service/Service/AvoidUpdateDelete.php
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);
}
}
}
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;
}
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;
}
}

0 comments on commit 2ec4fb6

Please sign in to comment.