Passed
Push — develop ( 7c7d13...1e3e9e )
by Brent
02:40
created

CollectionAdapter::getEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 = null;
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 16
    public function __construct(
28
        array $adapterConfiguration,
29
        VariableParser $variableParser
30
    ) {
31 16
        if (! $this->isValidConfiguration($adapterConfiguration)) {
32
            throw InvalidCollectionAdapter::create();
33
        }
34
35 16
        $this->variable = $adapterConfiguration['variable'];
36 16
        $this->parameter = $adapterConfiguration['parameter'];
37 16
        $this->variableParser = $variableParser;
38 16
    }
39
40 16
    public static function make(
41
        array $adapterConfiguration,
42
        VariableParser $variableParser
43
    ): CollectionAdapter {
44 16
        return new self($adapterConfiguration, $variableParser);
45
    }
46
47 5
    public static function setFilterId(?string $filterId)
48
    {
49 5
        self::$filterId = $filterId;
50 5
    }
51
52 15
    public function transform(array $pageConfiguration): array
53
    {
54 15
        $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 15
        $collectionPageConfiguration = [];
57
58 15
        while ($entry = current($this->entries)) {
59 15
            $entryId = key($this->entries);
60
61 15
            if (self::$filterId !== null && self::$filterId !== $entryId) {
62 1
                next($this->entries);
63
64 1
                continue;
65
            }
66
67 15
            $entryConfiguration = $this->createEntryConfiguration(
68 15
                $pageConfiguration,
69 15
                $entryId,
70 15
                $entry
71
            );
72
73 15
            $collectionPageConfiguration[$entryConfiguration['id']] = $entryConfiguration;
74
75 15
            next($this->entries);
76
        }
77
78 15
        return $collectionPageConfiguration;
79
    }
80
81 16
    public function isValidConfiguration($subject): bool
82
    {
83 16
        return is_array($subject) && isset($subject['variable']) && isset($subject['parameter']);
84
    }
85
86 15
    protected function getEntries($pageConfiguration): ?array
87
    {
88 15
        $variable = $pageConfiguration['variables'][$this->variable] ?? null;
89
90 15
        return $this->variableParser->parse($variable) ?? $variable;
91
    }
92
93 15
    protected function createEntryConfiguration(array $pageConfiguration, $entryId, $entry): array
94
    {
95 15
        $entryConfiguration = $pageConfiguration;
96
97 15
        $parsedEntryId = str_replace('{' . $this->parameter . '}', $entryId, $pageConfiguration['id']);
98
99 15
        $entryConfiguration['id'] = $parsedEntryId;
100
101 15
        $entryConfiguration['variables'][$this->variable] = $entry;
102
103 15
        $entryConfiguration['variables']['meta'] = array_merge(
104 15
            $entryConfiguration['variables']['meta'] ?? [],
105 15
            $this->createMetaVariable($entryConfiguration)
106
        );
107
108 15
        $entryConfiguration['variables']['_browse'] =
109 15
            $entryConfiguration['variables']['_browse']
110 15
            ?? $this->createBrowseData();
111
112 15
        unset($entryConfiguration['config']['collection']);
113
114 15
        return $entryConfiguration;
115
    }
116
117 15
    protected function createMetaVariable(array $entryConfiguration): array
118
    {
119 15
        $meta = [];
120
121 15
        if ($title = $this->getTitleMeta($entryConfiguration)) {
122 13
            $meta['title'] = $title;
123
        }
124
125 15
        if ($description = $this->getDescriptionMeta($entryConfiguration)) {
126 1
            $meta['description'] = $description;
127
        }
128
129 15
        return $meta;
130
    }
131
132 15
    protected function getTitleMeta(array $entryConfiguration): ?string
133
    {
134
        $title =
135 15
            $entryConfiguration['variables'][$this->variable]['meta']['title']
136 15
            ?? $entryConfiguration['variables'][$this->variable]['title']
137 15
            ?? null;
138
139 15
        return $title;
140
    }
141
142 15
    protected function getDescriptionMeta(array $entryConfiguration): ?string
143
    {
144
        $description =
145 15
            $entryConfiguration['variables'][$this->variable]['meta']['description']
146 15
            ?? $entryConfiguration['variables'][$this->variable]['description']
147 15
            ?? null;
148
149 15
        return $description;
150
    }
151
152 15
    protected function createBrowseData(): array
153
    {
154 15
        $browse = [];
155
156 15
        $prev = prev($this->entries);
157
158 15
        if (! $prev) {
159 15
            reset($this->entries);
160
        } else {
161 14
            $browse['prev'] = $prev;
162
163 14
            next($this->entries);
164
        }
165
166 15
        $next = next($this->entries);
167
168 15
        if ($next) {
169 15
            $browse['next'] = $next;
170
        }
171
172 15
        prev($this->entries);
173
174 15
        return $browse;
175
    }
176
}
177