RequestHandler::getUploadHandler()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * RequestHandler.php - Jaxon Request Handler
5
 *
6
 * This class processes an incoming jaxon request.
7
 *
8
 * @package jaxon-core
9
 * @author Jared White
10
 * @author J. Max Wilson
11
 * @author Joseph Woolley
12
 * @author Steffen Konerow
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
20
21
namespace Jaxon\Request\Handler;
22
23
use Jaxon\Di\Container;
24
use Jaxon\Exception\RequestException;
25
use Jaxon\Plugin\Manager\PluginManager;
26
use Jaxon\Plugin\RequestHandlerInterface;
27
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
28
use Jaxon\Request\Upload\UploadHandlerInterface;
29
use Jaxon\Response\ResponseManager;
30
use Exception;
31
32
class RequestHandler
33
{
34
    /**
35
     * @var UploadHandlerInterface
36
     */
37
    private $xUploadHandler;
38
39
    /**
40
     * The request plugin that is able to process the current request
41
     *
42
     * @var RequestHandlerInterface
43
     */
44
    private $xRequestPlugin = null;
45
46
    /**
47
     * The constructor
48
     *
49
     * @param Container $di
50
     * @param PluginManager $xPluginManager
51
     * @param ResponseManager $xResponseManager
52
     * @param CallbackManager $xCallbackManager
53
     * @param DataBagPlugin $xDataBagPlugin
54
     */
55
    public function __construct(private Container $di, private PluginManager $xPluginManager,
56
        private ResponseManager $xResponseManager, private CallbackManager $xCallbackManager,
57
        private DataBagPlugin $xDataBagPlugin)
58
    {}
59
60
    /**
61
     * @return UploadHandlerInterface|null
62
     */
63
    private function getUploadHandler(): ?UploadHandlerInterface
64
    {
65
        return $this->xUploadHandler ?: $this->xUploadHandler = $this->di->getUploadHandler();
66
    }
67
68
    /**
69
     * Check if the current request can be processed
70
     *
71
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
72
     *
73
     * @return bool
74
     */
75
    public function canProcessRequest(): bool
76
    {
77
        // Return true if the request plugin was already found
78
        if($this->xRequestPlugin !== null)
79
        {
80
            return true;
81
        }
82
83
        // The HTTP request
84
        $xRequest = $this->di->getRequest();
85
86
        // Find a plugin to process the request
87
        foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
88
        {
89
            if($sClassName::canProcessRequest($xRequest))
90
            {
91
                $this->xRequestPlugin = $this->di->g($sClassName);
92
                $xTarget = $this->xRequestPlugin->setTarget($xRequest);
93
                $xTarget->setMethodArgs($this->di->getRequestArguments());
94
                return true;
95
            }
96
        }
97
        return false;
98
    }
99
100
    /**
101
     * Process the current request and handle errors and exceptions.
102
     *
103
     * @return void
104
     * @throws RequestException
105
     */
106
    private function _processRequest()
107
    {
108
        // The HTTP request
109
        $xRequest = $this->di->getRequest();
110
111
        // Process uploaded files, if the upload plugin is enabled
112
        $xUploadHandler = $this->getUploadHandler();
113
        if($xUploadHandler !== null && $xUploadHandler->canProcessRequest($xRequest))
114
        {
115
            $xUploadHandler->processRequest($xRequest);
116
        }
117
118
        // Process the request
119
        if(($this->xRequestPlugin))
120
        {
121
            $this->xRequestPlugin->processRequest();
122
            // Process the databag
123
            $this->xDataBagPlugin->writeCommand();
124
        }
125
    }
126
127
    /**
128
     * Process the current request.
129
     *
130
     * @return void
131
     * @throws RequestException
132
     */
133
    public function processRequest()
134
    {
135
        // Check if there is a plugin to process this request
136
        if(!$this->canProcessRequest())
137
        {
138
            return;
139
        }
140
141
        try
142
        {
143
            $bEndRequest = false;
144
            // Handle before processing event
145
            if(($this->xRequestPlugin))
146
            {
147
                $this->xCallbackManager->onBefore($this->xRequestPlugin->getTarget(), $bEndRequest);
148
            }
149
            if($bEndRequest)
150
            {
151
                return;
152
            }
153
154
            $this->_processRequest();
155
156
            // Handle after processing event
157
            if(($this->xRequestPlugin))
158
            {
159
                $this->xCallbackManager->onAfter($this->xRequestPlugin->getTarget(), $bEndRequest);
160
            }
161
        }
162
        // An exception was thrown while processing the request.
163
        // The request missed the corresponding handler function,
164
        // or an error occurred while attempting to execute the handler.
165
        catch(RequestException $e)
166
        {
167
            $this->xResponseManager->error($e->getMessage());
168
            $this->xCallbackManager->onInvalid($e);
169
        }
170
        catch(Exception $e)
171
        {
172
            $this->xResponseManager->error($e->getMessage());
173
            $this->xCallbackManager->onError($e);
174
        }
175
176
        // Print the debug messages
177
        $this->xResponseManager->printDebug();
178
    }
179
}
180