Passed
Push — master ( 843515...db168e )
by Diego
04:15
created

mapAffiliateReferralAware()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 3
dl 0
loc 29
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReferralsPlugin\Mapping;
6
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
9
use Doctrine\ORM\Events;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralAwareInterface;
12
use Sylius\Component\Order\Model\OrderInterface;
13
use Sylius\Component\Resource\Metadata\RegistryInterface;
14
15
final class AffiliateReferralAwareListener implements EventSubscriber
16
{
17
    private RegistryInterface $resourceMetadataRegistry;
18
    private string $affiliateReferralClass;
19
20
    public function __construct(
21
        RegistryInterface $resourceMetadataRegistry,
22
        string $affiliateReferralClass
23
    ) {
24
        $this->resourceMetadataRegistry = $resourceMetadataRegistry;
25
        $this->affiliateReferralClass = $affiliateReferralClass;
26
    }
27
28
    public function getSubscribedEvents(): array
29
    {
30
        return [
31
            Events::loadClassMetadata,
32
        ];
33
    }
34
35
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
36
    {
37
        $classMetadata = $eventArgs->getClassMetadata();
38
        $reflection = $classMetadata->reflClass;
0 ignored issues
show
Bug introduced by
Accessing reflClass on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
39
40
        /**
41
         * @phpstan-ignore-next-line
42
         */
43
        if ($reflection === null || $reflection->isAbstract()) {
44
            return;
45
        }
46
47
        if (
48
            $reflection->implementsInterface(OrderInterface::class) &&
49
            $reflection->implementsInterface(AffiliateReferralAwareInterface::class)
50
        ) {
51
            $this->mapAffiliateReferralAware($classMetadata, 'affiliate_referral_id');
52
        }
53
    }
54
55
    private function mapAffiliateReferralAware(
56
        ClassMetadata $metadata,
57
        string $joinColumn,
58
        ?string $inversedBy = null
59
    ): void {
60
        try {
61
            $affiliateReferralMetadata = $this->resourceMetadataRegistry->getByClass($this->affiliateReferralClass);
62
        } catch (\InvalidArgumentException $exception) {
63
            return;
64
        }
65
66
        if (!$metadata->hasAssociation('affiliateReferral')) {
67
            $mapping = [
68
                'fieldName' => 'affiliateReferral',
69
                'targetEntity' => $affiliateReferralMetadata->getClass('model'),
70
                'joinColumns' => [
71
                    [
72
                        'name' => $joinColumn,
73
                        'referencedColumnName' => 'id',
74
                    ]
75
                ],
76
                'cascade' => ['persist']
77
            ];
78
79
            if (null !== $inversedBy) {
80
                $mapping['inversedBy'] = $inversedBy;
81
            }
82
83
            $metadata->mapManyToOne($mapping);
84
        }
85
    }
86
}
87