Completed
Push — master ( 46bd6f...59ea22 )
by Siim
11:25
created

convertViolationsToErrormessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 7.02.19
6
 * Time: 9:55
7
 */
8
9
namespace Sf4\Api\Notification\Traits;
10
11
use Sf4\Api\Notification\BaseErrorMessage;
12
use Sf4\Api\Notification\NotificationInterface;
13
use Symfony\Component\Validator\ConstraintViolationInterface;
14
use Symfony\Component\Validator\ConstraintViolationListInterface;
15
16
trait ConvertViolationsToErrorMessage
17
{
18
19
    /**
20
     * @param ConstraintViolationListInterface $violationList
21
     * @param NotificationInterface $notification
22
     * @param string $fieldName
23
     */
24
    protected function convertViolationsToErrormessage(
25
        ConstraintViolationListInterface $violationList,
26
        NotificationInterface $notification,
27
        string $fieldName
28
    ) {
29
        if (0 !== count($violationList)) {
30
            /** @var ConstraintViolationInterface $violation */
31
            foreach ($violationList as $violation) {
32
                $errorMessage = new BaseErrorMessage();
33
                $errorMessage->setKey($fieldName);
34
                $errorMessage->setMessage($violation->getMessage());
35
                $notification->addMessage($errorMessage);
36
            }
37
        }
38
    }
39
}
40