CanValidate::validateEntity()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
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 6
    protected function validateEntity($entity)
15
    {
16 6
        $errors = Validation::createValidatorBuilder()
17 6
            ->enableAnnotationMapping()
18 6
            ->getValidator()
19 6
            ->validate($entity);
20
21 6
        if ($errors->count() > 0) {
22 1
            throw RestException::createFromConstraintViolationList($errors);
23
        }
24
25 5
        return $entity;
26
    }
27
}
28