Test Failed
Push — develop ( d71559...359c84 )
by Brent
06:20 queued 17s
created

CollectionAdapter::createMetaVariable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

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