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

RangeConstraintBuilder::buildConstraint()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 19
Code Lines 11

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 6
loc 19
rs 7.2765
ccs 0
cts 16
cp 0
cc 10
eloc 11
nc 17
nop 4
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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