Passed
Pull Request — master (#52)
by Daniel
05:34
created

UploadableLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
c 0
b 0
f 0
dl 0
loc 25
ccs 3
cts 10
cp 0.3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadClassMetadata() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Validator\MappingLoader;
15
16
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader;
17
use Symfony\Component\Validator\Constraints as Assert;
18
use Symfony\Component\Validator\Mapping\ClassMetadata;
19
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class UploadableLoader implements LoaderInterface
25
{
26
    private UploadableAnnotationReader $uploadsHelper;
27
28 1
    public function __construct(UploadableAnnotationReader $uploadsHelper)
29
    {
30 1
        $this->uploadsHelper = $uploadsHelper;
31 1
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function loadClassMetadata(ClassMetadata $metadata): bool
37
    {
38
        if (!$this->uploadsHelper->isConfigured($metadata->getClassName())) {
39
            return false;
40
        }
41
42
        $fields = $this->uploadsHelper->getConfiguredProperties($metadata->getClassName(), true, false);
43
44
        foreach ($fields as $fileField) {
45
            $metadata->addPropertyConstraint($fileField, new Assert\NotNull(['groups' => sprintf('%s:create', $metadata->getClassName())]));
46
        }
47
48
        return true;
49
    }
50
}
51