Completed
Push — master ( b06cc9...501e50 )
by
unknown
13:23
created

RequestHandlerResolver::resolveRequestHandler()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 22
rs 9.2222
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Extbase\Mvc;
17
18
use Psr\Container\ContainerInterface;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Configuration\RequestHandlersConfigurationFactory;
21
22
/**
23
 * Analyzes the raw request and delivers a request handler which can handle it.
24
 * @internal only to be used within Extbase, not part of TYPO3 Core API.
25
 */
26
class RequestHandlerResolver
27
{
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var \TYPO3\CMS\Extbase\Configuration\RequestHandlersConfiguration
35
     */
36
    private $requestHandlersConfiguration;
37
38
    /**
39
     * @param ContainerInterface $container
40
     * @param RequestHandlersConfigurationFactory $requestHandlersConfigurationFactory
41
     * @throws \TYPO3\CMS\Extbase\Configuration\Exception
42
     */
43
    public function __construct(ContainerInterface $container, RequestHandlersConfigurationFactory $requestHandlersConfigurationFactory)
44
    {
45
        $this->container = $container;
46
        $this->requestHandlersConfiguration = $requestHandlersConfigurationFactory->createRequestHandlersConfiguration();
47
    }
48
49
    /**
50
     * Analyzes the raw request and tries to find a request handler which can handle
51
     * it. If none is found, an exception is thrown.
52
     *
53
     * @return \TYPO3\CMS\Extbase\Mvc\RequestHandlerInterface A request handler
54
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception
55
     */
56
    public function resolveRequestHandler()
57
    {
58
        $suitableRequestHandlers = [];
59
        foreach ($this->requestHandlersConfiguration->getRegisteredRequestHandlers() as $requestHandlerClassName) {
60
            /** @var RequestHandlerInterface $requestHandler */
61
            $requestHandler = $this->container->has($requestHandlerClassName)
62
                ? $this->container->get($requestHandlerClassName)
63
                : GeneralUtility::makeInstance($requestHandlerClassName)
64
            ;
65
            if ($requestHandler->canHandleRequest()) {
66
                $priority = $requestHandler->getPriority();
67
                if (isset($suitableRequestHandlers[$priority])) {
68
                    throw new Exception('More than one request handler with the same priority can handle the request, but only one handler may be active at a time!', 1176475350);
69
                }
70
                $suitableRequestHandlers[$priority] = $requestHandler;
71
            }
72
        }
73
        if (empty($suitableRequestHandlers)) {
74
            throw new Exception('No suitable request handler found.', 1205414233);
75
        }
76
        ksort($suitableRequestHandlers);
77
        return array_pop($suitableRequestHandlers);
78
    }
79
}
80