Passed
Push — master ( ced4f9...ace633 )
by Maciej
01:30
created

XmlRewritesRouter::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\ActionInterface;
15
use Magento\Framework\App\RequestInterface;
16
use Magento\Framework\App\RouterInterface;
17
use Magento\Framework\UrlInterface;
18
use MSlwk\XmlUrlRewrites\Api\RewritesProviderInterface;
19
20
/**
21
 * Class XmlRewritesRouter
22
 * @package MSlwk\XmlUrlRewrites\Controller
23
 */
24
class XmlRewritesRouter implements RouterInterface
25
{
26
    /**
27
     * @var \Magento\Framework\App\ActionFactory
28
     */
29
    private $actionFactory;
30
31
    /**
32
     * @var RewritesProviderInterface
33
     */
34
    private $rewritesProvider;
35
36
    /**
37
     * XmlRewritesRouter constructor.
38
     * @param \Magento\Framework\App\ActionFactory $actionFactory
39
     * @param RewritesProviderInterface $rewritesProvider
40
     */
41
    public function __construct(
42
        \Magento\Framework\App\ActionFactory $actionFactory,
43
        RewritesProviderInterface $rewritesProvider
44
    ) {
45
        $this->actionFactory = $actionFactory;
46
        $this->rewritesProvider = $rewritesProvider;
47
    }
48
49
    /**
50
     * @param RequestInterface $request
51
     * @return ActionInterface|null
52
     */
53
    public function match(RequestInterface $request)
54
    {
55
        $rewrites = $this->rewritesProvider->getRewrites();
56
        $path = trim($request->getPathInfo(), '/');
57
58
        if (isset($rewrites[$path])) {
59
            $request->setAlias(
60
                UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
61
                $path
62
            );
63
            $request->setPathInfo($rewrites[$path]);
64
            return $this->actionFactory->create(
65
                Forward::class
66
            );
67
        }
68
    }
69
}
70