Completed
Push — master ( dfd5cb...5cac00 )
by Tim
09:24
created

Classes/Service/JsonServer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Json server handling.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Autoloader\Service;
9
10
use HDNET\Autoloader\Exception;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Core\Utility\HttpUtility;
13
use TYPO3\CMS\Extbase\Object\ObjectManager;
14
use Zend\Json\Server\Server;
15
use Zend\Json\Server\Smd;
16
17
/**
18
 * Json server handling.
19
 */
20
class JsonServer
21
{
22
    /**
23
     * Server key.
24
     *
25
     * @var string
26
     */
27
    protected $serverKey = '';
28
29
    /**
30
     * Server class.
31
     *
32
     * @var string
33
     */
34
    protected $serverClass = '';
35
36
    /**
37
     * Check if the SMD should rendered.
38
     *
39
     * @var bool
40
     */
41
    protected $renderSmd = false;
42
43
    /**
44
     * Build up the object.
45
     *
46
     * @param string $server
47
     * @param bool   $smd
48
     */
49
    public function __construct($server, $smd)
50
    {
51
        $this->serverKey = $server;
52
        if (isset($GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Json'][$server])) {
53
            $this->serverClass = $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Json'][$server];
54
        }
55
56
        $this->renderSmd = (bool) $smd;
57
    }
58
59
    /**
60
     * Handle the request.
61
     *
62
     * @param $request
63
     *
64
     * @throws \Exception
65
     */
66
    public function handle($request)
67
    {
68
        if (!\class_exists(Server::class)) {
69
            throw new Exception('If you want to use the JSON server, please add \'"zendframework/zend-http": "2.*", "zendframework/zend-server": "2.*", "zendframework/zend-json": "2.*"\' to your root composer.json file.');
70
        }
71
72
        \header('Content-Type: application/json');
73
        if (!\class_exists($this->serverClass)) {
74
            $server = new Server();
75
            echo $server->fault(
76
                'No valid server class name for the given server key: "' . $this->serverKey . '"',
77
                2342358923745
78
            );
79
80
            return;
81
        }
82
83
        if ($this->renderSmd) {
84
            $this->renderSmd();
85
        } else {
86
            $this->handleRequest($request);
87
        }
88
    }
89
90
    /**
91
     * Handle the service request.
92
     *
93
     * @param $request
94
     */
95
    protected function handleRequest($request)
96
    {
97
        $server = new Server();
98
        $server->setRequest($request);
99
100
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
101
        $server->setClass($objectManager->get($this->serverClass));
102
        try {
103
            $server->handle();
104
        } catch (\Exception $ex) {
105
            echo $server->fault($ex->getMessage(), $ex->getCode());
106
        }
107
    }
108
109
    /**
110
     * Handle the SMD request.
111
     */
112
    protected function renderSmd()
113
    {
114
        $server = new Server();
115
        $server->setClass($this->serverClass);
116
117
        $smd = $server->getServiceMap();
118
        $smd->setTarget($this->getServiceUri());
119
        $smd->setEnvelope(Smd::ENV_JSONRPC_2);
120
121
        echo $smd;
122
    }
123
124
    /**
125
     * Get the Service URI.
126
     *
127
     * @return string
128
     */
129
    protected function getServiceUri()
130
    {
131
        $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
132
        $parts = \parse_url($uri);
133
        $parts['query'] = 'eID=JsonServer&server=' . $this->serverKey;
134
135
        return HttpUtility::buildUrl($parts);
0 ignored issues
show
It seems like $parts defined by \parse_url($uri) on line 132 can also be of type false; however, TYPO3\CMS\Core\Utility\HttpUtility::buildUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
136
    }
137
}
138