Completed
Push — master ( dbe8ee...cfcdbf )
by Michael
02:09
created

Transifex   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 120
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOption() 0 3 1
A get() 0 20 4
A setOption() 0 5 1
A __construct() 0 16 2
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
 * Base class for interacting with the Transifex API.
12
 */
13
class Transifex
14
{
15
    /**
16
     * The HTTP client.
17
     *
18
     * @var ClientInterface
19
     */
20
    protected $client;
21
22
    /**
23
     * The request factory.
24
     *
25
     * @var RequestFactoryInterface
26
     */
27
    protected $requestFactory;
28
29
    /**
30
     * The stream factory.
31
     *
32
     * @var StreamFactoryInterface
33
     */
34
    protected $streamFactory;
35
36
    /**
37
     * The URI factory.
38
     *
39
     * @var UriFactoryInterface
40
     */
41
    protected $uriFactory;
42
43
    /**
44
     * Options for the Transifex object.
45
     *
46
     * @var array
47
     */
48
    protected $options;
49
50
    /**
51
     * @param ClientInterface         $client         The HTTP client
52
     * @param RequestFactoryInterface $requestFactory The request factory
53
     * @param StreamFactoryInterface  $streamFactory  The stream factory
54
     * @param UriFactoryInterface     $uriFactory     The URI factory
55
     * @param array                   $options        Transifex options array
56
     */
57 6
    public function __construct(
58
        ClientInterface $client,
59
        RequestFactoryInterface $requestFactory,
60
        StreamFactoryInterface $streamFactory,
61
        UriFactoryInterface $uriFactory,
62
        array $options = []
63
    ) {
64 6
        $this->client         = $client;
65 6
        $this->requestFactory = $requestFactory;
66 6
        $this->streamFactory  = $streamFactory;
67 6
        $this->uriFactory     = $uriFactory;
68 6
        $this->options        = $options;
69
70
        // Setup the default API url if not already set.
71 6
        if (!$this->getOption('base_uri')) {
72 6
            $this->setOption('base_uri', 'https://www.transifex.com');
73
        }
74 6
    }
75
76
    /**
77
     * Factory method to fetch API objects.
78
     *
79
     * @param string $name Name of the API object to retrieve
80
     *
81
     * @return TransifexObject
82
     *
83
     * @throws \InvalidArgumentException
84
     */
85 5
    public function get(string $name): TransifexObject
86
    {
87 5
        $namespace = \rtrim($this->getOption('object.namespace', __NAMESPACE__), '\\');
88 5
        $class     = $namespace . '\\' . \ucfirst(\strtolower($name));
89
90 5
        if (\class_exists($class)) {
91 2
            return new $class($this->client, $this->requestFactory, $this->streamFactory, $this->uriFactory, $this->options);
92
        }
93
94
        // If a custom namespace was specified, let's try to find an object in the local namespace
95 3
        if ($namespace !== __NAMESPACE__) {
96 2
            $class = __NAMESPACE__ . '\\' . \ucfirst(\strtolower($name));
97
98 2
            if (\class_exists($class)) {
99 1
                return new $class($this->client, $this->requestFactory, $this->streamFactory, $this->uriFactory, $this->options);
100
            }
101
        }
102
103
        // No class found, sorry!
104 2
        throw new \InvalidArgumentException("Could not find an API object for '$name'.");
105
    }
106
107
    /**
108
     * Get an option from the Transifex instance.
109
     *
110
     * @param string $key     The name of the option to get
111
     * @param mixed  $default The default value if the option is not set
112
     *
113
     * @return mixed The option value
114
     */
115 1
    public function getOption(string $key, $default = null)
116
    {
117 1
        return $this->options[$key] ?? $default;
118
    }
119
120
    /**
121
     * Set an option for the Transifex instance.
122
     *
123
     * @param string $key   The name of the option to set
124
     * @param mixed  $value The option value to set
125
     *
126
     * @return $this
127
     */
128 1
    public function setOption(string $key, $value): self
129
    {
130 1
        $this->options[$key] = $value;
131
132 1
        return $this;
133
    }
134
}
135