Completed
Branch master (6d30bf)
by Philip
13:30
created

EntityEvents   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 79
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldExecute() 0 13 4
A push() 0 6 2
A pop() 0 7 2
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 34
    public function shouldExecute(Entity $entity, $moment, $action)
41
    {
42 34
        if (!isset($this->events[$moment.'.'.$action])) {
43 33
            return true;
44
        }
45 9
        foreach ($this->events[$moment.'.'.$action] as $event) {
46 9
            $result = $event($entity);
47 9
            if (!$result) {
48 9
                return false;
49
            }
50
        }
51 8
        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 10
    public function push($moment, $action, \Closure $function)
71
    {
72 10
        $events                            = isset($this->events[$moment.'.'.$action]) ? $this->events[$moment.'.'.$action] : [];
73 10
        $events[]                          = $function;
74 10
        $this->events[$moment.'.'.$action] = $events;
75 10
    }
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 9
    public function pop($moment, $action)
89
    {
90 9
        if (array_key_exists($moment.'.'.$action, $this->events)) {
91 9
            return array_pop($this->events[$moment.'.'.$action]);
92
        }
93 1
        return null;
94
    }
95
}
96