CollectionAdapter   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 167
ccs 0
cts 74
cp 0
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A make() 0 6 1
A setFilterId() 0 4 1
A transform() 0 28 4
A isValidConfiguration() 0 4 3
A getEntries() 0 6 1
A createEntryConfiguration() 0 23 1
A createMetaVariable() 0 14 3
A getTitleMeta() 0 9 1
A getDescriptionMeta() 0 9 1
A createBrowseData() 0 24 3
1
<?php
2
3
namespace Stitcher\Page\Adapter;
4
5
use Stitcher\Exception\InvalidCollectionAdapter;
6
use Stitcher\Page\Adapter;
7
use Stitcher\Configureable;
8
use Stitcher\Variable\VariableParser;
9
10
class CollectionAdapter implements Adapter, Configureable
11
{
12
    /** @var null */
13
    private static $filterId;
14
15
    /** @var array */
16
    private $entries = [];
17
18
    /** @var mixed */
19
    private $parameter;
20
21
    /** @var mixed */
22
    private $variable;
23
24
    /** @var \Stitcher\Variable\VariableParser */
25
    private $variableParser;
26
27
    public function __construct(
28
        array $adapterConfiguration,
29
        VariableParser $variableParser
30
    ) {
31
        if (! $this->isValidConfiguration($adapterConfiguration)) {
32
            throw InvalidCollectionAdapter::create();
33
        }
34
35
        $this->variable = $adapterConfiguration['variable'];
36
        $this->parameter = $adapterConfiguration['parameter'];
37
        $this->variableParser = $variableParser;
38
    }
39
40
    public static function make(
41
        array $adapterConfiguration,
42
        VariableParser $variableParser
43
    ): CollectionAdapter {
44
        return new self($adapterConfiguration, $variableParser);
45
    }
46
47
    public static function setFilterId(?string $filterId)
48
    {
49
        self::$filterId = $filterId;
50
    }
51
52
    public function transform(array $pageConfiguration): array
53
    {
54
        $this->entries = $this->getEntries($pageConfiguration);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getEntries($pageConfiguration) of type * is incompatible with the declared type array of property $entries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
56
        $collectionPageConfiguration = [];
57
58
        while ($entry = current($this->entries)) {
59
            $entryId = key($this->entries);
60
61
            if (self::$filterId !== null && self::$filterId != $entryId) {
62
                next($this->entries);
63
64
                continue;
65
            }
66
67
            $entryConfiguration = $this->createEntryConfiguration(
68
                $pageConfiguration,
69
                $entryId,
70
                $entry
71
            );
72
73
            $collectionPageConfiguration[$entryConfiguration['id']] = $entryConfiguration;
74
75
            next($this->entries);
76
        }
77
78
        return $collectionPageConfiguration;
79
    }
80
81
    public function isValidConfiguration($subject): bool
82
    {
83
        return \is_array($subject) && isset($subject['variable']) && isset($subject['parameter']);
84
    }
85
86
    protected function getEntries($pageConfiguration): ?array
87
    {
88
        $variable = $pageConfiguration['variables'][$this->variable] ?? null;
89
90
        return $this->variableParser->parse($variable) ?? $variable;
91
    }
92
93
    protected function createEntryConfiguration(array $pageConfiguration, $entryId, $entry): array
94
    {
95
        $entryConfiguration = $pageConfiguration;
96
97
        $parsedEntryId = str_replace('{' . $this->parameter . '}', $entryId, $pageConfiguration['id']);
98
99
        $entryConfiguration['id'] = $parsedEntryId;
100
101
        $entryConfiguration['variables'][$this->variable] = $entry;
102
103
        $entryConfiguration['variables']['meta'] = array_merge(
104
            $entryConfiguration['variables']['meta'] ?? [],
105
            $this->createMetaVariable($entryConfiguration)
106
        );
107
108
        $entryConfiguration['variables']['_browse'] =
109
            $entryConfiguration['variables']['_browse']
110
            ?? $this->createBrowseData();
111
112
        unset($entryConfiguration['config']['collection']);
113
114
        return $entryConfiguration;
115
    }
116
117
    protected function createMetaVariable(array $entryConfiguration): array
118
    {
119
        $meta = [];
120
121
        if ($title = $this->getTitleMeta($entryConfiguration)) {
122
            $meta['title'] = $title;
123
        }
124
125
        if ($description = $this->getDescriptionMeta($entryConfiguration)) {
126
            $meta['description'] = $description;
127
        }
128
129
        return $meta;
130
    }
131
132
    protected function getTitleMeta(array $entryConfiguration): ?string
133
    {
134
        $title =
135
            $entryConfiguration['variables'][$this->variable]['meta']['title']
136
            ?? $entryConfiguration['variables'][$this->variable]['title']
137
            ?? null;
138
139
        return $title;
140
    }
141
142
    protected function getDescriptionMeta(array $entryConfiguration): ?string
143
    {
144
        $description =
145
            $entryConfiguration['variables'][$this->variable]['meta']['description']
146
            ?? $entryConfiguration['variables'][$this->variable]['description']
147
            ?? null;
148
149
        return $description;
150
    }
151
152
    protected function createBrowseData(): array
153
    {
154
        $browse = [];
155
156
        $prev = prev($this->entries);
157
158
        if (! $prev) {
159
            reset($this->entries);
160
        } else {
161
            $browse['prev'] = $prev;
162
163
            next($this->entries);
164
        }
165
166
        $next = next($this->entries);
167
168
        if ($next) {
169
            $browse['next'] = $next;
170
        }
171
172
        prev($this->entries);
173
174
        return $browse;
175
    }
176
}
177