Completed
Push — master ( 52bda8...14effb )
by Tomas Norre
15:36
created

RestRequestHandler::canHandleRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
namespace Aoe\Restler\Http;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Restler\System\Dispatcher;
29
use Psr\Http\Message\ServerRequestInterface;
30
use TYPO3\CMS\Core\Core\Bootstrap;
31
use TYPO3\CMS\Core\Http\RequestHandlerInterface;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
34
35
/**
36
 * This is the main entry point for everything starting with /api/
37
 */
38
class RestRequestHandler implements RequestHandlerInterface
39
{
40
    /**
41
     * Instance of the current TYPO3 bootstrap
42
     * @var Bootstrap
43
     */
44
    protected $bootstrap;
45
46
    /**
47
     * The request handed over
48
     * @var \Psr\Http\Message\ServerRequestInterface
49
     */
50
    protected $request;
51
52
    /**
53
     * Constructor handing over the bootstrap
54
     *
55
     * @param Bootstrap $bootstrap
56
     */
57
    public function __construct(Bootstrap $bootstrap)
58
    {
59
        $this->bootstrap = $bootstrap;
60
    }
61
62
    /**
63
     * Handles a frontend request
64
     *
65
     * @param \Psr\Http\Message\ServerRequestInterface $request
66
     *
67
     * @return NULL|\Psr\Http\Message\ResponseInterface
68
     */
69
    public function handleRequest(\Psr\Http\Message\ServerRequestInterface $request)
70
    {
71
        // We define this constant, so that any TYPO3-Extension can check, if the REST-API is running
72
        define('REST_API_IS_RUNNING', true);
73
74
        // Dispatch the API-call
75
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
76
        $objectManager->get(Dispatcher::class)->dispatch();
77
    }
78
79
    /**
80
     * This request handler can handle any frontend request.
81
     *
82
     * @param ServerRequestInterface $request
83
     *
84
     * @return bool If the request is not an eID request, TRUE otherwise FALSE
85
     */
86
    public function canHandleRequest(ServerRequestInterface $request)
87
    {
88
        return $this->isRequestApi($request) || $this->isRequestApiExplorer($request);
89
    }
90
91
    /**
92
     * Returns the priority.
93
     *
94
     * Shows how eager the handler is to actually handle the
95
     * request. An integer > 0 means "I want to handle this request" where
96
     * "100" is default. "0" means "I am a fallback solution"
97
     *
98
     * @return int The priority of the request handler.
99
     */
100
    public function getPriority()
101
    {
102
        return 60;
103
    }
104
105
    /**
106
     * @param ServerRequestInterface $request
107
     *
108
     * @return bool
109
     */
110
    private function isRequestApi(ServerRequestInterface $request)
111
    {
112
        return strpos($request->getUri()->getPath(), '/api/') === 0;
113
    }
114
115
    /**
116
     * @param ServerRequestInterface $request
117
     *
118
     * @return bool
119
     */
120
    private function isRequestApiExplorer(ServerRequestInterface $request)
121
    {
122
        return strpos($request->getUri()->getPath(), '/api_explorer/') === 0;
123
    }
124
}