EnqueueWebhooksCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A configure() 0 6 1
B execute() 0 44 5
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Command;
4
5
use JMS\Serializer\SerializerInterface;
6
use Loevgaard\DandomainAltapayBundle\Entity\EventRepository;
7
use Loevgaard\DandomainAltapayBundle\Entity\WebhookExchangeRepository;
8
use Loevgaard\DandomainAltapayBundle\Entity\WebhookQueueItem;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Command\LockableTrait;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class EnqueueWebhooksCommand extends ContainerAwareCommand
15
{
16
    use LockableTrait;
17
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23
    /**
24
     * @var WebhookExchangeRepository
25
     */
26
    private $webhookExchangeRepository;
27
28
    /**
29
     * @var EventRepository
30
     */
31
    private $eventRepository;
32
33
    /**
34
     * @var SerializerInterface
35
     */
36
    private $serializer;
37
38
    public function __construct(
39
        WebhookExchangeRepository $webhookExchangeRepository,
40
        EventRepository $eventRepository,
41
        SerializerInterface $serializer
42
    ) {
43
        $this->webhookExchangeRepository = $webhookExchangeRepository;
44
        $this->eventRepository = $eventRepository;
45
        $this->serializer = $serializer;
46
47
        parent::__construct();
48
    }
49
50
    protected function configure()
51
    {
52
        $this->setName('loevgaard:dandomain:altapay:enqueue-webhooks')
53
            ->setDescription('Will enqueue events to be sent to webhooks')
54
        ;
55
    }
56
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        if (!($this->lock())) {
60
            $output->writeln('The command is already running in another process.');
61
62
            return 0;
63
        }
64
65
        $this->output = $output;
66
67
        $maxEventsToEnqueue = 50;
68
69
        $webhookUrls = $this->getContainer()->getParameter('loevgaard_dandomain_altapay.webhook_urls');
70
        if (empty($webhookUrls)) {
71
            $output->writeln('No webhook URLs defined');
72
73
            $this->release();
74
75
            return 0;
76
        }
77
78
        $eventsPerExchange = ceil($maxEventsToEnqueue / count($webhookUrls));
79
80
        foreach ($webhookUrls as $webhookUrl) {
81
            $webhookExchange = $this->webhookExchangeRepository->findByUrlOrCreate($webhookUrl);
82
83
            $events = $this->eventRepository->findRecentEvents($webhookExchange->getLastEventId(), $eventsPerExchange);
84
85
            foreach ($events as $event) {
0 ignored issues
show
Bug introduced by
The expression $events of type array<integer,object<Loe...dle\Entity\Event>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
86
                $webhookQueueItem = new WebhookQueueItem($this->serializer->serialize($event, 'json'), $webhookExchange);
87
88
                $webhookExchange
89
                    ->addWebhookQueueItem($webhookQueueItem)
90
                    ->setLastEventId($event->getId())
91
                ;
92
93
                $this->webhookExchangeRepository->flush();
94
            }
95
        }
96
97
        $this->release();
98
99
        return 0;
100
    }
101
}
102