Completed
Push — master ( ef15e7...7e484e )
by Michael
02:15 queued 16s
created

Transifex::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * BabDev Transifex Package
5
 *
6
 * (c) Michael Babker <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BabDev\Transifex;
13
14
/**
15
 * Base class for interacting with the Transifex API.
16
 */
17
class Transifex
18
{
19
    /**
20
     * Options for the Transifex object.
21
     *
22
     * @var array|\ArrayAccess
23
     */
24
    protected $options;
25
26
    /**
27
     * The HTTP client object to use in sending HTTP requests.
28
     *
29
     * @var Http
30
     */
31
    protected $client;
32
33
    /**
34
     * @param array|\ArrayAccess $options Transifex options array.
35
     * @param Http               $client  The HTTP client object.
36
     *
37
     * @throws \InvalidArgumentException
38
     */
39 1
    public function __construct($options = [], Http $client = null)
40
    {
41 1
        if (!is_array($options) && !($options instanceof \ArrayAccess)) {
42
            throw new \InvalidArgumentException(
43
                'The options param must be an array or implement the ArrayAccess interface.'
44
            );
45
        }
46
47 1
        $this->options = $options;
48
49
        // Set the Authorization header if we have credentials
50 1
        if ($this->getOption('api.username') && $this->getOption('api.password')) {
51
            $headers = [
52 1
                'Authorization' => 'Basic ' . base64_encode($this->getOption('api.username') . ':'
53 1
                        . $this->getOption('api.password')),
54 1
            ];
55
56 1
            $this->setOption('headers', $headers);
57 1
        }
58
59 1
        $this->client = isset($client) ? $client : new Http($this->options);
60
61
        // Setup the default API url if not already set.
62 1
        if (!$this->getOption('api.url')) {
63 1
            $this->setOption('api.url', 'https://www.transifex.com/api/2');
64 1
        }
65 1
    }
66
67
    /**
68
     * Factory method to fetch API objects.
69
     *
70
     * @param string $name Name of the API object to retrieve.
71
     *
72
     * @return TransifexObject
73
     *
74
     * @throws \InvalidArgumentException
75
     */
76 5
    public function get($name)
77
    {
78 5
        $namespace = $this->getOption('object.namespace', __NAMESPACE__);
79 5
        $class     = $namespace . '\\' . ucfirst(strtolower($name));
80
81 5
        if (class_exists($class)) {
82 2
            return new $class($this->options, $this->client);
83
        }
84
85
        // If a custom namespace was specified, let's try to find an object in the local namespace
86 3
        if ($namespace !== __NAMESPACE__) {
87 2
            $class = __NAMESPACE__ . '\\' . ucfirst(strtolower($name));
88
89 2
            if (class_exists($class)) {
90 1
                return new $class($this->options, $this->client);
91
            }
92 1
        }
93
94
        // No class found, sorry!
95 2
        throw new \InvalidArgumentException("Could not find an API object for '$name'.");
96
    }
97
98
    /**
99
     * Get an option from the Transifex instance.
100
     *
101
     * @param string $key     The name of the option to get.
102
     * @param mixed  $default The default value if the option is not set.
103
     *
104
     * @return mixed The option value.
105
     */
106 1
    public function getOption($key, $default = null)
107
    {
108 1
        return isset($this->options[$key]) ? $this->options[$key] : $default;
109
    }
110
111
    /**
112
     * Set an option for the Transifex instance.
113
     *
114
     * @param string $key   The name of the option to set.
115
     * @param mixed  $value The option value to set.
116
     *
117
     * @return $this
118
     */
119 1
    public function setOption($key, $value)
120
    {
121 1
        $this->options[$key] = $value;
122
123 1
        return $this;
124
    }
125
}
126