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
Push — 3.x ( 48a2e9...c23789 )
by Jindřich
20s queued 11s
created

WebService::soapCall()   A

Complexity

Conditions 5
Paths 24

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.675

Importance

Changes 0
Metric Value
cc 5
nc 24
nop 5
dl 0
loc 38
ccs 14
cts 20
cp 0.7
crap 5.675
rs 9.0008
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Skaut\Skautis\Wsdl;
5
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Skaut\Skautis\InvalidArgumentException;
8
use Skaut\Skautis\Wsdl\Event\RequestFailEvent;
9
use Skaut\Skautis\Wsdl\Event\RequestPostEvent;
10
use Skaut\Skautis\Wsdl\Event\RequestPreEvent;
11
use SoapClient;
12
use stdClass;
13
use Throwable;
14
15
/**
16
 * @author Hána František <[email protected]>
17
 */
18
class WebService implements WebServiceInterface
19
{
20
21
    /**
22
     * základní údaje volané při každém požadavku
23
     * ID_Application, ID_Login
24
     *
25
     * @var array<string, mixed>
26
     */
27
    protected $init;
28
29
    /**
30
     * @var SoapClient
31
     */
32
    protected $soapClient;
33
34
    /**
35
     * @var EventDispatcherInterface|null
36
     */
37
    private $eventDispatcher;
38
39
    /**
40
     * @param array<string, mixed> $soapOpts Nastaveni SOAP requestu
41
     * @throws InvalidArgumentException pokud je odkaz na WSDL soubor prázdný
42
     */
43 2
    public function __construct(
44
      SoapClient $soapClient,
45
      array $soapOpts,
46
      ?EventDispatcherInterface $eventDispatcher
47
    ) {
48 2
        $this->init = $soapOpts;
49 2
        $this->soapClient = $soapClient;
50 2
        $this->eventDispatcher = $eventDispatcher;
51 2
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 1
    public function call(string $functionName, array $arguments = [])
57
    {
58 1
        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 $functionName 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> $inputHeaders Hlavicky pouzite pri odesilani
79
     * @param array<int, string> $outputHeaders Hlavicky ktere prijdou s odpovedi
80
     * @return mixed
81
     */
82 1
    protected function soapCall(
83
      string $functionName,
84
      array $arguments,
85
      array $options = [],
86
      array $inputHeaders = [],
87
      array &$outputHeaders = []
88
    ) {
89 1
        $fname = ucfirst($functionName);
90 1
        $args = $this->prepareArgs($fname, $arguments);
91
92 1
        if ($this->eventDispatcher !== null) {
93 1
            $event = new RequestPreEvent($fname, $args, $options, $inputHeaders, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
94 1
            $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Skaut\Skautis\Wsdl\Event\RequestPreEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        }
96
97 1
        $requestStart = microtime(true);
98
        try {
99 1
            $soapResponse = $this->soapClient->__soapCall($fname, $args, $options, $inputHeaders, $outputHeaders);
100
            $soapResponse = $this->parseOutput($fname, $soapResponse);
101
102
            if ($this->eventDispatcher !== null) {
103
                $duration = microtime(true) - $requestStart;
104
                $event = new RequestPostEvent($fname, $args, $soapResponse, $duration);
105
                $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Skaut\Skautis\Wsdl\Event\RequestPostEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            }
107
108
            return $soapResponse;
109 1
        } catch (Throwable $t) {
110
111 1
            if ($this->eventDispatcher !== null) {
112 1
              $duration = microtime(true) - $requestStart;
113 1
              $event = new RequestFailEvent($fname, $args, $t, $duration);
114 1
              $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Skaut\Skautis\Wsdl\Event\RequestFailEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
            }
116
117 1
            throw $this->convertToSkautisException($t);
118
        }
119
    }
120
121
    /**
122
     * Z defaultnich parametru a parametru callu vytvori argumenty pro SoapClient::__soapCall
123
     *
124
     * @param string $functionName Jmeno funkce volane pres SOAP
125
     * @param array<int|string, mixed> $arguments Argumenty k mergnuti s defaultnimy
126
     *
127
     * @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...
128
     */
129 1
    protected function prepareArgs(string $functionName, array $arguments): array
130
    {
131 1
        if (!isset($arguments[0]) || !is_array($arguments[0])) {
132 1
            $arguments[0] = [];
133
        }
134
135
        //k argumentum připoji vlastni informace o aplikaci a uzivateli
136 1
        $args = array_merge($this->init, $arguments[0]);
137
138 1
        if (!isset($arguments[1])) {
139 1
            $functionName = lcfirst($functionName);
140 1
            $args = [[$functionName . 'Input' => $args]];
141 1
            return $args;
142
        }
143
144
        //pokud je zadan druhy parametr tak lze prejmenovat obal dat
145
        $matches = explode('/', $arguments[1]);
146
        //pole se budou vytvaret zevnitr ven
147
        $matches = array_reverse($matches);
148
149
        $matches[] = 0; //zakladni obal 0=>...
150
151
        foreach ($matches as $value) {
152
            $args = [$value => $args];
153
        }
154
155
        return $args;
156
    }
157
158
    /**
159
     * Parsuje output ze SoapClient do jednotného formátu
160
     *
161
     * @param string $fname Jméno funkce volané přes SOAP
162
     * @param mixed $ret    Odpoveď ze SoapClient::__soapCall
163
     *
164
     * @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...
165
     */
166
    protected function parseOutput(string $fname, $ret): array
167
    {
168
        //pokud obsahuje Output tak vždy vrací pole i s jedním prvkem.
169
        $result = $ret->{$fname . 'Result'};
170
        if (!isset($result)) {
171
            return $ret;
172
        }
173
174
        $output = $result->{$fname . 'Output'};
175
        if (!isset($output)) {
176
            return $result; //neobsahuje $fname.Output
177
        }
178
179
        if ($output instanceof stdClass) { //vraci pouze jednu hodnotu misto pole?
180
            return [$output]; //vraci pole se stdClass
181
        }
182
183
        return $output; //vraci pole se stdClass
184
    }
185
186 1
    private function convertToSkautisException(Throwable $e): WsdlException
187
    {
188 1
      if (preg_match('/Uživatel byl odhlášen/ui', $e->getMessage())) {
189
        return new AuthenticationException($e->getMessage(), $e->getCode(), $e);
190
      }
191
192 1
      if (preg_match('/Nemáte oprávnění/ui', $e->getMessage())) {
193
        return new PermissionException($e->getMessage(), $e->getCode(), $e);
194
      }
195
196 1
      return new WsdlException($e->getMessage(), $e->getCode(), $e);
197
    }
198
}
199