Completed
Push — master ( ec8ea2...b05dbd )
by Robert
04:41
created

FormValidationException::shouldAddChildErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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