ContextHelper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ofRequestUriPath() 0 23 3
B getDimensionValuesForUriSegments() 0 24 6
A getDefaultDimensionValues() 0 8 2
A getUriSegments() 0 5 1
A allowsCallOfMethod() 0 4 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 (empty($dimensionValues)) {
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
    public function allowsCallOfMethod($methodName)
97
    {
98
        return true;
99
    }
100
}
101