Completed
Push — master ( 61a9ce...9b36d9 )
by
unknown
03:37
created

FormValidationException::getFormErrorMessages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.7898

Importance

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