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.
Test Setup Failed
Push — master ( fe4962...3aa77f )
by Thijs
04:04
created

SoapFactory::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace WebservicesNl\Protocol\Soap\Client;
4
5
use GuzzleHttp\Client;
6
use WebservicesNl\Common\Endpoint\Manager;
7
use WebservicesNl\Common\Exception\Client\InputException;
8
use WebservicesNl\Common\Exception\Server\NoServerAvailableException;
9
use WebservicesNl\Connector\Client\AbstractClientFactory;
10
use WebservicesNl\Platform\PlatformConfigInterface;
11
use WebservicesNl\Protocol\Soap\Config\ConfigFactory as SoapConfigFactory;
12
use WebservicesNl\Protocol\Soap\Helper\GuzzleClientFactory;
13
14
/**
15
 * Class SoapFactory.
16
 * Managing class (factory) for creating a PHP SoapClient for a given platform (mainly webservices)
17
 */
18
class SoapFactory extends AbstractClientFactory
19
{
20
    /**
21
     * @var SoapConfig
22
     */
23
    private $config;
24
25
    /**
26
     * SoapBuilder constructor.
27
     *
28
     * @param PlatformConfigInterface $platformConfig
29
     *
30
     * @throws InputException
31
     */
32
    public function __construct(PlatformConfigInterface $platformConfig)
33
    {
34
        // convert a Platform config into a SoapConfig
35
        $this->config = SoapConfigFactory::config($platformConfig);
36
    }
37
38
    /**
39
     * Static function (LSB) for building this class.
40
     *
41
     * @param PlatformConfigInterface $platformConfig  PlatformConfig to create soapConfig Object
42
     *
43
     * @throws InputException
44
     * @return static
45
     */
46
    public static function build(PlatformConfigInterface $platformConfig)
47
    {
48
        return new static($platformConfig);
49
    }
50
51
    /**
52
     * Creates a SoapClient with configured SoapConfig object and given additional settings.
53
     *
54
     * @param array $settings additional settings go here
55
     *
56
     * @return SoapClient
57
     * @throws InputException
58
     * @throws NoServerAvailableException
59
     * @throws \InvalidArgumentException
60
     */
61
    public function create(array $settings = [])
62
    {
63
        // create soap settings, with given settings and platform settings
64
        $soapSettings = SoapSettings::loadFromArray($settings);
65
66
        // create a manager for endpoint management
67
        $manager = $this->createEndpointManager($settings);
68
69
        // replace the native soap client transport with a curl client
70
71
        $curlClient = $this->createCurlClient($soapSettings->toArray(), $manager);
72
73
        // build a SoapClient (extends the native soap client)
74
        $soapClient = new SoapClient($soapSettings, $manager, $curlClient);
75
76
        // set custom headers
77
        $soapHeaders = array_key_exists('soapHeaders', $settings) ?
78
            array_merge($settings['soapHeaders'], $this->config->getSoapHeaders()) : $this->config->getSoapHeaders();
79
80
        $soapClient->__setSoapHeaders($soapHeaders);
81
        if ($this->config->hasConverter()) {
82
            $soapClient->setConverter($this->config->getConverter());
83
        }
84
85
        if ($this->hasLogger() === true) {
86
            $soapClient->setLogger($this->logger);
87
            $this->logger->info(
88
                'Creating a SoapClient to connect to platform ' . $this->config->getPlatformConfig()->getPlatformName()
89
            );
90
            $this->logger->debug('Created SoapClient', ['SoapClient' => print_r($soapClient, true)]);
91
            $this->logger->debug('Settings', ['settings' => (array)$settings]);
92
        }
93
94
        return $soapClient;
95
    }
96
97
    /**
98
     * Configure PSR-7 guzzle client for this soap factory.
99
     *
100
     * @param array   $settings settings with extra guzzle settings
101
     * @param Manager $manager  endpoint Manager
102
     *
103
     * @return Client
104
     * @throws \WebservicesNl\Common\Exception\Server\NoServerAvailableException
105
     */
106
    private function createCurlClient(array $settings, Manager $manager)
107
    {
108
        $settings['url'] = (string)$manager->getActiveEndpoint()->getUri();
109
110
        return GuzzleClientFactory::build($this->config->getPlatformConfig(), $this->logger)->create($settings);
111
    }
112
113
    /**
114
     * Creates and configures a EndpointManager.
115
     *
116
     * If url key is present in settings array, this url will be set to the active endpoint
117
     *
118
     * @param array $settings optional settings
119
     *
120
     * @return Manager Endpoint manager
121
     * @throws InputException
122
     */
123
    private function createEndpointManager(array $settings = [])
124
    {
125
        // get the default end points from config object
126
        $endPoints = $this->getConfig()->getEndPoints();
127
128
        // merge defaults urls with custom url if present, custom url is set to active
129
        if (array_key_exists('url', $settings) && filter_var($settings['url'], FILTER_VALIDATE_URL) !== false) {
130
            array_unshift($endPoints, $settings['url']);
131
        }
132
133
        // Create EndPoint Manager
134
        $manager = new Manager();
135
        foreach ($endPoints as $endPoint) {
136
            $manager->createEndpoint($endPoint);
137
        }
138
139
        if ($this->hasLogger() === true) {
140
            $this->logger->info('Created endpoint manager', ['endpoint count' => $manager->getEndpoints()->count()]);
141
        }
142
143
        return $manager;
144
    }
145
146
    /**
147
     * Return SoapConfig.
148
     *
149
     * @return SoapConfig
150
     */
151
    public function getConfig()
152
    {
153
        return $this->config;
154
    }
155
}
156