File size: 1,430 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

declare(strict_types=1);

namespace Plugin\Application\CommandHandlers;

use Plugin\Application\Commands\UpdatePluginCommand;
use Plugin\Domain\Exceptions\PluginNotFoundException;
use Plugin\Domain\Hooks\ActivateHookInterface;
use Plugin\Domain\Hooks\DeactivateHookInterface;
use Plugin\Domain\PluginWrapper;
use Plugin\Domain\Repositories\PluginRepositoryInterface;
use Plugin\Domain\ValueObjects\Status;
use Shared\Infrastructure\CacheManager;

class UpdatePluginCommandHandler
{
    public function __construct(
        private PluginRepositoryInterface $repo,
        private CacheManager $cache
    ) {
    }

    /**
     * @throws PluginNotFoundException
     */
    public function handle(UpdatePluginCommand $cmd): PluginWrapper
    {
        $pw = $this->repo->ofName($cmd->name);

        $context = $pw->context;
        if ($context->getStatus() ==  $cmd->status) {
            return $pw;
        }

        $context->setStatus($cmd->status);
        $ins = $pw->plugin;

        if (
            $cmd->status == Status::ACTIVE
            && $ins instanceof ActivateHookInterface
        ) {
            $ins->activate($pw->context);
        } elseif (
            $cmd->status == Status::INACTIVE
            && $ins instanceof DeactivateHookInterface
        ) {
            $ins->deactivate($pw->context);
        }

        // Clear cache
        $this->cache->clearCache();

        return $pw;
    }
}