XmlRewritesRouter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A match() 0 16 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File: XmlRewritesRouter.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\XmlUrlRewrites\Controller;
12
13
use Magento\Framework\App\Action\Forward;
14
use Magento\Framework\App\ActionFactory;
15
use Magento\Framework\App\ActionInterface;
16
use Magento\Framework\App\RequestInterface;
17
use Magento\Framework\App\RouterInterface;
18
use Magento\Framework\UrlInterface;
19
use MSlwk\XmlUrlRewrites\Api\RewritesProviderInterface;
20
21
/**
22
 * Class XmlRewritesRouter
23
 * @package MSlwk\XmlUrlRewrites\Controller
24
 */
25
class XmlRewritesRouter implements RouterInterface
26
{
27
    /**
28
     * @var ActionFactory
29
     */
30
    private $actionFactory;
31
32
    /**
33
     * @var RewritesProviderInterface
34
     */
35
    private $rewritesProvider;
36
37
    /**
38
     * XmlRewritesRouter constructor.
39
     * @param ActionFactory $actionFactory
40
     * @param RewritesProviderInterface $rewritesProvider
41
     */
42
    public function __construct(
43
        ActionFactory $actionFactory,
44
        RewritesProviderInterface $rewritesProvider
45
    ) {
46
        $this->actionFactory = $actionFactory;
47
        $this->rewritesProvider = $rewritesProvider;
48
    }
49
50
    /**
51
     * @param RequestInterface $request
52
     * @return ActionInterface|null
53
     */
54
    public function match(RequestInterface $request)
55
    {
56
        $rewrites = $this->rewritesProvider->getRewrites();
57
        $path = trim($request->getPathInfo(), '/');
58
59
        if (isset($rewrites[$path])) {
60
            $request->setAlias(
61
                UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
62
                $path
63
            );
64
            $request->setPathInfo($rewrites[$path]);
65
            return $this->actionFactory->create(
66
                Forward::class
67
            );
68
        }
69
    }
70
}
71