Completed
Push — master ( 665986...1514e7 )
by Guilh
08:09 queued 05:04
created

InvalidParameterException::getViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Exception;
13
14
use FOS\RestBundle\Controller\Annotations\ParamInterface;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Validator\ConstraintViolationListInterface;
17
18
class InvalidParameterException extends BadRequestHttpException
19
{
20
    private $parameter;
21
    private $violations;
22
23 1
    public function getParameter()
24
    {
25 1
        return $this->parameter;
26
    }
27
28 1
    public function getViolations()
29
    {
30 1
        return $this->violations;
31
    }
32
33 2
    public static function withViolations(ParamInterface $parameter, ConstraintViolationListInterface $violations)
34
    {
35 2
        $message = '';
36
37 2
        foreach ($violations as $key => $violation) {
38 1
            if ($key > 0) {
39 1
                $message .= "\n";
40 1
            }
41
42 1
            $message .= sprintf(
43 1
                'Parameter "%s" of value "%s" violated a constraint "%s"',
44 1
                $parameter->getName(),
45 1
                $violation->getInvalidValue(),
46 1
                $violation->getMessage()
47 1
            );
48 2
        }
49
50 2
        return self::withViolationsAndMessage($parameter, $violations, $message);
51
    }
52
53
    /**
54
     * Do not use this method. It will be removed in 2.0.
55
     *
56
     * @param ParamInterface                   $parameter
57
     * @param ConstraintViolationListInterface $violations
58
     * @param string                           $message
59
     *
60
     * @return self
61
     *
62
     * @internal
63
     */
64 2
    public static function withViolationsAndMessage(ParamInterface $parameter, ConstraintViolationListInterface $violations, $message)
65
    {
66 2
        $exception = new self($message);
67 2
        $exception->parameter = $parameter;
68 2
        $exception->violations = $violations;
69
70 2
        return $exception;
71
    }
72
}
73