UploadableListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 32
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadClassMetadata() 0 23 5
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\EventListener\Doctrine;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Mapping\ClassMetadataInfo;
18
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
19
use Silverback\ApiComponentsBundle\AttributeReader\UploadableAttributeReader;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class UploadableListener
25
{
26
    private UploadableAttributeReader $uploadableAnnotationReader;
27
28
    public function __construct(UploadableAttributeReader $uploadableAnnotationReader)
29
    {
30
        $this->uploadableAnnotationReader = $uploadableAnnotationReader;
31
    }
32
33
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
34
    {
35
        /** @var ClassMetadataInfo $metadata */
36
        $metadata = $eventArgs->getClassMetadata();
37
        $className = $metadata->getName();
38
        if (!$this->uploadableAnnotationReader->isConfigured($className)) {
39
            return;
40
        }
41
42
        $em = $eventArgs->getObjectManager();
43
        if (!$em instanceof EntityManagerInterface) {
44
            return;
45
        }
46
47
        $propertyConfigurations = $this->uploadableAnnotationReader->getConfiguredProperties($className, true);
48
49
        foreach ($propertyConfigurations as $propertyConfiguration) {
50
            if (!$metadata->hasField($propertyConfiguration->property)) {
51
                $metadata->mapField(
52
                    [
53
                        'fieldName' => $propertyConfiguration->property,
54
                        'type' => 'string',
55
                        'nullable' => true,
56
                    ]
57
                );
58
            }
59
        }
60
    }
61
}
62