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 — 3.x (#86)
by
unknown
05:19
created

WebService::prepareArgs()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.9683

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 26
ccs 8
cts 14
cp 0.5714
crap 6.9683
rs 9.1928
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Skautis\Wsdl;
5
6
use Skautis\EventDispatcher\EventDispatcherTrait;
7
use Skautis\InvalidArgumentException;
8
use Skautis\SkautisQuery;
9
use SoapFault;
10
use stdClass;
11
use SoapClient;
12
13
/**
14
 * @author Hána František <[email protected]>
15
 */
16
class WebService implements WebServiceInterface
17
{
18
19
    use EventDispatcherTrait;
20
21
    public const EVENT_SUCCESS = 'success';
22
    public const EVENT_FAILURE = 'failure';
23
24
    /**
25
     * základní údaje volané při každém požadavku
26
     * ID_Application, ID_Login
27
     *
28
     * @var array<string, mixed>
29
     */
30
    protected $init;
31
32
    /**
33
     * @var SoapClient
34
     */
35
    protected $soapClient;
36
37
    /**
38
     * @param mixed $wsdl Odkaz na WSDL soubor
39
     * @param array<string, mixed> $soapOpts Nastaveni SOAP requestu
40
     * Ma pouzivat kompresi na prenasena data?
41
     * @throws InvalidArgumentException pokud je odkaz na WSDL soubor prázdný
42
     */
43 4
    public function __construct($wsdl, array $soapOpts)
44
    {
45 4
        $this->init = $soapOpts;
46 4
        if (empty($wsdl)) {
47 1
            throw new InvalidArgumentException('WSDL address cannot be empty.');
48
        }
49
50 3
        $this->soapClient = new SoapClient($wsdl, $soapOpts);
51 3
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 2
    public function call(string $functionName, array $arguments = [])
57
    {
58 2
        return $this->soapCall($functionName, $arguments);
59
    }
60
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function __call(string $functionName, array $arguments)
66
    {
67 1
        return $this->call($functionName, $arguments);
68
    }
69
70
    /**
71
     * Metoda provadejici SOAP pozadavek na servery Skautisu
72
     *
73
     * @see http://php.net/manual/en/soapclient.soapcall.php
74
     *
75
     * @param string $function_name Nazev akce k provedeni na WebService
76
     * @param array<int|string, mixed> $arguments ([0]=args [1]=cover)
77
     * @param array<string, mixed> $options Nastaveni
78
     * @param array<int, string> $input_headers Hlavicky pouzite pri odesilani
79
     * @param array<int, string> $output_headers Hlavicky ktere prijdou s odpovedi
80
     * @return mixed
81
     */
82 2
    protected function soapCall(
83
      string $function_name,
84
      array $arguments,
85
      array $options = [],
86
      array $input_headers = [],
87
      array &$output_headers = []
88
    ) {
89 2
        $fname = ucfirst($function_name);
90 2
        $args = $this->prepareArgs($fname, $arguments);
91
92 2
        if ($this->hasListeners()) {
93 2
            $query = new SkautisQuery($fname, $args, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
94
        }
95
96
        try {
97 2
            $soapResponse = $this->soapClient->__soapCall($fname, $args, $options, $input_headers, $output_headers);
98
99
            $soapResponse = $this->parseOutput($fname, $soapResponse);
100
101
            if (isset($query) && $this->hasListeners()) {
102
                $this->dispatch(self::EVENT_SUCCESS, $query->done($soapResponse));
103
            }
104
            return $soapResponse;
105 2
        } catch (SoapFault $e) {
106 2
            if (isset($query) && $this->hasListeners()) {
107 2
                $this->dispatch(self::EVENT_FAILURE, $query->done(null, $e));
108
            }
109 2
            if (preg_match('/Uživatel byl odhlášen/', $e->getMessage())) {
110
                throw new AuthenticationException($e->getMessage(), $e->getCode(), $e);
111
            }
112 2
            if (preg_match('/Nemáte oprávnění/', $e->getMessage())) {
113
                throw new PermissionException($e->getMessage(), $e->getCode(), $e);
114
            }
115 2
            throw new WsdlException($e->getMessage(), $e->getCode(), $e);
116
        }
117
    }
118
119
    /**
120
     * Z defaultnich parametru a parametru callu vytvori argumenty pro SoapClient::__soapCall
121
     *
122
     * @param string $function_name Jmeno funkce volane pres SOAP
123
     * @param array<int|string, mixed> $arguments Argumenty k mergnuti s defaultnimy
124
     *
125
     * @return array<int, mixed> Argumenty pro SoapClient::__soapCall
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
     */
127 2
    protected function prepareArgs(string $function_name, array $arguments): array
128
    {
129 2
        if (!isset($arguments[0]) || !is_array($arguments[0])) {
130 2
            $arguments[0] = [];
131
        }
132
133 2
        $args = array_merge($this->init, $arguments[0]); //k argumentum připoji vlastni informace o aplikaci a uzivateli
134
135 2
        if (!isset($arguments[1])) {
136 2
            $function_name = strtolower(substr($function_name, 0, 1)) . substr($function_name, 1); //nahrazuje lcfirst
137 2
            $args = [[$function_name . 'Input' => $args]];
138 2
            return $args;
139
        }
140
141
        //pokud je zadan druhy parametr tak lze prejmenovat obal dat
142
        $matches = explode('/', $arguments[1]); //rozdeli to na stringy podle /
143
        $matches = array_reverse($matches); //pole se budou vytvaret zevnitr ven
144
145
        $matches[] = 0; //zakladni obal 0=>...
146
147
        foreach ($matches as $value) {
148
            $args = [$value => $args];
149
        }
150
151
        return $args;
152
    }
153
154
    /**
155
     * Parsuje output ze SoapClient do jednotného formátu
156
     *
157
     * @param string $fname Jméno funkce volané přes SOAP
158
     * @param mixed $ret    Odpoveď ze SoapClient::__soapCall
159
     *
160
     * @return array<int|string, mixed>
0 ignored issues
show
Documentation introduced by
The doc-type array<int|string, could not be parsed: Expected ">" at position 7, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
161
     */
162
    protected function parseOutput(string $fname, $ret): array
163
    {
164
        //pokud obsahuje Output tak vždy vrací pole i s jedním prvkem.
165
        if (!isset($ret->{$fname . "Result"})) {
166
            return $ret;
167
        }
168
169
        if (!isset($ret->{$fname . "Result"}->{$fname . "Output"})) {
170
            return $ret->{$fname . "Result"}; //neobsahuje $fname.Output
171
        }
172
173
        if ($ret->{$fname . "Result"}->{$fname . "Output"} instanceof stdClass) { //vraci pouze jednu hodnotu misto pole?
174
            return [$ret->{$fname . "Result"}->{$fname . "Output"}]; //vraci pole se stdClass
175
        }
176
177
        return $ret->{$fname . "Result"}->{$fname . "Output"}; //vraci pole se stdClass
178
    }
179
}
180