1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Subscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs; |
7
|
|
|
use Doctrine\ODM\PHPCR\Event; |
8
|
|
|
use Metadata\MetadataFactory; |
9
|
|
|
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\FieldMapper; |
10
|
|
|
use Psi\Component\ContentType\FieldLoader; |
11
|
|
|
|
12
|
|
View Code Duplication |
class MetadataSubscriber implements EventSubscriber |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
private $metadataFactory; |
15
|
|
|
private $fieldLoader; |
16
|
|
|
private $mapper; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
MetadataFactory $metadataFactory, |
20
|
|
|
FieldLoader $fieldLoader, |
21
|
|
|
FieldMapper $mapper |
22
|
|
|
) { |
23
|
|
|
$this->metadataFactory = $metadataFactory; |
24
|
|
|
$this->fieldLoader = $fieldLoader; |
25
|
|
|
$this->mapper = $mapper; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getSubscribedEvents() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
Event::loadClassMetadata, |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $args) |
36
|
|
|
{ |
37
|
|
|
$metadata = $args->getClassMetadata(); |
38
|
|
|
|
39
|
|
|
if (null === $metadata = $this->metadataFactory->getMetadataForClass($metadata->getName())) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$odmMetadata = $args->getClassMetadata(); |
44
|
|
|
|
45
|
|
|
foreach ($metadata->getPropertyMetadata() as $property) { |
46
|
|
|
try { |
47
|
|
|
$field = $this->fieldLoader->load($property->getType(), $property->getOptions()); |
48
|
|
|
$this->mapper->__invoke($property->getName(), $field, $odmMetadata); |
|
|
|
|
49
|
|
|
} catch (\Exception $e) { |
50
|
|
|
throw new \InvalidArgumentException(sprintf( |
51
|
|
|
'Could not map field type "%s" on property "%s#%s"', |
52
|
|
|
$property->getType(), $property->getClass(), $property->getName() |
53
|
|
|
), null, $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.