Completed
Push — master ( 034dff...628390 )
by
unknown
10:02
created

FormValidationException::getFields()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MediaMonks\RestApi\Exception;
4
5
use MediaMonks\RestApi\Response\Error;
6
use MediaMonks\RestApi\Util\StringUtil;
7
use Symfony\Component\Form\FormError;
8
use Symfony\Component\Form\FormInterface;
9
10
class FormValidationException extends AbstractValidationException
11
{
12
    const FIELD_ROOT = '#';
13
14
    /**
15
     * @var FormInterface
16
     */
17
    protected $form;
18
19
    /**
20
     * @param FormInterface $form
21
     * @param string $message
22
     * @param string $code
23
     */
24 3
    public function __construct(
25
        FormInterface $form,
26
        $message = Error::MESSAGE_FORM_VALIDATION,
27
        $code = Error::CODE_FORM_VALIDATION
28
    ) {
29 3
        $this->form = $form;
30 3
        parent::__construct($message, $code);
31 3
    }
32
33
    /**
34
     * @return array
35
     */
36 3
    public function getFields()
37
    {
38 3
        return $this->getErrorMessages($this->form);
39
    }
40
41
    /**
42
     * @param FormInterface $form
43
     * @return array
44
     */
45 3
    protected function getErrorMessages(FormInterface $form)
46
    {
47 3
        $errors = [];
48 3
        foreach ($this->getFormErrorMessages($form) as $error) {
49 1
            $errors[] = $error;
50 3
        }
51 3
        foreach ($this->getFormChildErrorMessages($form) as $error) {
52 1
            $errors[] = $error;
53 3
        }
54
55 3
        return $errors;
56
    }
57
58
    /**
59
     * @param FormInterface $form
60
     * @return array
61
     */
62 3
    protected function getFormErrorMessages(FormInterface $form)
63
    {
64 3
        $errors = [];
65 3
        foreach ($form->getErrors() as $error) {
66 1
            if ($form->isRoot()) {
67 1
                $errors[] = $this->toErrorArray($error);
68 1
            } else {
69 1
                $errors[] = $this->toErrorArray($error, $form);
70
            }
71 3
        }
72
73 3
        return $errors;
74
    }
75
76
    /**
77
     * @param FormInterface $form
78
     * @return array
79
     */
80 3
    protected function getFormChildErrorMessages(FormInterface $form)
81
    {
82 3
        $errors = [];
83 3
        foreach ($form->all() as $child) {
84 1
            if ($this->shouldAddChildErrorMessage($child)) {
85 1
                foreach ($this->getErrorMessages($child) as $error) {
86 1
                    $errors[] = $error;
87 1
                }
88 1
            }
89 3
        }
90
91 3
        return $errors;
92
    }
93
94
    /**
95
     * @param FormInterface|null $child
96
     * @return bool
97
     */
98 1
    protected function shouldAddChildErrorMessage(FormInterface $child = null)
99
    {
100 1
        return !empty($child) && !$child->isValid();
101
    }
102
103
    /**
104
     * @param FormError $error
105
     * @param FormInterface|null $form
106
     * @return ErrorField
107
     */
108 1
    protected function toErrorArray(FormError $error, FormInterface $form = null)
109
    {
110 1
        if (is_null($form)) {
111 1
            $field = self::FIELD_ROOT;
112 1
        } else {
113 1
            $field = $form->getName();
114
        }
115 1
        if (!is_null($error->getCause()) && !is_null($error->getCause()->getConstraint())) {
116
            $code = $this->getErrorCode(StringUtil::classToSnakeCase($error->getCause()->getConstraint()));
117
        } else {
118 1
            $code = $this->getErrorCodeByMessage($error);
119
        }
120
121 1
        return new ErrorField($field, $code, $error->getMessage());
122
    }
123
124
    /**
125
     * @param FormError $error
126
     * @return string
127
     */
128 1
    protected function getErrorCodeByMessage(FormError $error)
129
    {
130 1
        if (stristr($error->getMessage(), Error::FORM_TYPE_CSRF)) {
131
            return $this->getErrorCode(Error::FORM_TYPE_CSRF);
132
        }
133
134 1
        return $this->getErrorCode(Error::FORM_TYPE_GENERAL);
135
    }
136
137
    /**
138
     * @param string $value
139
     * @return string
140
     */
141 1
    protected function getErrorCode($value)
142
    {
143 1
        return sprintf(Error::CODE_FORM_VALIDATION.'.%s', $value);
144
    }
145
}
146