Test Failed
Push — master ( a50670...f1acbc )
by Dominik
03:57
created

CourseMapping::getPropertyMappings()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nc 1
nop 0
dl 0
loc 45
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Validation;
6
7
use Chubbyphp\ApiSkeleton\Model\Document;
8
use Chubbyphp\Model\Collection\ModelCollectionInterface;
9
use Chubbyphp\Model\ResolverInterface;
10
use Chubbyphp\Validation\Constraint\CallbackConstraint;
11
use Chubbyphp\Validation\Constraint\ChoiceConstraint;
12
use Chubbyphp\Validation\Constraint\ConstraintInterface;
13
use Chubbyphp\Validation\Constraint\NotBlankConstraint;
14
use Chubbyphp\Validation\Constraint\NotNullConstraint;
15
use Chubbyphp\Validation\Constraint\NumericConstraint;
16
use Chubbyphp\Validation\Constraint\NumericRangeConstraint;
17
use Chubbyphp\Validation\Error\Error;
18
use Chubbyphp\Validation\Mapping\ObjectMappingInterface;
19
use Chubbyphp\Validation\Mapping\PropertyMapping;
20
use Chubbyphp\Validation\Mapping\PropertyMappingInterface;
21
use Chubbyphp\ApiSkeleton\Model\Course;
22
use Chubbyphp\ValidationModel\Constraint\ModelCollectionConstraint;
23
use Chubbyphp\ValidationModel\Constraint\UniqueModelConstraint;
24
25
final class CourseMapping implements ObjectMappingInterface
26
{
27
    /**
28
     * @var ResolverInterface
29
     */
30
    private $resolver;
31
32
    /**
33
     * @param ResolverInterface $resolver
34
     */
35
    public function __construct(ResolverInterface $resolver)
36
    {
37
        $this->resolver = $resolver;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getClass(): string
44
    {
45
        return Course::class;
46
    }
47
48
    /**
49
     * @return ConstraintInterface[]
50
     */
51
    public function getConstraints(): array
52
    {
53
        return [new UniqueModelConstraint($this->resolver, ['name'])];
54
    }
55
56
    /**
57
     * @return PropertyMappingInterface[]
58
     */
59
    public function getPropertyMappings(): array
60
    {
61
        return [
62
            new PropertyMapping('name', [new NotNullConstraint(), new NotBlankConstraint()]),
63
            new PropertyMapping('level', [
64
                new NotNullConstraint(),
65
                new ChoiceConstraint(
66
                    ChoiceConstraint::TYPE_INT,
67
                    Course::LEVEL
68
                ),
69
            ]),
70
            new PropertyMapping('progress', [
71
                new NotNullConstraint(),
72
                new NumericConstraint(),
73
                new NumericRangeConstraint(0, 1),
74
            ]),
75
            new PropertyMapping('active', [new NotNullConstraint(), new NotBlankConstraint()]),
76
            new PropertyMapping('documents', [
77
                new ModelCollectionConstraint(),
78
                new CallbackConstraint(function (string $path, $collection) {
79
                    if (!$collection instanceof ModelCollectionInterface) {
80
                        return [];
81
                    }
82
83
                    $names = [];
84
                    foreach ($collection->getModels() as $document) {
85
                        /** @var Document $document */
86
                        if (isset($names[$document->getName()])) {
87
                            return [
88
                                new Error(
89
                                    $path.'[_all]',
90
                                    'constraint.uniquemodel.notunique',
91
                                     ['uniqueProperties' => 'name']
92
                                ),
93
                            ];
94
                        }
95
96
                        $names[$document->getName()] = true;
97
                    }
98
99
                    return [];
100
                }),
101
            ]),
102
        ];
103
    }
104
}
105