Completed
Push — master ( eae036...c36670 )
by Philip
06:28
created

EntityEvents::shouldExecuteEvents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 3
crap 4
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CRUDlex;
13
14
/**
15
 * Management and execution of events.
16
 */
17
class EntityEvents
18
{
19
20
    /**
21
     * Holds the events.
22
     * @var array
23
     */
24
    protected $events;
25
26
    /**
27
     * Executes the event chain of an entity.
28
     *
29
     * @param Entity $entity
30
     * the entity having the event chain to execute
31
     * @param string $moment
32
     * the "moment" of the event, can be either "before" or "after"
33
     * @param string $action
34
     * the "action" of the event, can be either "create", "update" or "delete"
35
     *
36
     * @return boolean
37
     * true on successful execution of the full chain or false if it broke at
38
     * any point (and stopped the execution)
39
     */
40 1 View Code Duplication
    public function shouldExecuteEvents(Entity $entity, $moment, $action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42 1
        if (!isset($this->events[$moment.'.'.$action])) {
43 1
            return true;
44
        }
45 1
        foreach ($this->events[$moment.'.'.$action] as $event) {
46 1
            $result = $event($entity);
47 1
            if (!$result) {
48 1
                return false;
49
            }
50
        }
51 1
        return true;
52
    }
53
54
    /**
55
     * Adds an event to fire for the given parameters. The event function must
56
     * have this signature:
57
     * function (Entity $entity)
58
     * and has to return true or false.
59
     * The events are executed one after another in the added order as long as
60
     * they return "true". The first event returning "false" will stop the
61
     * process.
62
     *
63
     * @param string $moment
64
     * the "moment" of the event, can be either "before" or "after"
65
     * @param string $action
66
     * the "action" of the event, can be either "create", "update" or "delete"
67
     * @param \Closure $function
68
     * the event function to be called if set
69
     */
70 2
    public function pushEvent($moment, $action, \Closure $function)
71
    {
72 2
        $events                            = isset($this->events[$moment.'.'.$action]) ? $this->events[$moment.'.'.$action] : [];
73 2
        $events[]                          = $function;
74 2
        $this->events[$moment.'.'.$action] = $events;
75 2
    }
76
77
    /**
78
     * Removes and returns the latest event for the given parameters.
79
     *
80
     * @param string $moment
81
     * the "moment" of the event, can be either "before" or "after"
82
     * @param string $action
83
     * the "action" of the event, can be either "create", "update" or "delete"
84
     *
85
     * @return \Closure|null
86
     * the popped event or null if no event was available.
87
     */
88 1 View Code Duplication
    public function popEvent($moment, $action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 1
        if (array_key_exists($moment.'.'.$action, $this->events)) {
91 1
            return array_pop($this->events[$moment.'.'.$action]);
92
        }
93 1
        return null;
94
    }
95
}