ApiFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 69
ccs 12
cts 12
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createApiConnector() 0 11 2
A __construct() 0 10 1
1
<?php declare(strict_types=1);
2
3
namespace BabDev\Transifex;
4
5
use Psr\Http\Client\ClientInterface;
6
use Psr\Http\Message\RequestFactoryInterface;
7
use Psr\Http\Message\StreamFactoryInterface;
8
use Psr\Http\Message\UriFactoryInterface;
9
10
/**
11
 * Factory class responsible for creating Transifex API objects.
12
 */
13
final class ApiFactory implements FactoryInterface
14
{
15
    /**
16
     * The HTTP client.
17
     *
18
     * @var ClientInterface
19
     */
20
    private $client;
21
22
    /**
23
     * The request factory.
24
     *
25
     * @var RequestFactoryInterface
26
     */
27
    private $requestFactory;
28
29
    /**
30
     * The stream factory.
31
     *
32
     * @var StreamFactoryInterface
33
     */
34
    private $streamFactory;
35
36
    /**
37
     * The URI factory.
38
     *
39
     * @var UriFactoryInterface
40
     */
41
    private $uriFactory;
42
43
    /**
44
     * @param ClientInterface         $client         The HTTP client
45
     * @param RequestFactoryInterface $requestFactory The request factory
46
     * @param StreamFactoryInterface  $streamFactory  The stream factory
47
     * @param UriFactoryInterface     $uriFactory     The URI factory
48
     */
49 2
    public function __construct(
50
        ClientInterface $client,
51
        RequestFactoryInterface $requestFactory,
52
        StreamFactoryInterface $streamFactory,
53
        UriFactoryInterface $uriFactory
54
    ) {
55 2
        $this->client         = $client;
56 2
        $this->requestFactory = $requestFactory;
57 2
        $this->streamFactory  = $streamFactory;
58 2
        $this->uriFactory     = $uriFactory;
59 2
    }
60
61
    /**
62
     * Factory method to create API connectors.
63
     *
64
     * @param string $name    Name of the API connector to retrieve
65
     * @param array  $options API connector options
66
     *
67
     * @return ApiConnector
68
     *
69
     * @throws Exception\UnknownApiConnectorException
70
     */
71 2
    public function createApiConnector(string $name, array $options = []): ApiConnector
72
    {
73 2
        $namespace = __NAMESPACE__ . '\\Connector';
74 2
        $class     = $namespace . '\\' . \ucfirst(\strtolower($name));
75
76 2
        if (\class_exists($class)) {
77 1
            return new $class($this->client, $this->requestFactory, $this->streamFactory, $this->uriFactory, $options);
78
        }
79
80
        // No class found, sorry!
81 1
        throw new Exception\UnknownApiConnectorException("Could not find an API object for '$name'.");
82
    }
83
}
84