AbstractBaseService::dispatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Service\Base;
15
16
use Symfony\Component\EventDispatcher\Event;
17
use Symfony\Component\EventDispatcher\EventDispatcher;
18
use Thelia\Core\Event\ActionEvent;
19
20
/**
21
 * Class BaseService
22
 * @package Dealer\Service\Base
23
 */
24
abstract class AbstractBaseService
25
{
26
    const EVENT_CREATE = null;
27
    const EVENT_CREATE_BEFORE = null;
28
    const EVENT_CREATE_AFTER = null;
29
    const EVENT_UPDATE = null;
30
    const EVENT_UPDATE_BEFORE = null;
31
    const EVENT_UPDATE_AFTER = null;
32
    const EVENT_DELETE = null;
33
    const EVENT_DELETE_BEFORE = null;
34
    const EVENT_DELETE_AFTER = null;
35
36
    protected $dispatcher;
37
38
    //CREATION
39
40
    /**
41
     * Allow to proccess Creation Even Sender
42
     * @param Event $event
43
     */
44
    protected function create(Event $event)
45
    {
46
        $this->dispatch(static::EVENT_CREATE_BEFORE, $event);
47
        if (!$event->isPropagationStopped()) {
48
            $this->createProcess($event);
49
            if (!$event->isPropagationStopped()) {
50
                $this->dispatch(static::EVENT_CREATE_AFTER, $event);
51
            }
52
        }
53
    }
54
55
    /**
56
     * Allow to create an object from an Event
57
     * @param Event $event
58
     */
59
    protected function createProcess(Event $event)
60
    {
61
        $this->dispatch(static::EVENT_CREATE, $event);
62
    }
63
64
    // UPDATE
65
66
    /**
67
     * Allow to send Update Events
68
     * @param Event $event
69
     */
70
    protected function update(Event $event)
71
    {
72
        $this->dispatch(static::EVENT_UPDATE_BEFORE, $event);
73
        if (!$event->isPropagationStopped()) {
74
            $this->updateProcess($event);
75
            if (!$event->isPropagationStopped()) {
76
                $this->dispatch(static::EVENT_UPDATE_AFTER, $event);
77
            }
78
        }
79
    }
80
81
82
    /**
83
     * Allow to update an object from an event
84
     * @param Event $event
85
     */
86
    protected function updateProcess(Event $event)
87
    {
88
        $this->dispatch(static::EVENT_UPDATE, $event);
89
    }
90
91
    // DELETE
92
93
    protected function delete(Event $event)
94
    {
95
        $this->dispatch(static::EVENT_DELETE_BEFORE, $event);
96
        if (!$event->isPropagationStopped()) {
97
            $this->deleteProcess($event);
98
            if (!$event->isPropagationStopped()) {
99
                $this->dispatch(static::EVENT_DELETE_AFTER, $event);
100
            }
101
        }
102
    }
103
104
    protected function deleteProcess(Event $event)
105
    {
106
        $this->dispatch(static::EVENT_DELETE, $event);
107
    }
108
109
110
    // DEPENDENCIES
111
112
    /**
113
     * Dispatch a Thelia event
114
     *
115
     * @param string $eventName a TheliaEvent name, as defined in TheliaEvents class
116
     * @param ActionEvent $event the action event, or null (a DefaultActionEvent will be dispatched)
117
     */
118
    protected function dispatch($eventName, Event $event = null)
119
    {
120
        if ($event == null) {
121
            $event = new DefaultActionEvent();
122
        }
123
124
        $this->getDispatcher()->dispatch($eventName, $event);
125
    }
126
127
    /**
128
     * Return the event dispatcher,
129
     *
130
     * @return \Symfony\Component\EventDispatcher\EventDispatcher
131
     */
132
    public function getDispatcher()
133
    {
134
        return $this->dispatcher;
135
    }
136
137
    public function setDispatcher(EventDispatcher $dispatcher)
138
    {
139
        $this->dispatcher = $dispatcher;
140
    }
141
}
142