Passed
Pull Request — master (#48)
by Arman
03:56
created

HookManager::hasImplementer()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 10
nop 1
dl 0
loc 26
rs 9.2222
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A HookManager::handleHook() 0 3 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Hooks;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Di\Di;
19
20
/**
21
 * Class HookManager
22
 * @package Quantum\Hooks
23
 */
24
class HookManager
25
{
26
27
    /**
28
     * @var \Quantum\Hooks\HookManager|null
29
     */
30
    private static $instance = null;
31
32
    /**
33
     * @return \Quantum\Hooks\HookManager|null
34
     */
35
    public static function getInstance(): ?HookManager
36
    {
37
        if (self::$instance == null) {
38
            self::$instance = new self();
39
        }
40
41
        return self::$instance;
42
    }
43
44
    /**
45
     * @param string $hookName
46
     * @throws \Quantum\Exceptions\DiException
47
     * @throws \ReflectionException
48
     */
49
    public function __invoke(string $hookName)
50
    {
51
        $fs = Di::get(FileSystem::class);
52
53
        if ($fs->exists(hooks_dir() . DS . $hookName . '.php')) {
54
            $className = '\\Hooks\\' . ucfirst($hookName);
55
56
            if (class_exists($className, true)) {
57
                $this->handleHook(new $className);
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param \Quantum\Hooks\HookInterface $hook
64
     */
65
    private function handleHook(HookInterface $hook)
66
    {
67
        $hook->apply();
68
    }
69
70
}
71