AbstractHookCollector::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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-2022, 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
 */
28
abstract class AbstractHookCollector implements HookCollectorInterface, HookInterface
29
{
30
    /** @var Collection<int, array<THook>> $hooks */
31
    protected Collection $hooks;
32
33
    private ModuleInterface $module;
34
35
    /**
36
     * Constructor for AbstractHookCollector
37
     *
38
     * @param ModuleInterface $module
39
     */
40
    public function __construct(ModuleInterface $module)
41
    {
42
        $this->hooks = new Collection();
43
        $this->module = $module;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module()
49
     */
50
    public function module(): ModuleInterface
51
    {
52
        return $this->module;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::name()
58
     */
59
    public function name(): string
60
    {
61
        return $this->module->name() . '-' .
62
            mb_substr(str_replace('collector', '', mb_strtolower((new ReflectionClass($this))->getShortName())), 0, 64);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::title()
68
     */
69
    abstract public function title(): string;
70
71
    /**
72
     * {@inheritDoc}
73
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::description()
74
     */
75
    abstract public function description(): string;
76
77
    /**
78
     * {@inheritDoc}
79
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hookInterface()
80
     */
81
    abstract public function hookInterface(): string;
82
83
    /**
84
     * {@inheritDoc}
85
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::register()
86
     */
87
    public function register(HookInterface $hook_instance, int $order): void
88
    {
89
        $this->hooks->put($order, array_merge($this->hooks->get($order, []), [$hook_instance]));
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface::hooks()
95
     *
96
     * @return Collection<THook>
97
     */
98
    public function hooks(): Collection
99
    {
100
        /** @var Collection<THook> */
101
        return $this->hooks->sortKeys()->flatten();
102
    }
103
}
104