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

ConnectorFactory::createPlatformConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace WebservicesNl\Connector;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerInterface;
8
use WebservicesNl\Common\Exception\Client\InputException;
9
use WebservicesNl\Connector\Client\ClientFactoryInterface;
10
use WebservicesNl\Connector\Client\ClientInterface;
11
use WebservicesNl\Connector\ProtocolAdapter\AdapterInterface;
12
use WebservicesNl\Platform\PlatformConfigInterface;
13
14
/**
15
 * ConnectorFactory for creating platform connector.
16
 * Helps with creating a connector for a given platform over a certain protocol.
17
 * Provide some user settings and afterwards create platforms like a boss.
18
 */
19
class ConnectorFactory implements LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    const PLATFORM_PATH = 'WebservicesNl\\Platform\\%1$s\\PlatformConfig';
24
    const PROTOCOL_PATH = 'WebservicesNl\\Protocol\\%1$s\\Client\\%1$sFactory';
25
    const ADAPTER_PATH = 'WebservicesNl\\Connector\\ProtocolAdapter\\%1$sAdapter';
26
27
    /**
28
     * Generic user settings (eg credentials).
29
     *
30
     * @var array
31
     */
32
    protected $userSettings;
33
34
    /**
35
     * ConnectorFactory constructor.
36
     *
37
     * @param array $settings
38
     */
39
    public function __construct(array $settings = [])
40
    {
41
        $this->userSettings = $settings;
42
    }
43
44
    /**
45
     * @param array $settings
46
     *
47
     * @return static
48
     */
49
    public static function build(array $settings = [])
50
    {
51
        return new static($settings);
52
    }
53
54
    /**
55
     * Creates an adapter for given platform and client.
56
     * Wrap the client inside a platform adapter.
57
     *
58
     * @param ClientInterface $client
59
     *
60
     * @return AdapterInterface
61
     * @throws InputException
62
     */
63
    private function buildAdapter(ClientInterface $client)
64
    {
65
        // Build an adapter for client (as proxy between the connector and the client)
66
        $adapterFQCN = sprintf(self::ADAPTER_PATH, ucfirst($client->getProtocolName()));
67
68
        /** @var AdapterInterface $platFormAdapter */
69
        return new $adapterFQCN($client);
70
    }
71
72
    /**
73
     * Creates an connection for a given platform.
74
     *
75
     * @param string $protocolName type of connection (SOAP, REST etc)
76
     * @param string $platformName
77
     *
78
     * @return ConnectorInterface
79
     * @throws InputException
80
     */
81
    public function create($protocolName, $platformName)
82
    {
83
        $config = $this->createPlatformConfig($platformName);
84
85
        // instantiate client factory for given protocol and pass along platform config. Ask Factory build a client
86
        $factory = $this->createProtocolFactory($protocolName, $config);
87
        if ($this->getLogger() instanceof LoggerInterface) {
88
            $factory->setLogger($this->getLogger());
89
        }
90
91
        $client = $factory->create($this->userSettings);
92
93
        // wrap the client in a connector
94
        return $this->buildConnector($client, $config);
95
    }
96
97
    /**
98
     * Build the connector with given client and platform config.
99
     *
100
     * @param ClientInterface         $client
101
     * @param PlatformConfigInterface $config
102
     *
103
     * @return ConnectorInterface
104
     * @throws \WebservicesNl\Common\Exception\Client\InputException
105
     */
106
    private function buildConnector(ClientInterface $client, PlatformConfigInterface $config)
107
    {
108
        $adapter = $this->buildAdapter($client);
109
        $connectorName = $config->getConnectorName();
110
111
        return new $connectorName($adapter);
112
    }
113
114
    /**
115
     * @return LoggerInterface
116
     */
117
    public function getLogger()
118
    {
119
        return $this->logger;
120
    }
121
122
    /**
123
     * Tries to find the factory for given protocol, to configure and create the ClientInterface.
124
     *
125
     * @param string                  $protocolName name of the protocol
126
     * @param PlatformConfigInterface $config       platform config object
127
     *
128
     * @return ClientFactoryInterface
129
     * @throws InputException
130
     */
131
    private function createProtocolFactory($protocolName, PlatformConfigInterface $config)
132
    {
133
        $clientFactory = sprintf(self::PROTOCOL_PATH, ucfirst($protocolName));
134
        if (!class_exists($clientFactory)) {
135
            throw new InputException("Could not find a factory for $protocolName");
136
        }
137
138
        /** @var ClientFactoryInterface $clientFactory */
139
        return $clientFactory::build($config);
140
    }
141
142
    /**
143
     * Build a platform config with given settings.
144
     *
145
     * @param string $platformName
146
     *
147
     * @return PlatformConfigInterface
148
     * @throws InputException
149
     */
150
    private function createPlatformConfig($platformName)
151
    {
152
        // create platform config from string
153
        $platformConfig = sprintf(self::PLATFORM_PATH, ucfirst($platformName));
154
        if (!class_exists($platformConfig)) {
155
            throw new InputException("Could not find a platformConfig for $platformName");
156
        }
157
158
        /** @var PlatformConfigInterface $platformConfig */
159
        $platformConfig = new $platformConfig();
160
        $platformConfig->loadFromArray($this->userSettings);
161
162
        return $platformConfig;
163
    }
164
}
165