Passed
Push — master ( adcfba...df27c1 )
by Pavel
01:38
created

CanValidate::validateEntity()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Traits;
2
3
use Pz\Doctrine\Rest\Exceptions\RestException;
4
use Symfony\Component\Validator\Validation;
5
6
trait CanValidate
7
{
8
    /**
9
     * @param object $entity
10
     *
11
     * @return object
12
     * @throws RestException
13
     */
14 4
    protected function validateEntity($entity)
15
    {
16 4
        $errors = Validation::createValidatorBuilder()
17 4
            ->enableAnnotationMapping()
18 4
            ->getValidator()
19 4
            ->validate($entity);
20
21 4
        if ($errors->count() > 0) {
22 1
            throw RestException::createFromConstraintViolationList($errors);
23
        }
24
25 3
        return $entity;
26
    }
27
}
28