Completed
Pull Request — master (#45)
by
unknown
15:41
created

RestlerEnhancer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Aoe\Restler\System\TYPO3;
4
5
use Aoe\Restler\System\Restler\Builder as RestlerBuilder;
6
use TYPO3\CMS\Core\Routing\Aspect\AspectInterface;
7
use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface;
8
use TYPO3\CMS\Core\Routing\RouteCollection;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Extbase\Object\ObjectManager;
11
12
class RestlerEnhancer implements DecoratingEnhancerInterface
13
{
14
    /**
15
     * @var RestlerBuilder
16
     */
17
    private $restlerBuilder;
18
19
    public function __construct($configuration)
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
    {
21
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
22
        $this->restlerBuilder = $objectManager->get(RestlerBuilder::class);
23
    }
24
25
26
    /**
27
     * Gets pattern that can be used to redecorate (undecorate)
28
     * a potential previously decorated route path.
29
     *
30
     * Example:
31
     * + route path: 'first/second.html'
32
     * + redecoration pattern: '(?:\.html|\.json)$'
33
     * -> 'first/second' might be the redecorated route path after
34
     *    applying the redecoration pattern to preg_match/preg_replace
35
     *
36
     * @return string regular expression pattern
37
     */
38
    public function getRoutePathRedecorationPattern(): string
39
    {
40
        return '.$';
41
    }
42
43
    /**
44
     * Decorates route collection to be processed during URL resolving.
45
     * Executed before invoking routing enhancers.
46
     *
47
     * @param RouteCollection $collection
48
     * @param string $routePath URL path
49
     */
50
    public function decorateForMatching(RouteCollection $collection, string $routePath): void
51
    {
52
        $this->restlerBuilder->build(null);
53
54
        // set path according to typo3/sysext/core/Classes/Routing/PageRouter.php:132
55
        $prefixedUrlPath = '/' . trim($routePath, '/');
56
        
57
        if ($this->isRestlerUrl($prefixedUrlPath)) {
58
            $defaultRoute = $collection->get('default');
59
            $defaultRoute->setPath($prefixedUrlPath);
60
            $collection->add('restler', $defaultRoute);
0 ignored issues
show
Bug introduced by
It seems like $defaultRoute defined by $collection->get('default') on line 58 can be null; however, Symfony\Component\Routing\RouteCollection::add() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
61
        }
62
    }
63
64
    /**
65
     * Decorates route collection during URL URL generation.
66
     * Executed before invoking routing enhancers.
67
     *
68
     * @param RouteCollection $collection
69
     * @param array $parameters query parameters
70
     */
71
    public function decorateForGeneration(RouteCollection $collection, array $parameters): void
72
    {
73
    }
74
75
    /**
76
     * @param AspectInterface[] $aspects
77
     */
78
    public function setAspects(array $aspects): void
79
    {
80
    }
81
82
    /**
83
     * @return AspectInterface[]
84
     */
85
    public function getAspects(): array
86
    {
87
        return [];
88
    }
89
90
    private function isRestlerUrl($uri): bool
91
    {
92
        return \Aoe\Restler\System\Restler\Routes::containsUrl($uri);
93
    }
94
95
}
96