Passed
Push — master ( f41107...4862c9 )
by Brent
02:34
created

CollectionAdapter::transform()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 3
nop 2
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Adapter;
4
5
use Brendt\Stitcher\Exception\ConfigurationException;
6
use Brendt\Stitcher\Exception\IdFieldNotFoundException;
7
use Brendt\Stitcher\Exception\VariableNotFoundException;
8
use Brendt\Stitcher\Factory\AdapterFactory;
9
use Brendt\Stitcher\Site\Page;
10
11
/**
12
 * The CollectionAdapter takes a page with a collection of entries, and generates a detail page for each entry in the
13
 * collection.
14
 *
15
 * Sample configuration:
16
 *
17
 *  /examples/{id}:
18
 *      template: examples/detail
19
 *      data:
20
 *          example: collection.yml
21
 *      adapters:
22
 *          collection:
23
 *              variable: example
24
 *              field: id
25
 *
26
 * @todo Rename `field` option to `urlVariable`
27
 */
28
class CollectionAdapter extends AbstractAdapter
29
{
30
31
    /**
32
     * @param Page  $page
33
     * @param mixed $filter
34
     *
35
     * @return Page[]
36
     * @throws IdFieldNotFoundException
37
     * @throws VariableNotFoundException
38
     */
39
    public function transform(Page $page, $filter = null) {
40
        $config = $page->getAdapterConfig(AdapterFactory::COLLECTION_ADAPTER);
41
42
        $this->validateConfig($config, $page);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $page->getAdapterConfig(...ry::COLLECTION_ADAPTER) on line 40 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...
43
44
        $variable = $config['variable'];
45
        $source = $page->getVariable($variable);
46
        $field = $config['field'];
47
        $pageId = $page->getId();
48
        $entries = $this->getData($source);
49
50
        $result = [];
51
        foreach ($entries as $entry) {
52
            if (!isset($entry[$field]) || ($filter && $entry[$field] !== $filter)) {
53
                continue;
54
            }
55
56
            $fieldValue = $entry[$field];
57
58
            $url = str_replace('{' . $field . '}', $fieldValue, $pageId);
59
            $entryPage = clone $page;
60
61
            $entryPage
62
                ->removeAdapter(AdapterFactory::COLLECTION_ADAPTER)
63
                ->setVariableValue($variable, $entry)
64
                ->setVariableIsParsed($variable)
65
                ->setId($url);
66
67
            $result[$url] = $entryPage;
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * @param array $config
75
     * @param Page  $page
76
     *
77
     * @return void
78
     * @throws ConfigurationException
79
     * @throws IdFieldNotFoundException
80
     * @throws VariableNotFoundException
81
     */
82
    protected function validateConfig(array $config, Page $page) {
83
        if (!isset($config['field'], $config['variable'])) {
84
            throw new ConfigurationException('Both the configuration entry `field` and `variable` are required when using the Collection adapter.');
85
        }
86
87
        $variable = $config['variable'];
88
89
        if (!$page->getVariable($variable)) {
90
            throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\"");
91
        }
92
93
        $field = $config['field'];
94
        $pageId = $page->getId();
95
96
        if (strpos($pageId, '{' . $field . '}') === false) {
97
            throw new IdFieldNotFoundException("The field \"{{$field}}\" was not found in the URL \"{$page->getId()}\"");
98
        }
99
    }
100
101
}
102