Passed
Push — master ( d1b72a...68b5a8 )
by Anatoly
01:04 queued 11s
created

InvalidEntityException::getEntityViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 1
b 0
f 0
cc 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace App\Exception;
4
5
/**
6
 * Import classes
7
 */
8
use Sunrise\Http\Router\Exception\BadRequestException;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
/**
12
 * InvalidEntityException
13
 */
14
final class InvalidEntityException extends BadRequestException
15
{
16
17
    /**
18
     * Throws the exception if the given entity isn't valid
19
     *
20
     * @param object $entity
21
     * @param ValidatorInterface $validator
22
     *
23
     * @return void
24
     * @throws self
25
     */
26 12
    public static function assert(object $entity, ValidatorInterface $validator) : void
27
    {
28 12
        $violations = $validator->validate($entity);
29 12
        if (0 === $violations->count()) {
30 10
            return;
31
        }
32
33 6
        throw new self('Invalid Entity', [
34 6
            'violations' => $violations->getIterator()->getArrayCopy(),
35
        ]);
36
    }
37
}
38