1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusAvataxPlugin\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\SyliusAvataxPlugin\Entity\AvataxAwareInterface; |
12
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
13
|
|
|
use Sylius\Component\Resource\Metadata\RegistryInterface; |
14
|
|
|
|
15
|
|
|
final class AvataxAwareListener implements EventSubscriber |
16
|
|
|
{ |
17
|
|
|
/** @var RegistryInterface */ |
18
|
|
|
private $resourceMetadataRegistry; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $productClass; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
RegistryInterface $resourceMetadataRegistry, |
25
|
|
|
string $productClass |
26
|
|
|
) { |
27
|
|
|
$this->resourceMetadataRegistry = $resourceMetadataRegistry; |
28
|
|
|
$this->productClass = $productClass; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function getSubscribedEvents(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
Events::loadClassMetadata, |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
43
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
44
|
|
|
*/ |
45
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
46
|
|
|
{ |
47
|
|
|
$classMetadata = $eventArgs->getClassMetadata(); |
48
|
|
|
$reflection = $classMetadata->reflClass; |
49
|
|
|
|
50
|
|
|
if ($reflection->isAbstract()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( |
55
|
|
|
$reflection->implementsInterface(ProductInterface::class) && |
56
|
|
|
$reflection->implementsInterface(AvataxAwareInterface::class) |
57
|
|
|
) { |
58
|
|
|
$this->mapAvataxAware($classMetadata); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ClassMetadata $metadata |
64
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
65
|
|
|
*/ |
66
|
|
|
private function mapAvataxAware(ClassMetadata $metadata): void |
67
|
|
|
{ |
68
|
|
|
if (!$metadata->hasField('avataxCode')) { |
69
|
|
|
$metadata->mapField([ |
70
|
|
|
'fieldName' => 'avataxCode', |
71
|
|
|
'columnName' => 'avatax_code', |
72
|
|
|
'type' => 'string', |
73
|
|
|
'nullable' => true |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|