File size: 1,112 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

declare(strict_types=1);

namespace Preset\Application\CommandHandlers;

use Preset\Application\Commands\DeletePresetCommand;
use Preset\Domain\Events\PresetDeletedEvent;
use Preset\Domain\Exceptions\LockedPresetException;
use Preset\Domain\Exceptions\PresetNotFoundException;
use Preset\Domain\Repositories\PresetRepositoryInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

class DeletePresetCommandHandler
{
    public function __construct(
        private PresetRepositoryInterface $repo,
        private EventDispatcherInterface $dispatcher
    ) {
    }

    /**
     * @throws PresetNotFoundException
     * @throws LockedPresetException
     */
    public function handle(DeletePresetCommand $cmd): void
    {
        $preset = $this->repo->ofId($cmd->id);

        if ($preset->isLocked()) {
            throw new LockedPresetException();
        }

        // Delete the preset from the repository
        $this->repo->remove($preset);

        // Dispatch the preset deleted event
        $event = new PresetDeletedEvent($preset);
        $this->dispatcher->dispatch($event);
    }
}