Passed
Push — master ( 488060...dad5ed )
by Igor
06:18 queued 22s
created

AddBarcodeUniqueSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBarcodePlugin\EventListener;
6
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
9
use Doctrine\ORM\Events;
10
use Loevgaard\SyliusBarcodePlugin\Model\BarcodeAwareInterface;
11
12
final class AddBarcodeUniqueSubscriber implements EventSubscriber
13
{
14
    /** @var bool */
15
    private $uniqueEnabled;
16
17
    public function __construct(bool $uniqueEnabled)
18
    {
19
        $this->uniqueEnabled = $uniqueEnabled;
20
    }
21
22
    public function getSubscribedEvents(): array
23
    {
24
        return [
25
            Events::loadClassMetadata,
26
        ];
27
    }
28
29
    public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
30
    {
31
        $metadata = $event->getClassMetadata();
32
33
        if (!is_subclass_of($metadata->name, BarcodeAwareInterface::class, true)) {
34
            return;
35
        }
36
37
        if (!$this->uniqueEnabled) {
38
            return;
39
        }
40
41
        $metadata->fieldMappings['barcode']['unique'] = true;
42
    }
43
}
44