ValidationException::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 29
cp 0
rs 8.439
cc 6
eloc 22
nc 2
nop 5
crap 42
1
<?php
2
3
namespace Majora\Framework\Validation;
4
5
use Majora\Framework\Model\CollectionableInterface;
6
use Majora\Framework\Model\EntityCollection;
7
use Symfony\Component\Form\FormErrorIterator;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
10
/**
11
 * Custom exception class for validation exceptions.
12
 */
13
class ValidationException extends \InvalidArgumentException
14
{
15
    /**
16
     * @var object
17
     */
18
    protected $entity;
19
20
    /**
21
     * @var FormErrorIterator|ConstraintViolationListInterface
22
     */
23
    protected $report;
24
25
    /**
26
     * @var array
27
     */
28
    protected $groups;
29
30
    /**
31
     * construct.
32
     *
33
     * @param object                                             $entity
34
     * @param FormErrorIterator|ConstraintViolationListInterface $report
35
     * @param array                                              $groups
36
     * @param int                                                $code
37
     * @param \Exception                                         $previous
38
     */
39
    public function __construct(
40
        $entity = null,
41
        $report = null,
42
        array $groups = null,
43
        $code     = null,
44
        $previous = null
45
    ) {
46
        if (!empty($entity) && !is_object($entity)) {
47
            throw new \InvalidArgumentException('Cannot create a ValidationException from a plain value.');
48
        }
49
50
        $this->entity = $entity;
51
        $this->groups = $groups;
0 ignored issues
show
Documentation Bug introduced by
It seems like $groups can be null. However, the property $groups is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
52
        $this->report = $report;
53
54
        parent::__construct(
55
            trim(sprintf(
56
                'Validation failed on %s%s',
57
                $this->entity ? get_class($this->entity) : '',
58
                $this->entity instanceof CollectionableInterface ?
59
                    sprintf('#%s', $this->entity->getId()) :
60
                    ''
61
                ,
62
                empty($this->groups) ? '' : sprintf(' for ["%s"] groups',
63
                    implode('", "', $this->groups)
64
                )
65
            )),
66
            $code,
67
            $previous
68
        );
69
    }
70
71
    /**
72
     * return failed entity
73
     *
74
     * @return object
75
     */
76
    public function getEntity()
77
    {
78
        return $this->entity;
79
    }
80
81
    /**
82
     * return validation groups which are failing
83
     *
84
     * @return array
85
     */
86
    public function getGroups()
87
    {
88
        return $this->groups;
89
    }
90
91
    /**
92
     * return violation list report
93
     *
94
     * @return EntityCollection|FormErrorIterator|ConstraintViolationListInterface
95
     */
96
    public function getReport()
97
    {
98
        return $this->report ?
99
            $this->report :
100
            new EntityCollection()
101
        ;
102
    }
103
104
    /**
105
     * format and return report (translatable) messages
106
     *
107
     * @return array
108
     */
109
    public function formatReport()
110
    {
111
        $messages = array();
112
        $report = $this->getReport();
113
114
        switch(true){
115
116
            case $report instanceof ConstraintViolationListInterface:
117
                foreach ($report as $constraintViolation){
118
                    $messages[] = $constraintViolation->getMessage();
119
                }
120
                break;
121
122
            case $report instanceof FormErrorIterator:
123
                foreach ($report as $formError) {
124
                    $messages[] = $formError->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not seem to exist on object<Symfony\Component\Form\FormErrorIterator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
                }
126
                break;
127
128
            default:
129
                $messages[] = $this->getMessage();
130
131
        }
132
133
        return $messages;
134
    }
135
}
136