Completed
Push — master ( 3e5bb1...8eabd5 )
by Guilh
06:59
created

FileParam::getConstraints()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 8.5906
cc 5
eloc 13
nc 16
nop 0
crap 5
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 Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\Constraints;
17
use Symfony\Component\Validator\Constraints\All;
18
19
/**
20
 * Represents a file that must be present.
21
 *
22
 * @Annotation
23
 * @Target("METHOD")
24
 *
25
 * @author Ener-Getick <[email protected]>
26
 */
27
class FileParam extends AbstractParam
28
{
29
    /** @var bool */
30
    public $strict = true;
31
    /** @var mixed */
32
    public $requirements = null;
33
    /** @var bool */
34
    public $image = false;
35
    /** @var bool */
36
    public $map = false;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 9
    public function getConstraints()
42
    {
43 9
        $constraints = parent::getConstraints();
44 9
        if ($this->requirements instanceof Constraint) {
45 1
            $constraints[] = $this->requirements;
46 1
        }
47
48 9
        $options = is_array($this->requirements) ? $this->requirements : array();
49 9
        if ($this->image) {
50 4
            $constraints[] = new Constraints\Image($options);
51 4
        } else {
52 5
            $constraints[] = new Constraints\File($options);
53
        }
54
55
        // If the user wants to map the value
56 9
        if ($this->map) {
57
            $constraints = array(
58 5
                new All(array('constraints' => $constraints)),
59 5
            );
60 5
        }
61
62 9
        return $constraints;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 6
    public function getValue(Request $request, $default = null)
69
    {
70 6
        return $request->files->get($this->getKey(), $default);
71
    }
72
}
73