Passed
Push — develop ( 0151b1...067575 )
by Brent
06:26
created

OrderAdapter::isValidConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 4
cts 4
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\InvalidOrderAdapter;
6
use Stitcher\Page\Adapter;
7
use Stitcher\Configureable;
8
use Stitcher\Variable\VariableParser;
9
10
class OrderAdapter implements Adapter, Configureable
11
{
12
    private const REVERSE = [
13
        'desc',
14
        'DESC',
15
        '-',
16
    ];
17
18
    /** @var \Stitcher\Variable\VariableParser */
19
    private $variableParser;
20
21
    /** @var string */
22
    private $variable;
23
24
    /** @var string */
25
    private $field;
26
27
    /** @var string */
28
    private $direction;
29
30 2
    public function __construct(
31
        array $adapterConfiguration,
32
        VariableParser $variableParser
33
    ) {
34 2
        if (! $this->isValidConfiguration($adapterConfiguration)) {
35
            throw InvalidOrderAdapter::create();
36
        }
37
38 2
        $this->variable = $adapterConfiguration['variable'];
39 2
        $this->field = $adapterConfiguration['field'];
40 2
        $this->direction = $adapterConfiguration['direction'] ?? 'asc';
41
42 2
        $this->variableParser = $variableParser;
43 2
    }
44
45 2
    public static function make(
46
        array $adapterConfiguration,
47
        VariableParser $variableParser
48
    ): OrderAdapter {
49 2
        return new self($adapterConfiguration, $variableParser);
50
    }
51
52 1
    public function transform(array $pageConfiguration): array
53
    {
54 1
        $entries = $this->getEntries($pageConfiguration);
55
56 1
        $orderedEntries = $this->orderEntries($entries);
57
58 1
        $pageConfiguration['variables'][$this->variable] = $orderedEntries;
59
60 1
        unset($pageConfiguration['config']['order']);
61
62 1
        return [$pageConfiguration];
63
    }
64
65 2
    public function isValidConfiguration($subject): bool
66
    {
67 2
        return \is_array($subject)
68 2
            && isset($subject['variable'])
69 2
            && isset($subject['field']);
70
    }
71
72 1
    private function getEntries($pageConfiguration): ?array
73
    {
74 1
        $variable = $pageConfiguration['variables'][$this->variable] ?? null;
75
76 1
        return $this->variableParser->parse($variable) ?? $variable;
77
    }
78
79
    private function orderEntries(array $entries): array
80
    {
81 1
        uasort($entries, function ($a, $b) {
82 1
            return strcmp($a[$this->field] ?? '', $b[$this->field] ?? '');
83 1
        });
84
85 1
        if (in_array($this->direction, self::REVERSE)) {
86 1
            $entries = array_reverse($entries, true);
87
        }
88
89 1
        return $entries;
90
    }
91
}
92