GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — 2.x (#59)
by
unknown
04:21
created

WebService::resolveException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.0261
1
<?php
2
3
namespace Skautis\Wsdl;
4
5
use Skautis\EventDispatcher\EventDispatcherTrait;
6
use Skautis\InvalidArgumentException;
7
use Skautis\SkautisQuery;
8
use SoapFault;
9
use stdClass;
10
use SoapClient;
11
12
/**
13
 * @author Hána František <[email protected]>
14
 */
15
class WebService implements WebServiceInterface
16
{
17
18
    use EventDispatcherTrait;
19
20
    const EVENT_SUCCESS = 1;
21
    const EVENT_FAILURE = 2;
22
23
    /**
24
     * základní údaje volané při každém požadavku
25
     * ID_Application, ID_Login
26
     * @var array
27
     */
28
    protected $init;
29
30
    /**
31
     * @var SoapClient
32
     */
33
    protected $soapClient;
34
35
    private $exceptions = [
36
        'EventFunction_LeaderMustBeAdult' => 'Skautis\Wsdl\Event\LeaderNotAdultException',
37
        'EventFunction_AssistantMustBeAdult' => 'Skautis\Wsdl\Event\AssistantNotAdultException',
38
    ];
39
40
    /**
41
     * @param mixed $wsdl Odkaz na WSDL soubor
42
     * @param array $soapOpts Nastaveni SOAP requestu
43
     * Ma pouzivat kompresi na prenasena data?
44
     * @throws InvalidArgumentException pokud je odkaz na WSDL soubor prázdný
45
     */
46 3
    public function __construct($wsdl, array $soapOpts)
47
    {
48 3
        $this->init = $soapOpts;
49 3
        if (empty($wsdl)) {
50 1
            throw new InvalidArgumentException("WSDL address cannot be empty.");
51
        }
52
53 2
        $this->soapClient = new SoapClient($wsdl, $soapOpts);
54 2
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 2
    public function call($functionName, array $arguments = [])
60
    {
61 2
        return $this->soapCall($functionName, $arguments);
62
    }
63
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function __call($functionName, $arguments)
69
    {
70 1
        return $this->call($functionName, $arguments);
71
    }
72
73
    /**
74
     * Metoda provadejici SOAP pozadavek na servery Skautisu
75
     *
76
     * @see http://php.net/manual/en/soapclient.soapcall.php
77
     *
78
     * @param string $function_name Nazev akce k provedeni na WebService
79
     * @param array $arguments ([0]=args [1]=cover)
80
     * @param array $options Nastaveni
81
     * @param mixed $input_headers Hlavicky pouzite pri odesilani
82
     * @param array $output_headers Hlavicky ktere prijdou s odpovedi
83
     * @return mixed
84
     */
85 2
    protected function soapCall($function_name, $arguments, array $options = [], $input_headers = null, array &$output_headers = [])
86
    {
87 2
        $fname = ucfirst($function_name);
88 2
        $args = $this->prepareArgs($fname, $arguments);
89
90 2
        if ($this->hasListeners()) {
91 2
            $query = new SkautisQuery($fname, $args, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
92 2
        }
93
94
        try {
95 2
            $soapResponse = $this->soapClient->__soapCall($fname, $args, $options, $input_headers, $output_headers);
96
97
            $soapResponse = $this->parseOutput($fname, $soapResponse);
98
99
            if ($this->hasListeners()) {
100
                $this->dispatch(self::EVENT_SUCCESS, $query->done($soapResponse));
101
            }
102
            return $soapResponse;
103 2
        } catch (SoapFault $e) {
104 2
            if ($this->hasListeners()) {
105 2
                $this->dispatch(self::EVENT_FAILURE, $query->done(null, $e));
106 2
            }
107 2
            if (preg_match('/Uživatel byl odhlášen/', $e->getMessage())) {
108
                throw new AuthenticationException($e->getMessage(), $e->getCode(), $e);
109
            }
110 2
            if (preg_match('/Nemáte oprávnění/', $e->getMessage())) {
111
                throw new PermissionException($e->getMessage(), $e->getCode(), $e);
112
            }
113
114 2
            $this->resolveException($e);
115
        }
116
    }
117
118 2
    private function resolveException(SoapFault $e)
119
    {
120 2
        $message = $e->getMessage();
121 2
        foreach($this->exceptions as $error => $exception) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
122 2
            if(strpos($message, $error) !== FALSE) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
123
                throw new $exception($e->getMessage(), $e->getCode(), $e);
124
            }
125 2
        }
126
127 2
        throw new WsdlException($e->getMessage(), $e->getCode(), $e);
128
    }
129
130
    /**
131
     * Z defaultnich parametru a parametru callu vytvori argumenty pro SoapClient::__soapCall
132
     *
133
     * @param string $function_name Jmeno funkce volane pres SOAP
134
     * @param array $arguments      Argumenty k mergnuti s defaultnimy
135
     *
136
     * @return array Argumenty pro SoapClient::__soapCall
137
     */
138 2
    protected function prepareArgs($function_name, array $arguments)
139
    {
140 2
        if (!isset($arguments[0]) || !is_array($arguments[0])) {
141 2
            $arguments[0] = [];
142 2
        }
143
144 2
        $args = array_merge($this->init, $arguments[0]); //k argumentum připoji vlastni informace o aplikaci a uzivateli
145
146 2
        if (!isset($arguments[1]) || $arguments[1] === null) {
147 2
            $function_name = strtolower(substr($function_name, 0, 1)) . substr($function_name, 1); //nahrazuje lcfirst
148 2
            $args = [[$function_name . "Input" => $args]];
149 2
            return $args;
150
        }
151
152
        //pokud je zadan druhy parametr tak lze prejmenovat obal dat
153
        $matches = preg_split('~/~', $arguments[1]); //rozdeli to na stringy podle /
154
        $matches = array_reverse($matches); //pole se budou vytvaret zevnitr ven
155
156
        $matches[] = 0; //zakladni obal 0=>...
157
158
        foreach ($matches as $value) {
159
            $args = [$value => $args];
160
        }
161
162
        return $args;
163
    }
164
165
    /**
166
     * Parsuje output ze SoapClient do jednotného formátu
167
     *
168
     * @param string $fname Jméno funkce volané přes SOAP
169
     * @param mixed $ret    Odpoveď ze SoapClient::__soapCall
170
     *
171
     * @return array
172
     */
173
    protected function parseOutput($fname, $ret)
174
    {
175
        //pokud obsahuje Output tak vždy vrací pole i s jedním prvkem.
176
        if (!isset($ret->{$fname . "Result"})) {
177
            return $ret;
178
        }
179
180
        if (!isset($ret->{$fname . "Result"}->{$fname . "Output"})) {
181
            return $ret->{$fname . "Result"}; //neobsahuje $fname.Output
182
        }
183
184
        if ($ret->{$fname . "Result"}->{$fname . "Output"} instanceof stdClass) { //vraci pouze jednu hodnotu misto pole?
185
            return [$ret->{$fname . "Result"}->{$fname . "Output"}]; //vraci pole se stdClass
186
        }
187
188
        return $ret->{$fname . "Result"}->{$fname . "Output"}; //vraci pole se stdClass
189
    }
190
}
191