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 ( 52ff30...48a2e9 )
by Jindřich
12s queued 11s
created

WsdlManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 79.41%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 27
cts 34
cp 0.7941
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConfig() 0 4 1
A getWebService() 0 12 2
A createWebService() 0 13 3
A getWebServiceUrl() 0 8 2
A isMaintenance() 0 18 2
A addWebServiceListener() 0 7 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Skaut\Skautis\Wsdl;
5
6
use Skaut\Skautis\Config;
7
use Skaut\Skautis\EventDispatcher\EventDispatcherInterface;
8
use Skaut\Skautis\User;
9
use Skaut\Skautis\SkautisQuery;
10
11
/**
12
 * Třída pro správu webových služeb SkautISu
13
 */
14
class WsdlManager
15
{
16
17
    /**
18
     * @var WebServiceFactoryInterface
19
     */
20
    protected $webServiceFactory;
21
22
    /**
23
     * @var Config
24
     */
25
    protected $config;
26
27
    /**
28
     * @var array<int, array{eventName: string, callback: (callable(SkautisQuery): void)}>
29
     */
30
    protected $webServiceListeners = [];
31
32
    /**
33
     * Pole aktivních webových služeb
34
     *
35
     * @var array<string, WebServiceInterface>
36
     */
37
    protected $webServices = [];
38
39
40
    /**
41
     * @param WebServiceFactoryInterface $webServiceFactory továrna pro vytváření objektů webových služeb
42
     * @param Config $config
43
     */
44 8
    public function __construct(WebServiceFactoryInterface $webServiceFactory, Config $config)
45
    {
46 8
        $this->webServiceFactory = $webServiceFactory;
47 8
        $this->config = $config;
48 8
    }
49
50 2
    public function getConfig(): Config
51
    {
52 2
        return $this->config;
53
    }
54
55
    /**
56
     * Získá objekt webové služby
57
     *
58
     * @param string $name celé jméno webové služby
59
     * @param string|null $loginId skautIS login token
60
     */
61 3
    public function getWebService(string $name, ?string $loginId = null): WebServiceInterface
62
    {
63 3
        $key = $loginId . '_' . $name;
64
65 3
        if (!isset($this->webServices[$key])) {
66 2
            $options = $this->config->getSoapOptions();
67 2
            $options[User::ID_LOGIN] = $loginId;
68 2
            $this->webServices[$key] = $this->createWebService($name, $options);
69
        }
70
71 3
        return $this->webServices[$key];
72
    }
73
74
    /**
75
     * Vytváří objekt webové služby
76
     *
77
     * @param string $name jméno webové služby
78
     * @param array<string, mixed> $options volby pro SoapClient
79
     */
80 2
    public function createWebService(string $name, array $options = []): WebServiceInterface
81
    {
82 2
        $webService = $this->webServiceFactory->createWebService($this->getWebServiceUrl($name), $options);
83
84 2
        if ($webService instanceof EventDispatcherInterface) {
85
            // Zaregistruj listenery na vytvořeném objektu webové služby, pokud je to podporováno
86 2
            foreach ($this->webServiceListeners as $listener) {
87
                $webService->subscribe($listener['eventName'], $listener['callback']);
88
            }
89
        }
90
91 2
        return $webService;
92
    }
93
94
    /**
95
     * Vrací URL webové služby podle jejího jména
96
     */
97 6
    protected function getWebServiceUrl(string $name): string
98
    {
99 6
        if (!WebServiceName::isValidServiceName($name)) {
100
          throw new WsdlException("Web service '$name' not found.");
101
        }
102
103 6
        return $this->config->getBaseUrl() . 'JunakWebservice/' . rawurlencode($name) . '.asmx?WSDL';
104
    }
105
106 4
    public function isMaintenance(): bool
107
    {
108
        // Transformuje PHP error/warning do Exception
109
        // Funkce get_headers totiž používá warning když má problém aby vysvětlil co se děje
110
        // Pokud například DNS selže tak to hodí PHP warning, který nejde chytat jako exception
111
        set_error_handler(function($errno, $errstr, $errfile, $errline) {
112 1
          throw new MaintenanceErrorException($errstr, $errno, $errfile, $errline);
113 4
        });
114
115
        try {
116 4
          $headers = get_headers($this->getWebServiceUrl('UserManagement'));
117
118 3
          return !$headers || !in_array('HTTP/1.1 200 OK', $headers, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
119
        }
120
        finally {
121 4
          restore_error_handler();
122
        }
123
    }
124
125
    /**
126
     * Přidá listener na spravovaných vytvářených webových služeb.
127
     *
128
     * @param callable(SkautisQuery): void $callback
129
     */
130
    public function addWebServiceListener(string $eventName, callable $callback): void
131
    {
132
        $this->webServiceListeners[] = [
133
            'eventName' => $eventName,
134
            'callback' => $callback,
135
        ];
136
    }
137
}
138