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 ( d805b4...27c796 )
by Jindřich
01:53
created

WsdlManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 79.41%

Importance

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

7 Methods

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