Passed
Push — master ( 7c51ba...a22fab )
by Peter
04:20
created

Delete::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Console\Commands\ApiClient;
6
7
use AbterPhp\Admin\Orm\ApiClientRepo;
8
use AbterPhp\Framework\Authorization\CacheManager;
9
use Opulence\Console\Commands\Command;
10
use Opulence\Console\Requests\Argument;
11
use Opulence\Console\Requests\ArgumentTypes;
12
use Opulence\Console\Requests\Option;
13
use Opulence\Console\Requests\OptionTypes;
14
use Opulence\Console\Responses\IResponse;
15
use Opulence\Orm\IUnitOfWork;
16
17
class Delete extends Command
18
{
19
    const COMMAND_NAME            = 'apiclient:delete';
20
    const COMMAND_DESCRIPTION     = 'Deletes an existing user';
21
    const COMMAND_SUCCESS         = '<success>Existing API client is deleted.</success>';
22
    const COMMAND_DRY_RUN_MESSAGE = '<info>Dry run prevented deleting existing user.</info>';
23
24
    const ARGUMENT_IDENTIFIER = 'identifier';
25
26
    const OPTION_DRY_RUN    = 'dry-run';
27
    const SHORTENED_DRY_RUN = 'd';
28
29
    /** @var ApiClientRepo */
30
    protected $apiClientRepo;
31
32
    /** @var IUnitOfWork */
33
    protected $unitOfWork;
34
35
    /** @var CacheManager */
36
    protected $cacheManager;
37
38
    /**
39
     * Delete constructor.
40
     *
41
     * @param ApiClientRepo $apiClientRepo
42
     * @param IUnitOfWork   $unitOfWork
43
     * @param CacheManager  $cacheManager
44
     */
45
    public function __construct(ApiClientRepo $apiClientRepo, IUnitOfWork $unitOfWork, CacheManager $cacheManager)
46
    {
47
        $this->apiClientRepo = $apiClientRepo;
48
        $this->unitOfWork    = $unitOfWork;
49
        $this->cacheManager  = $cacheManager;
50
51
        parent::__construct();
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    protected function define()
58
    {
59
        $this->setName(static::COMMAND_NAME)
60
            ->setDescription(static::COMMAND_DESCRIPTION)
61
            ->addArgument(
62
                new Argument(
63
                    static::ARGUMENT_IDENTIFIER,
64
                    ArgumentTypes::REQUIRED,
65
                    'Secret ID'
66
                )
67
            )
68
            ->addOption(
69
                new Option(
70
                    static::OPTION_DRY_RUN,
71
                    static::SHORTENED_DRY_RUN,
72
                    OptionTypes::OPTIONAL_VALUE,
73
                    'Dry run (default: 0)',
74
                    '0'
75
                )
76
            );
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    protected function doExecute(IResponse $response)
83
    {
84
        $identifier = $this->getArgumentValue(static::ARGUMENT_IDENTIFIER);
85
        $dryRun     = $this->getOptionValue(static::OPTION_DRY_RUN);
86
87
        $entity = $this->apiClientRepo->getById($identifier);
88
        if (!$entity) {
0 ignored issues
show
introduced by
$entity is of type object, thus it always evaluated to true.
Loading history...
89
            $response->writeln(sprintf('<fatal>API client not found</fatal>'));
90
91
            return;
92
        }
93
94
        $this->apiClientRepo->delete($entity);
95
96
        if ($dryRun) {
97
            $this->unitOfWork->dispose();
98
            $response->writeln(static::COMMAND_DRY_RUN_MESSAGE);
99
100
            return;
101
        }
102
103
        try {
104
            $this->unitOfWork->commit();
105
            $this->cacheManager->clearAll();
106
        } catch (\Exception $e) {
107
            if ($e->getPrevious()) {
108
                $response->writeln(sprintf('<error>%s</error>', $e->getPrevious()->getMessage()));
109
            }
110
            $response->writeln(sprintf('<fatal>%s</fatal>', $e->getMessage()));
111
112
            return;
113
        }
114
115
        $response->writeln(static::COMMAND_SUCCESS);
116
    }
117
}
118