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

UploadableLoader::loadClassMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
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