Plugin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Plugin;
25
26
use byrokrat\giroapp\Console\ConsoleInterface;
27
use byrokrat\giroapp\Db\DriverFactoryInterface;
28
use byrokrat\giroapp\Filter\FilterInterface;
29
use byrokrat\giroapp\Formatter\FormatterInterface;
30
use byrokrat\giroapp\Event\Listener\ListenerInterface;
31
use byrokrat\giroapp\Sorter\SorterInterface;
32
use byrokrat\giroapp\Status\StatisticInterface;
33
use byrokrat\giroapp\Xml\CompilerPassInterface;
34
use Psr\EventDispatcher\ListenerProviderInterface;
35
36
class Plugin implements PluginInterface
37
{
38
    /** @var array<int, mixed> */
39
    private $objects;
40
41
    /** @var string */
42
    private $pluginName = '';
43
44
    /**
45
     * @param array<int, mixed> $objects
46
     */
47
    public function __construct(...$objects)
48
    {
49
        $this->objects = $objects;
50
    }
51
52
    final public function loadPlugin(EnvironmentInterface $environment): void
53
    {
54
        foreach ($this->objects as $item) {
55
            switch (true) {
56
                case $item instanceof ApiVersionConstraint:
57
                    $environment->assertApiVersion($item);
58
                    $this->pluginName = $item->getName();
59
                    break;
60
                case $item instanceof DriverFactoryInterface:
61
                    $environment->registerDatabaseDriver($item);
62
                    break;
63
                case $item instanceof ConsoleInterface:
64
                    $environment->registerConsoleCommand($item);
65
                    break;
66
                case $item instanceof ListenerInterface:
67
                    if (!is_callable($item)) {
68
                        throw new \LogicException(sprintf(
69
                            'Class %s implements the ListenerInterface but is not callable',
70
                            get_class($item)
71
                        ));
72
                    }
73
                    $environment->registerListener($item);
74
                    break;
75
                case $item instanceof ListenerProviderInterface:
76
                    $environment->registerListenerProvider($item);
77
                    break;
78
                case $item instanceof FilterInterface:
79
                    $environment->registerDonorFilter($item);
80
                    break;
81
                case $item instanceof FormatterInterface:
82
                    $environment->registerDonorFormatter($item);
83
                    break;
84
                case $item instanceof SorterInterface:
85
                    $environment->registerDonorSorter($item);
86
                    break;
87
                case $item instanceof StatisticInterface:
88
                    $environment->registerStatistic($item);
89
                    break;
90
                case $item instanceof CompilerPassInterface:
91
                    $environment->registerXmlMandateCompilerPass($item);
92
                    break;
93
                default:
94
                    throw new \InvalidArgumentException(sprintf(
95
                        "Unknown item type '%s' in plugin '%s'",
96
                        is_object($item) ? get_class($item) : gettype($item),
97
                        $this->pluginName
98
                    ));
99
            }
100
        }
101
    }
102
}
103