Passed
Push — develop ( 08926b...fc8040 )
by Brent
03:01
created

OrderAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B transformPage() 0 29 4
A validateConfig() 0 5 2
1
<?php
2
3
namespace Brendt\Stitcher\Adapter;
4
5
use Brendt\Stitcher\Exception\ConfigurationException;
6
use Brendt\Stitcher\Factory\AdapterFactory;
7
use Brendt\Stitcher\Site\Page;
8
9
/**
10
 * Order a collection of variables by a specified field.
11
 *
12
 * Sample configuration:
13
 *
14
 *  /examples:
15
 *      template: examples/detail
16
 *      data:
17
 *          example: collection.yml
18
 *      adapters:
19
 *          order:
20
 *              variableName:
21
 *                  field: id
22
 *                  direction: desc
23
 */
24
class OrderAdapter extends AbstractAdapter
25
{
26
27
    /**
28
     * A collection of keywords which would reverse the data set.
29
     *
30
     * @var array
31
     */
32
    const REVERSE = [
33
        'desc',
34
        'DESC',
35
        '-',
36
    ];
37
38
    /**
39
     * @param Page        $page
40
     * @param string|null $filter
41
     *
42
     * @return Page[]
43
     */
44
    public function transformPage(Page $page, $filter = null) : array {
45
        $config = $page->getAdapterConfig(AdapterFactory::ORDER_ADAPTER);
46
        $config = isset($config['variable']) ? [$config['variable'] => $config] : $config;
47
48
        foreach ($config as $variable => $order) {
49
            $this->validateConfig($order);
0 ignored issues
show
Bug introduced by
It seems like $order defined by $order on line 48 can also be of type null; however, Brendt\Stitcher\Adapter\...apter::validateConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
51
            $field = $order['field'];
52
            $direction = $order['direction'] ?? null;
53
54
            $entries = $this->getData($page->getVariable($variable));
55
56
            uasort($entries, function ($a, $b) use ($field) {
57
                return strcmp($a[$field], $b[$field]);
58
            });
59
60
            if (in_array($direction, self::REVERSE)) {
61
                $entries = array_reverse($entries, true);
62
            }
63
64
            $page->setVariableValue($variable, $entries)
65
                ->setVariableIsParsed($variable);
66
        }
67
68
        /** @var Page[] $result */
69
        $result = [$page->getId() => $page];
70
71
        return $result;
72
    }
73
74
    /**
75
     * @param array $config
76
     *
77
     * @throws ConfigurationException
78
     */
79
    private function validateConfig(array $config) {
80
        if (!isset($config['field'])) {
81
            throw ConfigurationException::requiredAdapterOptions(AdapterFactory::ORDER_ADAPTER, 'field');
82
        }
83
    }
84
}
85