Completed
Push — master ( 693002...4ea4f2 )
by Tim
08:26 queued 04:02
created

JsonServer::renderSmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Json server handling
5
 *
6
 * @author  Tim Lochmüller
7
 * @author  Tito Duarte <[email protected]>
8
 */
9
10
namespace HDNET\Autoloader\Service;
11
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Core\Utility\HttpUtility;
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
    /**
24
     * Server key
25
     *
26
     * @var string
27
     */
28
    protected $serverKey = '';
29
30
    /**
31
     * Server class
32
     *
33
     * @var string
34
     */
35
    protected $serverClass = '';
36
37
    /**
38
     * Check if the WSDL should rendered
39
     *
40
     * @var bool
41
     */
42
    protected $renderSmd = false;
43
44
    /**
45
     * Build up the object
46
     *
47
     * @param string $server
48
     * @param boolean $smd
49
     * @todo move to hook logic
50
     */
51
    public function __construct($server, $smd)
52
    {
53
        $this->serverKey = $server;
54
        if (isset($GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Json'][$server])) {
55
            $this->serverClass = $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Json'][$server];
56
        }
57
        $this->renderSmd = (bool)$smd;
58
    }
59
60
    /**
61
     * Handle the request
62
     */
63
    public function handle()
64
    {
65
        header('Content-Type: application/json');
66
        if (!class_exists($this->serverClass)) {
67
            $server = new Server();
68
            $server->fault(
69
                'No valid server class name for the given server key: "' . $this->serverClass . '"',
70
                2342358923745
71
            );
72
            return;
73
        }
74
        if ($this->renderSmd) {
75
            $this->renderSmd();
76
        } else {
77
            $this->handleRequest();
78
        }
79
    }
80
81
    /**
82
     * Handle the service request
83
     */
84
    protected function handleRequest()
85
    {
86
        $server = new Server();
87
88
        $server->setClass($this->serverClass);
89
        try {
90
            $server->handle();
91
        } catch (\Exception $ex) {
92
            $server->fault($ex->getMessage(), $ex->getCode());
93
        }
94
    }
95
96
    /**
97
     * Handle the SMD request
98
     */
99
    protected function renderSmd()
100
    {
101
        $server = new Server();
102
        $server->setClass($this->serverClass);
103
104
        $smd =  $server->getServiceMap();
105
        $smd->setTarget($this->getServiceUri());
106
        $smd->setEnvelope(Smd::ENV_JSONRPC_2);
107
        
108
        echo $smd;
109
    }
110
111
    /**
112
     * Get the Service URI
113
     *
114
     * @return string
115
     */
116
    protected function getServiceUri()
117
    {
118
        $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
119
        $parts = parse_url($uri);
120
        $parts['query'] = 'eID=JsonServer&amp;server=' . $this->serverKey;
121
        return HttpUtility::buildUrl($parts);
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($uri) on line 119 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...
122
    }
123
}
124