Completed
Push — master ( 92a758...e31339 )
by
unknown
06:27
created

AbstractScalarParam   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 60
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getConstraints() 0 47 12
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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
namespace FOS\RestBundle\Controller\Annotations;
13
14
use FOS\RestBundle\Validator\Constraints\Regex;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\Constraints\NotBlank;
17
use Symfony\Component\Validator\Constraints\All;
18
use Symfony\Component\Validator\Constraints\NotNull;
19
20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author Ener-Getick <[email protected]>
24
 */
25
abstract class AbstractScalarParam extends AbstractParam
26
{
27
    /** @var mixed */
28
    public $requirements = null;
29
30
    /** @var bool */
31
    public $map = false;
32
33
    /** @var bool */
34 11
    public $allowBlank = true;
35
36 11
    /** {@inheritdoc} */
37
    public function getConstraints()
38 11
    {
39 4
        $constraints = parent::getConstraints();
40 11
41 5
        if ($this->requirements instanceof Constraint) {
42 5
            $constraints[] = $this->requirements;
43 5
        } elseif (is_scalar($this->requirements)) {
44 5
            $constraints[] = new Regex(array(
45 5
                'pattern' => '#^(?:'.$this->requirements.')$#xsu',
46 5
                'message' => sprintf(
47 5
                    'Parameter \'%s\' value, does not match requirements \'%s\'',
48 5
                    $this->getName(),
49 10
                    $this->requirements
50 1
                ),
51 1
            ));
52 1
        } elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
53 1
            $constraints[] = new Regex(array(
54 1
                'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
55
                'message' => $this->requirements['error_message'],
56 11
            ));
57 1
        } elseif (is_array($this->requirements)) {
58 1
            foreach ($this->requirements as $requirement) {
59
                if ($requirement instanceof Constraint) {
60
                    $constraints[] = $requirement;
61
                } else {
62 11
                    @trigger_error('Using an array not only containing `Constraint`s as requirements is deprecated since version 2.6.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
                }
64 4
            }
65 4
        }
66 4
67
        if (false === $this->allowBlank) {
68 11
            $constraints[] = new NotBlank();
69
        }
70
71
        // If the user wants to map the value, apply all constraints to every
72
        // value of the map
73
        if ($this->map) {
74
            $constraints = array(
75
                new All(array('constraints' => $constraints)),
76
            );
77
            if (false === $this->nullable) {
78
                $constraints[] = new NotNull();
79
            }
80
        }
81
82
        return $constraints;
83
    }
84
}
85