Completed
Push — master ( 3260ca...0c0ec5 )
by Christian
02:12
created

DeleteCommand::inject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace N98\Magento\Command\Integration;
4
5
use Magento\Integration\Model\IntegrationService;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use Symfony\Component\Console\Exception\RuntimeException;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Class DeleteCommand
14
 * @package N98\Magento\Command\Integration
15
 */
16
class DeleteCommand extends AbstractMagentoCommand
17
{
18
    /**
19
     * @var IntegrationService
20
     */
21
    private $integrationService;
22
23
    /**
24
     * @var OauthService
25
     */
26
    private $oauthService;
27
28
    /**
29
     * @var TokenCollectionFactory
30
     */
31
    private $tokenCollectionFactory;
32
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('integration:delete')
37
            ->addArgument('name', InputArgument::REQUIRED, 'Name or ID of the integration')
38
            ->setDescription('Show details of an existing integration.');
39
    }
40
41
    public function inject(
42
        IntegrationService $integrationService
43
    ) {
44
        $this->integrationService = $integrationService;
45
    }
46
47
    /**
48
     * @param \Symfony\Component\Console\Input\InputInterface $input
49
     * @param \Symfony\Component\Console\Output\OutputInterface $output
50
     * @return int|void
51
     * @throws \Exception
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $integrationName = $input->getArgument('name');
56
57
        $integrationModel = $this->integrationService->findByName($integrationName);
58
59
        if ($integrationModel->getId() <= 0 && is_numeric($integrationName)) {
60
            $integrationModel = $this->integrationService->get($integrationName);
61
        }
62
63
        if ($integrationModel->getId() <= 0) {
64
            throw new RuntimeException('Integration with this name or ID does not exist.');
65
        }
66
67
        $this->integrationService->delete($integrationModel->getId());
68
        $output->writeln(
69
            sprintf(
70
                '<info>Successfully deleted integration <comment>%s</comment> with ID: <comment>%d</comment></info>',
71
                $integrationModel->getName(),
72
                $integrationModel->getId()
73
            )
74
        );
75
    }
76
}
77