|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Loevgaard\SyliusBarcodePlugin\Message\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use Loevgaard\SyliusBarcodePlugin\BarcodeChecker\BarcodeCheckerInterface; |
|
11
|
|
|
use Loevgaard\SyliusBarcodePlugin\Message\Command\ProcessBatch; |
|
12
|
|
|
use Loevgaard\SyliusBarcodePlugin\Model\BarcodeAwareInterface; |
|
13
|
|
|
use Safe\Exceptions\StringsException; |
|
14
|
|
|
use function Safe\sprintf; |
|
15
|
|
|
use Setono\DoctrineORMBatcher\Query\QueryRebuilder; |
|
16
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class ProcessBatchHandler implements MessageHandlerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ManagerRegistry */ |
|
21
|
|
|
private $managerRegistry; |
|
22
|
|
|
|
|
23
|
|
|
/** @var BarcodeCheckerInterface */ |
|
24
|
|
|
private $barcodeChecker; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(ManagerRegistry $managerRegistry, BarcodeCheckerInterface $barcodeChecker) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->managerRegistry = $managerRegistry; |
|
29
|
|
|
$this->barcodeChecker = $barcodeChecker; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws StringsException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __invoke(ProcessBatch $message): void |
|
36
|
|
|
{ |
|
37
|
|
|
$queryRebuilder = new QueryRebuilder($this->managerRegistry); |
|
38
|
|
|
|
|
39
|
|
|
$q = $queryRebuilder->rebuild($message->getBatch()); |
|
40
|
|
|
|
|
41
|
|
|
/** @var BarcodeAwareInterface[] $variants */ |
|
42
|
|
|
$variants = $q->getResult(); |
|
43
|
|
|
|
|
44
|
|
|
$manager = $this->getManager($message->getBatch()->getClass()); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($variants as $variant) { |
|
47
|
|
|
$this->barcodeChecker->check($variant); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$manager->flush(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws StringsException |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getManager(string $class): EntityManagerInterface |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var EntityManagerInterface|null $manager */ |
|
59
|
|
|
$manager = $this->managerRegistry->getManagerForClass($class); |
|
60
|
|
|
|
|
61
|
|
|
if (null === $manager) { |
|
62
|
|
|
throw new InvalidArgumentException(sprintf('No manager associated with class %s', $class)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $manager; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|