RewritingRouterFirst   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 0
dl 0
loc 122
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F matchRequest() 0 102 16
A fetchRewritingRedirectTypeFromUrl() 0 9 1
1
<?php
2
3
namespace RewriteUrl\Service;
4
5
use RewriteUrl\Model\RewriteurlRule;
6
use RewriteUrl\Model\RewriteurlRuleQuery;
7
use RewriteUrl\Model\RewritingRedirectType;
8
use RewriteUrl\Model\RewritingRedirectTypeQuery;
9
use Thelia\Core\Routing\RewritingRouter;
10
use Propel\Runtime\ActiveQuery\Criteria;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
13
use Thelia\Core\HttpFoundation\Request as TheliaRequest;
14
use Thelia\Exception\UrlRewritingException;
15
use Thelia\Model\ConfigQuery;
16
use Thelia\Model\LangQuery;
17
use Thelia\Model\RewritingUrlQuery;
18
use Thelia\Tools\URL;
19
20
/**
21
 * This router is intended to be the very first checked by the ChainRouter on a request.
22
 */
23
class RewritingRouterFirst extends RewritingRouter
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function matchRequest(Request $request)
29
    {
30
        if (ConfigQuery::isRewritingEnable()) {
31
            $urlTool = URL::getInstance();
32
33
            $pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo();
0 ignored issues
show
Bug introduced by
The class Thelia\Core\HttpFoundation\Request does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
34
35
36
37
            // Check RewriteUrl rules
38
39
            $ruleCollection = RewriteurlRuleQuery::create()
40
                ->filterByOnly404(0)
41
                ->orderByPosition()
42
                ->find();
43
44
            /** @var RewriteurlRule $rule */
45
            foreach ($ruleCollection as $rule) {
46
                if ($rule->isMatching($pathInfo, $request->query->all())) {
47
                    $this->redirect($urlTool->absoluteUrl($rule->getRedirectUrl()), 301);
48
                }
49
            }
50
51
52
53
            try {
54
                $rewrittenUrlData = $urlTool->resolve($pathInfo);
55
            } catch (UrlRewritingException $e) {
0 ignored issues
show
Bug introduced by
The class Thelia\Exception\UrlRewritingException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
56
                switch ($e->getCode()) {
57
                    case UrlRewritingException::URL_NOT_FOUND:
58
                        throw new ResourceNotFoundException();
59
                        break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
60
                    default:
61
                        throw $e;
62
                }
63
            }
64
65
            // If we have a "lang" parameter, whe have to check if the found URL has the proper locale
66
            // If it's not the case, find the rewritten URL with the requested locale, and redirect to it.
67
            if (null ==! $requestedLocale = $request->get('lang')) {
68
                if (null !== $requestedLang = LangQuery::create()->findOneByLocale($requestedLocale)) {
69
                    if ($requestedLang->getLocale() != $rewrittenUrlData->locale) {
70
                        $localizedUrl = $urlTool->retrieve(
71
                            $rewrittenUrlData->view,
72
                            $rewrittenUrlData->viewId,
73
                            $requestedLang->getLocale()
74
                        )->toString();
75
76
                        $this->redirect($urlTool->absoluteUrl($localizedUrl), 301);
77
                    }
78
                }
79
            }
80
81
            /* is the URL redirected ? */
82
            if (null !== $rewrittenUrlData->redirectedToUrl) {
83
                $redirect = RewritingUrlQuery::create()
84
                    ->filterByView($rewrittenUrlData->view)
85
                    ->filterByViewId($rewrittenUrlData->viewId)
86
                    ->filterByViewLocale($rewrittenUrlData->locale)
87
                    ->filterByRedirected(null, Criteria::ISNULL)
88
                    ->findOne()
89
                ;
90
91
                // Differences with the base class for handling 301 or 302 redirection
92
                $redirectType = $this->fetchRewritingRedirectTypeFromUrl($rewrittenUrlData->rewrittenUrl);
93
94
                if ($redirectType == null) {
95
                    $httpRedirectCode = RewritingRedirectType::DEFAULT_REDIRECT_TYPE;
96
                } else {
97
                    $httpRedirectCode = $redirectType->getHttpcode();
98
                }
99
                // End of differences
100
101
                $this->redirect($urlTool->absoluteUrl($redirect->getUrl()), $httpRedirectCode);
102
            }
103
104
            /* define GET arguments in request */
105
106
            if (null !== $rewrittenUrlData->view) {
107
                $request->attributes->set('_view', $rewrittenUrlData->view);
108
                if (null !== $rewrittenUrlData->viewId) {
109
                    $request->query->set($rewrittenUrlData->view . '_id', $rewrittenUrlData->viewId);
110
                }
111
            }
112
113
            if (null !== $rewrittenUrlData->locale) {
114
                $this->manageLocale($rewrittenUrlData, $request);
115
            }
116
117
118
            foreach ($rewrittenUrlData->otherParameters as $parameter => $value) {
119
                $request->query->set($parameter, $value);
120
            }
121
122
            return array(
123
                '_controller' => 'Thelia\\Controller\\Front\\DefaultController::noAction',
124
                '_route' => 'rewrite',
125
                '_rewritten' => true,
126
            );
127
        }
128
        throw new ResourceNotFoundException();
129
    }
130
131
    /**
132
     * @param $url
133
     * @return RewritingRedirectType
134
     */
135
    public function fetchRewritingRedirectTypeFromUrl($url)
136
    {
137
        return RewritingRedirectTypeQuery::create()
138
            ->joinRewritingUrl()
139
            ->useRewritingUrlQuery()
140
            ->filterByUrl($url)
141
            ->endUse()
142
            ->findOne();
143
    }
144
}
145