WebhooksCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 26 5
A __construct() 0 6 1
1
<?php
2
3
namespace CodeCloud\Bundle\ShopifyBundle\Command;
4
5
use CodeCloud\Bundle\ShopifyBundle\Exception\StoreNotFoundException;
6
use CodeCloud\Bundle\ShopifyBundle\Model\ShopifyStoreManagerInterface;
7
use CodeCloud\Bundle\ShopifyBundle\Service\WebhookCreatorInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class WebhooksCommand extends Command
15
{
16
    /**
17
     * @var WebhookCreatorInterface
18
     */
19
    private $webhookCreator;
20
21
    /**
22
     * @var ShopifyStoreManagerInterface
23
     */
24
    private $storeManager;
25
26
    /**
27
     * @var array
28
     */
29
    private $topics = [];
30
31
    /**
32
     * @param WebhookCreatorInterface $webhookCreator
33
     * @param ShopifyStoreManagerInterface $storeManager
34
     * @param array $topics
35
     */
36
    public function __construct(WebhookCreatorInterface $webhookCreator, ShopifyStoreManagerInterface $storeManager, array $topics)
37
    {
38
        $this->webhookCreator = $webhookCreator;
39
        $this->storeManager = $storeManager;
40
        $this->topics = $topics;
41
        parent::__construct();
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setName('codecloud:shopify:webhooks')
48
            ->setDescription('Interact with Shopify Webhooks')
49
            ->addArgument('store', InputArgument::REQUIRED, 'The store to install webhooks in')
50
            ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete existing webhooks')
51
            ->addOption('list', null, InputOption::VALUE_NONE, 'List existing webhooks')
52
        ;
53
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        if (!$this->storeManager->storeExists($store = $input->getArgument('store'))) {
58
            throw new StoreNotFoundException($store);
59
        }
60
61
        if ($input->getOption('list')) {
62
            $output->writeln(print_r($this->webhookCreator->listWebhooks($store), true));
0 ignored issues
show
Bug introduced by
It seems like print_r($this->webhookCr...Webhooks($store), true) can also be of type true; however, parameter $messages of Symfony\Component\Consol...putInterface::writeln() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            $output->writeln(/** @scrutinizer ignore-type */ print_r($this->webhookCreator->listWebhooks($store), true));
Loading history...
63
64
            return;
65
        }
66
67
        if ($input->getOption('delete')) {
68
            $this->webhookCreator->deleteAllWebhooks($store);
69
            $output->writeln('Webhooks deleted');
70
71
            return;
72
        }
73
74
        if (empty($this->topics)) {
75
            throw new \LogicException('No webhook topics configured');
76
        }
77
78
        $this->webhookCreator->createWebhooks($store, $this->topics);
79
80
        $output->writeln('Webhooks created');
81
    }
82
}
83