BootstrapEventHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 14
c 3
b 1
f 0
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onControllerInitialize() 0 8 2
A implementedEvents() 0 5 1
A onModelInitialize() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2022 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\Placeholders\Event;
17
18
use BEdita\API\Controller\ObjectsController;
19
use BEdita\Core\Model\Table\ObjectsBaseTable;
20
use BEdita\Core\Model\Table\ObjectsTable;
21
use Cake\Event\Event;
22
use Cake\Event\EventListenerInterface;
23
24
/**
25
 * Attach placeholders behavior and component to relevant models and controllers, respectively, upon initialization.
26
 */
27
class BootstrapEventHandler implements EventListenerInterface
28
{
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @codeCoverageIgnore
33
     */
34
    public function implementedEvents(): array
35
    {
36
        return [
37
            'Controller.initialize' => 'onControllerInitialize',
38
            'Model.initialize' => 'onModelInitialize',
39
        ];
40
    }
41
42
    /**
43
     * Attach placeholders component on BEdita API controllers.
44
     *
45
     * @param \Cake\Event\Event $event Dispatched event.
46
     * @return void
47
     */
48
    public function onControllerInitialize(Event $event): void
49
    {
50
        $controller = $event->getSubject();
51
        if (!$controller instanceof ObjectsController) {
52
            return;
53
        }
54
55
        $controller->loadComponent('BEdita/Placeholders.Placeholders');
56
    }
57
58
    /**
59
     * Attach placeholders behavior on BEdita objects tables.
60
     *
61
     * @param \Cake\Event\Event $event Dispatched event.
62
     * @return void
63
     */
64
    public function onModelInitialize(Event $event): void
65
    {
66
        $table = $event->getSubject();
67
        if (!$table instanceof ObjectsTable && !$table instanceof ObjectsBaseTable) {
68
            return;
69
        }
70
71
        $table
72
            ->addBehavior('BEdita/Placeholders.Placeholders')
73
            ->addBehavior('BEdita/Placeholders.Placeholded');
74
    }
75
}
76