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.

ConnectorFactory::createProtocolFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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
 *
17
 * Factory for creating a client (connector) for given platform over given certain protocol.
18
 * Provide user settings for creating new client instances.
19
 */
20
class ConnectorFactory implements LoggerAwareInterface
21
{
22
    use LoggerAwareTrait;
23
24
    const PLATFORM_PATH = 'WebservicesNl\\Platform\\%1$s\\PlatformConfig';
25
    const PROTOCOL_PATH = 'WebservicesNl\\Protocol\\%1$s\\Client\\%1$sFactory';
26
    const ADAPTER_PATH = 'WebservicesNl\\Connector\\ProtocolAdapter\\%1$sAdapter';
27
28
    /**
29
     * Generic user settings (eg credentials).
30
     *
31
     * @var array
32
     */
33
    protected $userSettings;
34
35
    /**
36
     * ConnectorFactory constructor.
37
     *
38
     * @param array $settings
39
     * @param LoggerInterface $logger
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be null|LoggerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     */
41 6
    public function __construct(array $settings = [], LoggerInterface $logger = null)
42
    {
43 6
        $this->userSettings = $settings;
44 6
        if ($logger instanceof LoggerInterface) {
45 1
            $this->setLogger($logger);
46 1
        }
47 6
    }
48
49
    /**
50
     * @param array $settings
51
     * @param LoggerInterface $logger
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be null|LoggerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     *
53
     * @return static
54
     */
55 6
    public static function build(array $settings = [], LoggerInterface $logger = null)
56
    {
57 6
        return new static($settings, $logger);
58
    }
59
60
    /**
61
     * Creates an adapter for given platform and client.
62
     * Wrap the client inside a platform adapter.
63
     *
64
     * @param ClientInterface $client
65
     *
66
     * @return AdapterInterface
67
     * @throws InputException
68
     */
69 3
    private function buildAdapter(ClientInterface $client)
70
    {
71
        // Build an adapter for client (as proxy between the connector and the client)
72 3
        $adapterFQCN = sprintf(self::ADAPTER_PATH, ucfirst($client->getProtocolName()));
73
74
        /** @var AdapterInterface $platFormAdapter */
75 3
        return new $adapterFQCN($client);
76
    }
77
78
    /**
79
     * Creates an connection for a given platform.
80
     *
81
     * @param string $protocolName type of connection (SOAP, REST etc)
82
     * @param string $platformName name of platform (webservices)
83
     *
84
     * @return ConnectorInterface
85
     * @throws InputException
86
     */
87 6
    public function create($protocolName, $platformName)
88
    {
89 6
        $config = $this->createPlatformConfig($platformName);
90
91
        // instantiate client factory for given protocol and pass along platform config.
92 4
        $factory = $this->createProtocolFactory($protocolName, $config);
93 3
        if ($this->getLogger() instanceof LoggerInterface) {
94 2
            $factory->setLogger($this->getLogger());
95 2
        }
96
97
        // build a protocol client (eg: Soap client, RPC client)
98 3
        $client = $factory->create($this->userSettings);
99
100
        // add the client to the wrapper (eg platform connector)
101 3
        return $this->buildConnector($client, $config);
102
    }
103
104
    /**
105
     * Build the connector with given client and platform config.
106
     *
107
     * @param ClientInterface         $client
108
     * @param PlatformConfigInterface $config
109
     *
110
     * @return ConnectorInterface
111
     * @throws \WebservicesNl\Common\Exception\Client\InputException
112
     */
113 3
    private function buildConnector(ClientInterface $client, PlatformConfigInterface $config)
114
    {
115 3
        $adapter = $this->buildAdapter($client);
116 3
        $connectorName = $config->getConnectorName();
117
118 3
        return new $connectorName($adapter);
119
    }
120
121
    /**
122
     * @return LoggerInterface
123
     */
124 3
    public function getLogger()
125
    {
126 3
        return $this->logger;
127
    }
128
129
    /**
130
     * Tries to find the factory for given protocol, to configure and create the ClientInterface.
131
     *
132
     * @param string                  $protocolName name of the protocol
133
     * @param PlatformConfigInterface $config       platform config object
134
     *
135
     * @return ClientFactoryInterface
136
     * @throws InputException
137
     */
138 4
    private function createProtocolFactory($protocolName, PlatformConfigInterface $config)
139
    {
140 4
        $clientFactory = sprintf(self::PROTOCOL_PATH, ucfirst($protocolName));
141 4
        if (!class_exists($clientFactory)) {
142 1
            throw new InputException("Could not find a factory for $protocolName");
143
        }
144
145
        /** @var ClientFactoryInterface $clientFactory */
146 3
        return $clientFactory::build($config);
147
    }
148
149
    /**
150
     * Build a platform config with given settings.
151
     *
152
     * @param string $platformName
153
     *
154
     * @return PlatformConfigInterface
155
     * @throws InputException
156
     */
157 6
    private function createPlatformConfig($platformName)
158
    {
159
        // create platform config from string
160 6
        $platformConfig = sprintf(self::PLATFORM_PATH, ucfirst($platformName));
161 6
        if (!class_exists($platformConfig)) {
162 1
            throw new InputException("Could not find a platformConfig for $platformName");
163
        }
164
165
        /** @var PlatformConfigInterface $platformConfig */
166 5
        $platformConfig = new $platformConfig();
167 5
        $platformConfig->loadFromArray($this->userSettings);
168
169 4
        return $platformConfig;
170
    }
171
}
172