Completed
Pull Request — master (#21)
by
unknown
06:16
created

ContextHelper::ofRequestUriPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace MOC\NotFound\Fusion\Eel\Helper;
4
5
use Neos\Flow\Annotations as Flow;
6
use Neos\Eel\ProtectedContextAwareInterface;
7
8
class ContextHelper implements ProtectedContextAwareInterface
9
{
10
    /**
11
     * @Flow\InjectConfiguration(path="contentDimensions", package="Neos.ContentRepository")
12
     * @var array
13
     */
14
    protected $contentDimensionsConfiguration;
15
16
    /**
17
     * Returns a context array with matched dimension values per dimension for given request uri path. If nothing
18
     * matches, it returns a context array with default dimension values per dimension.
19
     *
20
     * @param $requestUriPath
21
     * @return array
22
     */
23
    public function ofRequestUriPath($requestUriPath)
24
    {
25
        // No dimensions configured, context is empty
26
        if (count($this->contentDimensionsConfiguration) === 0) {
27
            return [];
28
        }
29
30
        $uriSegments = $this->getUriSegments($requestUriPath);
31
        $dimensionValues = $this->getDimensionValuesForUriSegments($uriSegments);
32
        if (!$dimensionValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dimensionValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            $dimensionValues = $this->getDefaultDimensionValues();
34
        }
35
36
        $targetDimensionValues = array_map(function ($dimensionValues) {
37
            return reset($dimensionValues); // Default target dimension value is first dimension value
38
        }, $dimensionValues);
39
40
41
        return [
42
            'dimensions' => $dimensionValues,
43
            'targetDimensions' => $targetDimensionValues
44
        ];
45
    }
46
47
    /**
48
     * @param array $uriSegments
49
     * @return array
50
     */
51
    protected function getDimensionValuesForUriSegments($uriSegments)
52
    {
53
        if (count($uriSegments) !== count($this->contentDimensionsConfiguration)) {
54
            return [];
55
        }
56
57
        $index = 0;
58
        $dimensionValues = [];
59
        foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) {
60
            $uriSegment = $uriSegments[$index++];
61
            foreach ($dimensionConfiguration['presets'] as $preset) {
62
                if ($uriSegment === $preset['uriSegment']) {
63
                    $dimensionValues[$dimensionName] = $preset['values'];
64
                    continue 2;
65
                }
66
            }
67
        }
68
69
        if (count($uriSegments) !== count($dimensionValues)) {
70
            return [];
71
        }
72
73
        return $dimensionValues;
74
    }
75
76
    /**
77
     * Returns default dimension values per dimension.
78
     *
79
     * @return array
80
     */
81
    protected function getDefaultDimensionValues()
82
    {
83
        $dimensionValues = [];
84
        foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) {
85
            $dimensionValues[$dimensionName] =  [$dimensionConfiguration['default']];
86
        }
87
        return $dimensionValues;
88
    }
89
90
    protected function getUriSegments($requestUriPath)
91
    {
92
        $pathParts = explode('/', trim($requestUriPath, '/'), 2);
93
        return explode('_', $pathParts[0]);
94
    }
95
96
    function allowsCallOfMethod($methodName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
    {
98
        return true;
99
    }
100
}
101