OpenCubes::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\OpenCubes;
4
5
use BenTools\OpenCubes\Component\BreakDown\BreakDownComponent;
6
use BenTools\OpenCubes\Component\BreakDown\BreakDownComponentFactory;
7
use BenTools\OpenCubes\Component\BreakDown\BreakDownUriManager;
8
use BenTools\OpenCubes\Component\BreakDown\BreakDownUriManagerInterface;
9
use BenTools\OpenCubes\Component\ComponentFactoryInterface;
10
use BenTools\OpenCubes\Component\ComponentInterface;
11
use BenTools\OpenCubes\Component\Filter\FilterComponent;
12
use BenTools\OpenCubes\Component\Filter\FilterComponentFactory;
13
use BenTools\OpenCubes\Component\Filter\FilterUriManager;
14
use BenTools\OpenCubes\Component\Filter\FilterUriManagerInterface;
15
use BenTools\OpenCubes\Component\Pager\PagerComponent;
16
use BenTools\OpenCubes\Component\Pager\PagerComponentFactory;
17
use BenTools\OpenCubes\Component\Pager\PagerUriManager;
18
use BenTools\OpenCubes\Component\Pager\PagerUriManagerInterface;
19
use BenTools\OpenCubes\Component\Sort\SortComponent;
20
use BenTools\OpenCubes\Component\Sort\SortComponentFactory;
21
use BenTools\OpenCubes\Component\Sort\SortUriManager;
22
use BenTools\OpenCubes\Component\Sort\SortUriManagerInterface;
23
use Psr\Http\Message\UriInterface;
24
25
final class OpenCubes
26
{
27
    /**
28
     * @var ComponentFactoryInterface[]
29
     */
30
    private $factories = [];
31
32
    /**
33
     * OpenCubes constructor.
34
     * @param iterable $factories
35
     */
36
    public function __construct(iterable $factories = [])
37
    {
38
        foreach ($factories as $factory) {
39
            $this->registerFactory($factory);
40
        }
41
    }
42
43
    /**
44
     * @param ComponentFactoryInterface $factory
45
     */
46
    public function registerFactory(ComponentFactoryInterface $factory): void
47
    {
48
        array_unshift($this->factories, $factory);
49
    }
50
51
    /**
52
     * @param string            $componentName
53
     * @param array             $options
54
     * @param UriInterface|null $uri
55
     * @return ComponentInterface
56
     * @throws \RuntimeException
57
     */
58
    public function getComponent(string $componentName, array $options = [], UriInterface $uri = null): ComponentInterface
59
    {
60
        $uri = $uri ?? current_location();
61
62
        foreach ($this->factories as $factory) {
63
            if ($factory->supports($componentName)) {
64
                return $factory->createComponent($uri, $options);
65
            }
66
        }
67
68
        throw new \RuntimeException(sprintf('Component "%s" could not be instanciated.', $componentName));
69
    }
70
71
    /**
72
     * Instanciate with default configuration.
73
     *
74
     * @param array                             $options
75
     * @param PagerUriManagerInterface|null     $pagerUriManager
0 ignored issues
show
Bug introduced by
There is no parameter named $pagerUriManager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @param SortUriManagerInterface|null      $sortUriManager
0 ignored issues
show
Bug introduced by
There is no parameter named $sortUriManager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     * @param FilterUriManagerInterface|null    $filterUriManager
0 ignored issues
show
Bug introduced by
There is no parameter named $filterUriManager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
     * @param BreakDownUriManagerInterface|null $breakDownUriManager
0 ignored issues
show
Bug introduced by
There is no parameter named $breakDownUriManager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
     * @return OpenCubes
80
     */
81
    public static function create(
82
        array $options = [],
83
        ...$args
84
    ): self {
85
86
        $pagerUriManager = (function (array $args) use ($options): PagerUriManagerInterface {
87
            foreach ($args as $arg) {
88
                if ($arg instanceof PagerUriManagerInterface) {
89
                    return $arg;
90
                }
91
            }
92
93
            return new PagerUriManager($options[PagerComponent::getName() . '_uri'] ?? []);
94
        })($args);
95
96
        $sortUriManager = (function (array $args) use ($options, $pagerUriManager): SortUriManagerInterface {
97
            foreach ($args as $arg) {
98
                if ($arg instanceof SortUriManagerInterface) {
99
                    return $arg;
100
                }
101
            }
102
103
            return new SortUriManager($options[SortComponent::getName() . '_uri'] ?? [], $pagerUriManager);
104
        })($args);
105
106
        $filterUriManager = (function (array $args) use ($options, $pagerUriManager): FilterUriManagerInterface {
107
            foreach ($args as $arg) {
108
                if ($arg instanceof FilterUriManagerInterface) {
109
                    return $arg;
110
                }
111
            }
112
113
            return new FilterUriManager($options[FilterComponent::getName() . '_uri'] ?? [], $pagerUriManager);
114
        })($args);
115
116
        $breakDownUriManager = (function (array $args) use ($options, $pagerUriManager, $sortUriManager): BreakDownUriManagerInterface {
117
            foreach ($args as $arg) {
118
                if ($arg instanceof BreakDownUriManagerInterface) {
119
                    return $arg;
120
                }
121
            }
122
123
            return new BreakDownUriManager($options[BreakDownComponent::getName() . '_uri'] ?? [], $pagerUriManager, $sortUriManager);
124
        })($args);
125
126
        return new self([
127
            new PagerComponentFactory($options[PagerComponent::getName()] ?? [], $pagerUriManager),
128
            new SortComponentFactory($options[SortComponent::getName()] ?? [], $sortUriManager),
129
            new FilterComponentFactory($options[FilterComponent::getName()] ?? [], $filterUriManager),
130
            new BreakDownComponentFactory($options[BreakDownComponent::getName()] ?? [], $breakDownUriManager),
131
        ]);
132
    }
133
}
134