Passed
Branch main (f9aaf7)
by Jonathan
14:43
created

AbstractHookCollector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hooks() 0 3 1
A __construct() 0 4 1
A register() 0 6 2
A module() 0 3 1
A name() 0 4 1
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Hooks
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2011-2021, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Common\Hooks;
16
17
use Fisharebest\Webtrees\Module\ModuleInterface;
18
use Illuminate\Support\Collection;
19
use MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface;
20
use MyArtJaub\Webtrees\Contracts\Hooks\HookInterface;
21
use ReflectionClass;
22
23
/**
24
 * Abstract calss for hooks collectors.
25
 *
26
 * @template THook of HookInterface
27
 * @implements HookCollectorInterface<THook>
28
 */
29
abstract class AbstractHookCollector implements HookCollectorInterface, HookInterface
30
{
31
    protected Collection $hooks;
32
    private ModuleInterface $module;
33
34
    /**
35
     * Constructor for AbstractHookCollector
36
     *
37
     * @param ModuleInterface $module
38
     */
39
    public function __construct(ModuleInterface $module)
40
    {
41
        $this->hooks = new Collection();
42
        $this->module = $module;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module()
48
     */
49
    public function module(): ModuleInterface
50
    {
51
        return $this->module;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name()
57
     */
58
    public function name(): string
59
    {
60
        return $this->module->name() . '-' .
61
            mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title()
67
     */
68
    abstract public function title(): string;
69
70
    /**
71
     * {@inheritDoc}
72
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description()
73
     */
74
    abstract public function description(): string;
75
76
    /**
77
     * {@inheritDoc}
78
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface()
79
     */
80
    abstract public function hookInterface(): string;
81
82
    /**
83
     * {@inheritDoc}
84
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register()
85
     */
86
    public function register(HookInterface $hook_instance, int $order): void
87
    {
88
        if ($this->hooks->has($order)) {
89
            $this->hooks->splice($order + 1, 0, [$hook_instance]);
90
        } else {
91
            $this->hooks->put($order, $hook_instance);
92
        }
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks()
98
     */
99
    public function hooks(): Collection
100
    {
101
        return $this->hooks->sortKeys();
102
    }
103
}
104