Completed
Push — master ( c00bbf...06e52a )
by Pavel
17:08 queued 22s
created

JmsValidatorSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 33
ccs 12
cts 22
cp 0.5455
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A __construct() 0 4 1
A onPostDeserialize() 0 16 3
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
6
use JMS\Serializer\EventDispatcher\ObjectEvent;
7
use ScayTrase\Api\Cruds\Exception\EntityProcessingException;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
final class JmsValidatorSubscriber implements EventSubscriberInterface
11
{
12
    private $validator;
13
14 2
    public function __construct(ValidatorInterface $validator)
15
    {
16 2
        $this->validator = $validator;
17 2
    }
18
19 15
    public static function getSubscribedEvents()
20
    {
21
        return [
22 15
            ['event' => 'serializer.post_deserialize', 'method' => 'onPostDeserialize'],
23
        ];
24
    }
25
26 2
    public function onPostDeserialize(ObjectEvent $event)
27
    {
28 2
        $context = $event->getContext();
29
30 2
        if ($context->getDepth() > 0) {
31
            return;
32
        }
33
34 2
        $groups = $context->attributes->get('groups')->getOrElse(null);
35
36 2
        $list = $this->validator->validate($event->getObject(), $groups);
37
38 2
        if ($list->count() > 0) {
39
            throw EntityProcessingException::fromViolationList($list);
40
        }
41 2
    }
42
}
43