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 ( 27c796...fe01f0 )
by Jindřich
20s queued 11s
created

WebService::convertToSkautisException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 9.8666
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
     * @var array
28
     */
29
    protected $init;
30
31
    /**
32
     * @var SoapClient
33
     */
34
    protected $soapClient;
35
36
    /**
37
     * @param array $soapOpts Nastaveni SOAP requestu
38
     * @throws InvalidArgumentException pokud je odkaz na WSDL soubor prázdný
39
     */
40 3
    public function __construct(SoapClient $soapClient, array $soapOpts)
41
    {
42 3
        $this->init = $soapOpts;
43 3
        $this->soapClient = $soapClient;
44 3
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 2
    public function call(string $functionName, array $arguments = [])
50
    {
51 2
        return $this->soapCall($functionName, $arguments);
52
    }
53
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function __call(string $functionName, array $arguments)
59
    {
60 1
        return $this->call($functionName, $arguments);
61
    }
62
63
    /**
64
     * Metoda provadejici SOAP pozadavek na servery Skautisu
65
     *
66
     * @see http://php.net/manual/en/soapclient.soapcall.php
67
     *
68
     * @param string $functionName Nazev akce k provedeni na WebService
69
     * @param array $arguments ([0]=args [1]=cover)
70
     * @param array $options Nastaveni
71
     * @param array|null $inputHeaders Hlavicky pouzite pri odesilani
72
     * @param array $outputHeaders Hlavicky ktere prijdou s odpovedi
73
     *
74
     * @return mixed
75
     */
76 2
    protected function soapCall(
77
      string $functionName,
78
      array $arguments,
79
      array $options = [],
80
      ?array $inputHeaders = null,
81
      array &$outputHeaders = []
82
    ) {
83 2
        $fname = ucfirst($functionName);
84 2
        $args = $this->prepareArgs($fname, $arguments);
85
86 2
        if ($this->hasListeners()) {
87 2
            $query = new SkautisQuery($fname, $args, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
88
        }
89
90
        try {
91 2
            $soapResponse = $this->soapClient->__soapCall($fname, $args, $options, $inputHeaders, $outputHeaders);
92
93
            $soapResponse = $this->parseOutput($fname, $soapResponse);
94
95
            if (isset($query) && $this->hasListeners()) {
96
                $this->dispatch(self::EVENT_SUCCESS, $query->done($soapResponse));
97
            }
98
            return $soapResponse;
99 2
        } catch (SoapFault $e) {
100 2
            if (isset($query) && $this->hasListeners()) {
101 2
              $this->dispatch(self::EVENT_FAILURE, $query->done(null, $e));
102
            }
103
104 2
            throw $this->convertToSkautisException($e);
105
        }
106
    }
107
108
    /**
109
     * Z defaultnich parametru a parametru callu vytvori argumenty pro SoapClient::__soapCall
110
     *
111
     * @param string $functionName Jmeno funkce volane pres SOAP
112
     * @param array $arguments      Argumenty k mergnuti s defaultnimy
113
     *
114
     * @return array Argumenty pro SoapClient::__soapCall
115
     */
116 2
    protected function prepareArgs($functionName, array $arguments): array
117
    {
118 2
        if (!isset($arguments[0]) || !is_array($arguments[0])) {
119 2
            $arguments[0] = [];
120
        }
121
122
        //k argumentum připoji vlastni informace o aplikaci a uzivateli
123 2
        $args = array_merge($this->init, $arguments[0]);
124
125 2
        if (!isset($arguments[1]) || $arguments[1] === null) {
126 2
            $functionName = lcfirst($functionName);
127 2
            $args = [[$functionName . 'Input' => $args]];
128 2
            return $args;
129
        }
130
131
        //pokud je zadan druhy parametr tak lze prejmenovat obal dat
132
        $matches = explode('/', $arguments[1]);
133
        //pole se budou vytvaret zevnitr ven
134
        $matches = array_reverse($matches);
135
136
        $matches[] = 0; //zakladni obal 0=>...
137
138
        foreach ($matches as $value) {
139
            $args = [$value => $args];
140
        }
141
142
        return $args;
143
    }
144
145
    /**
146
     * Parsuje output ze SoapClient do jednotného formátu
147
     *
148
     * @param string $fname Jméno funkce volané přes SOAP
149
     * @param mixed $ret    Odpoveď ze SoapClient::__soapCall
150
     *
151
     * @return array
152
     */
153
    protected function parseOutput(string $fname, $ret): array
154
    {
155
        //pokud obsahuje Output tak vždy vrací pole i s jedním prvkem.
156
        $result = $ret->{$fname . 'Result'};
157
        if (!isset($result)) {
158
            return $ret;
159
        }
160
161
        $output = $result->{$fname . 'Output'};
162
        if (!isset($output)) {
163
            return $result; //neobsahuje $fname.Output
164
        }
165
166
        if ($output instanceof stdClass) { //vraci pouze jednu hodnotu misto pole?
167
            return [$output]; //vraci pole se stdClass
168
        }
169
170
        return $output; //vraci pole se stdClass
171
    }
172
173 2
    private function convertToSkautisException(SoapFault $e): WsdlException
174
    {
175 2
      if (preg_match('/Uživatel byl odhlášen/ui', $e->getMessage())) {
176
        return new AuthenticationException($e->getMessage(), $e->getCode(), $e);
177
      }
178
179 2
      if (preg_match('/Nemáte oprávnění/ui', $e->getMessage())) {
180
        return new PermissionException($e->getMessage(), $e->getCode(), $e);
181
      }
182
183 2
      return new WsdlException($e->getMessage(), $e->getCode(), $e);
184
    }
185
}
186