Completed
Push — v7 ( a1649b...61f560 )
by Georges
11s
created

EventManager::dispatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache;
17
18
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
19
20
/**
21
 * Class CacheManager
22
 * @package phpFastCache
23
 *
24
 * == ItemPool Events ==
25
 * @method Void onCacheGetItem() onCacheGetItem(Callable $callable)
26
 * @method Void onCacheDeleteItem() onCacheDeleteItem(Callable $callable)
27
 * @method Void onCacheSaveItem() onCacheSaveItem(Callable $callable)
28
 * @method Void onCacheSaveDeferredItem() onCacheSaveDeferredItem(Callable $callable)
29
 * @method Void onCacheCommitItem() onCacheCommitItem(Callable $callable)
30
 * @method Void onCacheClearItem() onCacheClearItem(Callable $callable)
31
 * @method Void onCacheWriteFileOnDisk() onCacheWriteFileOnDisk(Callable $callable)
32
 * @method Void onCacheGetItemInSlamBatch() onCacheGetItemInSlamBatch(Callable $callable)
33
 *
34
 * == Item Events ==
35
 * @method Void onCacheItemSet() onCacheItemSet(Callable $callable)
36
 * @method Void onCacheItemExpireAt() onCacheItemExpireAt(Callable $callable)
37
 * @method Void onCacheItemExpireAfter() onCacheItemExpireAfter(Callable $callable)
38
 *
39
 *
40
 */
41
class EventManager
42
{
43
    /**
44
     * @var $this
45
     */
46
    protected static $instance;
47
48
    /**
49
     * @var array
50
     */
51
    protected $events = [];
52
53
    /**
54
     * @return \Phpfastcache\EventManager
55
     */
56
    public static function getInstance(): self
57
    {
58
        return (self::$instance ?: self::$instance = new self);
59
    }
60
61
    /**
62
     * EventManager constructor.
63
     */
64
    protected function __construct()
65
    {
66
    }
67
68
    /**
69
     * @param string $eventName
70
     * @param array ...$args
71
     */
72
    public function dispatch(string $eventName, ...$args)
73
    {
74
        /**
75
         * Replace array_key_exists by isset
76
         * due to performance issue on huge
77
         * loop dispatching operations
78
         */
79
        if (isset($this->events[ $eventName ])) {
80
            foreach ($this->events[ $eventName ] as $event) {
81
                call_user_func_array($event, $args);
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param array $arguments
89
     * @throws PhpfastcacheInvalidArgumentException
90
     * @throws \BadMethodCallException
91
     */
92
    public function __call(string $name, array $arguments)
93
    {
94
        if (\strpos($name, 'on') === 0) {
95
            $name = \substr($name, 2);
96
            if (\is_callable($arguments[ 0 ])) {
97
                if (isset($arguments[ 1 ]) && \is_string($arguments[ 0 ])) {
98
                    $this->events[ $name ][ $arguments[ 1 ] ] = $arguments[ 0 ];
99
                } else {
100
                    $this->events[ $name ][] = $arguments[ 0 ];
101
                }
102
            } else {
103
                throw new PhpfastcacheInvalidArgumentException(\sprintf('Expected Callable, got "%s"', \gettype($arguments[ 0 ])));
104
            }
105
        } else {
106
            throw new \BadMethodCallException('An event must start with "on" such as "onCacheGetItem"');
107
        }
108
    }
109
110
    /**
111
     * @param string $eventName
112
     * @param string $callbackName
113
     * @return bool
114
     */
115
    public function unbindEventCallback(string $eventName, string $callbackName): bool
116
    {
117
        $return = isset($this->events[ $eventName ][ $callbackName ]);
118
        unset($this->events[ $eventName ][ $callbackName ]);
119
120
        return $return;
121
    }
122
}
123