Completed
Push — feature/other-validation ( c4ebdf...a3189f )
by Narcotic
152:45 queued 146:40
created

ConstraintBuilder::addConstraints()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 8.8571
c 1
b 0
f 1
cc 5
eloc 9
nc 5
nop 3
crap 5
1
<?php
2
/**
3
 * ConstraintBuilder class file
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint;
7
8
use Graviton\RestBundle\Model\DocumentModel;
9
use Graviton\SchemaBundle\Constraint\Builder\ConstraintBuilderInterface;
10
use Graviton\SchemaBundle\Document\Schema;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class ConstraintBuilder
18
{
19
    /**
20
     * @var ConstraintBuilderInterface[]
21
     */
22
    private $builders = [];
23
24
    /**
25
     * Add constraint builder
26
     *
27
     * @param ConstraintBuilderInterface $builder Constraint builder
28
     *
29
     * @return void
30
     */
31 8
    public function addConstraintBuilder(ConstraintBuilderInterface $builder)
32
    {
33 8
        $this->builders[] = $builder;
34 8
    }
35
36
    /**
37
     * Go through the constraints and call the builders to do their job
38
     *
39
     * @param string        $fieldName field name
40
     * @param Schema        $property  the property
41
     * @param DocumentModel $model     the parent model
42
     *
43
     * @return Schema
44
     */
45 4
    public function addConstraints($fieldName, Schema $property, DocumentModel $model)
46
    {
47 4
        $constraints = $model->getConstraints($fieldName);
48
49 4
        if (!is_array($constraints)) {
50 2
            return $property;
51
        }
52
53 2
        foreach ($constraints as $constraint) {
54 2
            foreach ($this->builders as $builder) {
55 2
                if ($builder->supportsConstraint($constraint->name, $constraint->options)) {
56 2
                    $property = $builder->buildConstraint($fieldName, $property, $model, $constraint->options);
57 1
                }
58 1
            }
59 1
        }
60
61 2
        return $property;
62
    }
63
}
64