DalActionTrait::setEventDispatcher()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Majora\Framework\Domain\Action\Dal;
4
5
use Majora\Framework\Api\Client\RestApiClient;
6
use Majora\Framework\Validation\ValidationException;
7
use Symfony\Component\EventDispatcher\Event;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\Serializer\SerializerInterface;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
/**
13
 * Base trait for Dal actions
14
 */
15
trait DalActionTrait
16
{
17
    /**
18
     * @var ValidatorInterface
19
     */
20
    protected $validator;
21
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    protected $eventDispatcher;
26
27
    /**
28
     * define event dispatcher.
29
     *
30
     * @param EventDispatcherInterface $eventDispatcher
31
     */
32
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
33
    {
34
        $this->eventDispatcher = $eventDispatcher;
35
    }
36
37
    /**
38
     * define validator.
39
     *
40
     * @param ValidatorInterface $validator
41
     */
42
    public function setValidator(ValidatorInterface $validator)
43
    {
44
        $this->validator = $validator;
45
    }
46
47
    /**
48
     * assert given entity is valid on given scope.
49
     *
50
     * @param object       $entity
51
     * @param string|array $scope
52
     *
53
     * @throws ValidationException If given object is invalid on given scope
54
     */
55
    protected function assertEntityIsValid($entity, $scope = null)
56
    {
57
        if (!$this->validator) {
58
            throw new \BadMethodCallException(sprintf(
59
                'Method %s() cannot be used while validator is not configured.',
60
                __METHOD__
61
            ));
62
        }
63
64
        $scopes = $scope ? (array) $scope : null;
65
66
        $violationList = $this->validator->validate(
67
            $entity,
68
            null,
69
            $scopes
70
        );
71
72
        if (!count($violationList)) {
73
            return;
74
        }
75
76
        throw new ValidationException($entity, $violationList, $scopes);
77
    }
78
79
    /**
80
     * fire given event.
81
     *
82
     * @param string $eventName
83
     * @param Event  $event
84
     *
85
     * @throws \BadMethodCallException If any event dispatcher set
86
     */
87
    protected function fireEvent($eventName, Event $event)
88
    {
89
        if (!$this->eventDispatcher) {
90
            throw new \BadMethodCallException(sprintf(
91
                'Method %s() cannot be used while event dispatcher is not configured.',
92
                __METHOD__
93
            ));
94
        }
95
96
        $this->eventDispatcher->dispatch($eventName, $event);
97
    }
98
99
    /**
100
     * @see NormalizableInterface::getScopes()
101
     */
102
    public static function getScopes()
103
    {
104
        return array();
105
    }
106
107
    /**
108
     * @see NormalizableInterface::normalize()
109
     */
110
    public function normalize($scope = 'default')
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        return array();
113
    }
114
115
    /**
116
     * @see NormalizableInterface::denormalize()
117
     */
118
    public function denormalize(array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        return $this;
121
    }
122
}
123