CheckBarcodesCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 4
eloc 22
c 1
b 1
f 1
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A execute() 0 17 2
A configure() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBarcodePlugin\Command;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Loevgaard\SyliusBarcodePlugin\Message\Command\ProcessBatch;
9
use Setono\DoctrineORMBatcher\Batch\RangeBatchInterface;
10
use Setono\DoctrineORMBatcher\Factory\BatcherFactoryInterface;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Messenger\MessageBusInterface;
15
16
class CheckBarcodesCommand extends Command
17
{
18
    /** @var EntityManagerInterface */
19
    private $productVariantManager;
20
21
    /** @var MessageBusInterface */
22
    private $commandBus;
23
24
    /** @var BatcherFactoryInterface */
25
    private $batcherFactory;
26
27
    /** @var string */
28
    private $productVariantClass;
29
30
    public function __construct(
31
        EntityManagerInterface $productVariantManager,
32
        MessageBusInterface $commandBus,
33
        BatcherFactoryInterface $batcherFactory,
34
        string $productVariantClass
35
    ) {
36
        parent::__construct();
37
38
        $this->productVariantManager = $productVariantManager;
39
        $this->commandBus = $commandBus;
40
        $this->batcherFactory = $batcherFactory;
41
        $this->productVariantClass = $productVariantClass;
42
    }
43
44
    protected function configure(): void
45
    {
46
        $this
47
            ->setName('loevgaard:barcode:check')
48
            ->setDescription('Checks barcodes on products')
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $qb = $this->productVariantManager->createQueryBuilder();
55
        $qb->select('o')
56
            ->from($this->productVariantClass, 'o')
57
            ->andWhere('o.barcodeChecked is null')
58
        ;
59
60
        $bestChoiceIdBatcher = $this->batcherFactory->createIdRangeBatcher($qb);
61
62
        /** @var RangeBatchInterface[] $batches */
63
        $batches = $bestChoiceIdBatcher->getBatches(50);
64
        foreach ($batches as $batch) {
65
            $this->commandBus->dispatch(new ProcessBatch($batch));
66
        }
67
68
        return 0;
69
    }
70
}
71