Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

RangeConstraintBuilder::supportsConstraint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
rs 9.6666
ccs 0
cts 5
cp 0
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * RangeConstraintBuilder class file
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint\Builder;
7
8
use Graviton\RestBundle\Model\DocumentModel;
9
use Graviton\SchemaBundle\Document\Schema;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class RangeConstraintBuilder implements ConstraintBuilderInterface
17
{
18
19
    /**
20
     * @var array
21
     */
22
    private $types = [
23
        'Range',
24
        'GreaterThanOrEqual',
25
        'LessThanOrEqual'
26
    ];
27
28
    /**
29
     * @var string
30
     */
31
    private $type;
32
    
33
    /**
34
     * if this builder supports a given constraint
35
     *
36
     * @param string $type    Field type
37
     * @param array  $options Options
38
     *
39
     * @return bool
40
     */
41
    public function supportsConstraint($type, array $options = [])
42
    {
43
        if (in_array($type, $this->types)) {
44
            $this->type = $type;
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * Adds constraints to the property
53
     *
54
     * @param string        $fieldName field name
55
     * @param Schema        $property  property
56
     * @param DocumentModel $model     parent model
57
     * @param array         $options   the constraint options
58
     *
59
     * @return Schema the modified property
60
     */
61
    public function buildConstraint($fieldName, Schema $property, DocumentModel $model, array $options)
62
    {
63
        foreach ($options as $option) {
64
            if ($option->name == 'min' && $this->type == 'Range') {
65
                $property->setNumericMinimum(floatval($option->value));
66
            }
67 View Code Duplication
            if ($option->name == 'max' && $this->type == 'Range') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                $property->setNumericMaximum(floatval($option->value));
69
            }
70
            if ($option->name == 'value' && $this->type == 'GreaterThanOrEqual') {
71
                $property->setNumericMinimum(floatval($option->value));
72
            }
73 View Code Duplication
            if ($option->name == 'value' && $this->type == 'LessThanOrEqual') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                $property->setNumericMaximum(floatval($option->value));
75
            }
76
        }
77
78
        return $property;
79
    }
80
}
81