Passed
Push — develop ( 359c84...5df010 )
by Brent
04:09
created

CollectionAdapter::isValidConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

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