Intraface_XMLRPC_Server::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 6
1
<?php
2
/**
3
 * Main XMLRPC server class to extend all other Serves from
4
 *
5
 * Gives ability to encode and decode data correct.
6
 * @category XMLRPC_Server
7
 * @package  Intraface_XMLRPC
8
 * @author   Sune Jensen <[email protected]>
9
 * @version  @package-version@
10
 */
11
12
/**
13
 * Main XMLRPC server class to extend all other Serves from
14
 *
15
 * Gives ability to encode and decode data correct.
16
 * @category XMLRPC_Server
17
 * @package  Intraface_XMLRPC
18
 * @author   Sune Jensen <[email protected]>
19
 * @version  @package-version@
20
 */
21 View Code Duplication
class Intraface_XMLRPC_Server
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    /**
24
     * @var struct $credentials
25
     */
26
    protected $credentials;
27
28
    /**
29
     * @var object $kernel intraface kernel
30
     */
31
    protected $kernel;
32
33
    protected $valid_encodings = array('utf-8', 'iso-8859-1');
34
35
    /**
36
     * Constructor
37
     *
38
     * @param string  $encoding The encoding wich the server recieves and returns data in
39
     *
40
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
     */
42
    public function __construct($encoding = 'utf-8')
43
    {
44
        if (!in_array($encoding, $this->valid_encodings)) {
45
            throw new Exception('Invalid encoding: '.$encoding.'. Should either be utf-8 or iso-8859');
46
        }
47
        $this->encoding = $encoding;
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
    }
49
50
    /**
51
     * Checks credentials
52
     *
53
     * @param struct $credentials Credentials to use the server
54
     *
55
     * @return array
56
     */
57
    protected function checkCredentials($credentials)
58
    {
59
        $this->credentials = $credentials;
60
61
        if (count($credentials) != 2) { // -4
62
            require_once 'XML/RPC2/Exception.php';
63
            throw new XML_RPC2_FaultException('wrong argument count in $credentials - got ' . count($credentials) . ' arguments - need 2', -4);
64
        }
65
        if (empty($credentials['private_key'])) { // -5
66
            require_once 'XML/RPC2/Exception.php';
67
            throw new XML_RPC2_FaultException('supply a private_key', -5);
68
        }
69
        if (empty($credentials['session_id'])) { // -5
70
            require_once 'XML/RPC2/Exception.php';
71
            throw new XML_RPC2_FaultException('supply a session_id', -5);
72
        }
73
74
        $auth_adapter = new Intraface_Auth_PrivateKeyLogin(MDB2::singleton(DB_DSN), $credentials['session_id'], $credentials['private_key']);
75
        $weblogin = $auth_adapter->auth();
76
77
        if (!$weblogin) {
78
            require_once 'XML/RPC2/Exception.php';
79
            throw new XML_RPC2_FaultException('access to intranet denied', -2);
80
        }
81
82
        $this->kernel = new Intraface_Kernel($credentials['session_id']);
83
        $this->kernel->weblogin = $weblogin;
0 ignored issues
show
Bug introduced by
The property weblogin does not seem to exist in Intraface_Kernel.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
84
        $this->kernel->intranet = new Intraface_Intranet($weblogin->getActiveIntranetId());
85
        $this->kernel->setting = new Intraface_Setting($this->kernel->intranet->get('id'));
86
87
        // makes intranet_id accessable in Doctrine
88
        Intraface_Doctrine_Intranet::singleton($this->kernel->intranet->getId());
89
90
        return true;
91
    }
92
93
    /**
94
     * Prepares response to be sent with the correct UTF-8 encoding.
95
     *
96
     * @param mixed $values Array or string to decode
97
     * @return mixed UTF8 decoded request
98
     */
99
    protected function prepareResponseData($values)
100
    {
101
        $values = $this->recursiveMap(array($this, 'handleNull'), $values);
102
        if ($this->encoding == 'utf-8') {
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
103
            return $this->recursiveMap('utf8_encode', $values);
104
        }
105
106
        return $values;
107
    }
108
109
    function handleNull($value)
110
    {
111
        if ($value === null) {
112
            return '';
113
        }
114
        return $value;
115
    }
116
117
    /**
118
     * Process data from client, so that data is returned with the correct encoding.
119
     *
120
     * @param mixed $values Array or string to decode
121
     * @return mixed correct encoded response
122
     */
123
    protected function processRequestData($values)
124
    {
125
        if ($this->encoding == 'utf-8') {
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
126
            return $this->recursiveMap('utf8_decode', $values);
127
        }
128
        return $values;
129
    }
130
131
    protected function recursiveMap($function, $values)
132
    {
133
        if (is_string($values)) {
134
            return call_user_func($function, $values);
135
        } elseif (is_null($values)) {
136
            return call_user_func($function, $values);
137
        } elseif (is_array($values)) {
138
            foreach ($values as $key => $value) {
139
                $values[$key] = $this->recursiveMap($function, $value);
140
            }
141
            return $values;
142
        } else {
143
            return $values;
144
        }
145
    }
146
}
147