Client::setLoggerInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * xMDB-API
4
 *
5
 * Copyright © 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
/**
16
 * Created by Marcin.
17
 * Date: 29.11.2017
18
 * Time: 00:05
19
 */
20
21
namespace mrcnpdlk\Xmdb;
22
23
use mrcnpdlk\Psr16Cache\Adapter;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\NullLogger;
26
use Psr\SimpleCache\CacheInterface;
27
28
/**
29
 * Class Client
30
 *
31
 * @package mrcnpdlk\Xmdb
32
 */
33
class Client
34
{
35
36
    /**
37
     * Cache handler
38
     *
39
     * @var CacheInterface
40
     */
41
    private $oCache;
42
    /**
43
     * @var Adapter
44
     */
45
    private $oCacheAdapter;
46
    /**
47
     * Logger handler
48
     *
49
     * @var \Psr\Log\LoggerInterface
50
     */
51
    private $oLogger;
52
    /**
53
     * @var string
54
     */
55
    private $sTmdbToken;
56
    /**
57
     * @var string
58
     */
59
    private $sImdbUser;
60
    /**
61
     * Language code
62
     *
63
     * @var string
64
     */
65
    private $sLangCode;
66
    /**
67
     * @var string
68
     */
69
    private $sOmdbToken;
70
71
    /**
72
     * Client constructor.
73
     *
74
     */
75
    public function __construct()
76
    {
77
        $this->setLoggerInstance();
78
        $this->setCacheInstance();
79
    }
80
81
    /**
82
     * @return \mrcnpdlk\Psr16Cache\Adapter
83
     */
84
    public function getAdapter(): Adapter
85
    {
86
        return $this->oCacheAdapter;
87
    }
88
89
    /**
90
     * @return string
91
     * @throws \mrcnpdlk\Xmdb\Exception
92
     */
93
    public function getImdbUser(): string
94
    {
95
        if (empty($this->sImdbUser)) {
96
            throw new Exception('Imdb User not set');
97
        }
98
99
        return $this->sImdbUser;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getLang(): string
106
    {
107
        return $this->sLangCode;
108
    }
109
110
    /**
111
     * Get logger instance
112
     *
113
     * @return \Psr\Log\LoggerInterface
114
     */
115
    public function getLogger(): LoggerInterface
116
    {
117
        return $this->oLogger;
118
    }
119
120
    /**
121
     * @return string
122
     * @throws \mrcnpdlk\Xmdb\Exception
123
     */
124
    public function getOmdbToken(): string
125
    {
126
        if (empty($this->sOmdbToken)) {
127
            throw new Exception('Tmdb Token not set');
128
        }
129
130
        return $this->sOmdbToken;
131
    }
132
133
    /**
134
     * @return string
135
     * @throws \mrcnpdlk\Xmdb\Exception
136
     */
137
    public function getTmdbToken(): string
138
    {
139
        if (empty($this->sTmdbToken)) {
140
            throw new Exception('Tmdb Token not set');
141
        }
142
143
        return $this->sTmdbToken;
144
    }
145
146
    /**
147
     * Setting Cache Adapter
148
     *
149
     * @return $this
150
     */
151
    private function setCacheAdapter()
152
    {
153
        $this->oCacheAdapter = new Adapter($this->oCache, $this->oLogger);
154
155
        return $this;
156
    }
157
158
    /**
159
     * Set Cache handler (PSR-16)
160
     *
161
     * @param \Psr\SimpleCache\CacheInterface|null $oCache
162
     *
163
     * @return \mrcnpdlk\Xmdb\Client
164
     * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md PSR-16
165
     */
166
    public function setCacheInstance(CacheInterface $oCache = null): Client
167
    {
168
        $this->oCache = $oCache;
169
        $this->setCacheAdapter();
170
171
        return $this;
172
    }
173
174
    /**
175
     * @param string $user
176
     *
177
     * @return \mrcnpdlk\Xmdb\Client
178
     */
179
    public function setImdbUser(string $user): Client
180
    {
181
        $this->sImdbUser = $user;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string $lang ISO 639-1 language code
188
     *
189
     * @return $this
190
     */
191
    public function setLang(string $lang = 'en')
192
    {
193
        $this->sLangCode = strtolower($lang);
194
195
        return $this;
196
    }
197
198
    /**
199
     * Set Logger handler (PSR-3)
200
     *
201
     * @param \Psr\Log\LoggerInterface|null $oLogger
202
     *
203
     * @return $this
204
     */
205
    public function setLoggerInstance(LoggerInterface $oLogger = null)
206
    {
207
        $this->oLogger = $oLogger ?: new NullLogger();
208
        $this->setCacheAdapter();
209
210
        return $this;
211
    }
212
213
    /**
214
     * @param string $user
215
     *
216
     * @return \mrcnpdlk\Xmdb\Client
217
     */
218
    public function setOmdbToken(string $user): Client
219
    {
220
        $this->sOmdbToken = $user;
221
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $token
227
     *
228
     * @return \mrcnpdlk\Xmdb\Client
229
     */
230
    public function setTmdbToken(string $token): Client
231
    {
232
        $this->sTmdbToken = $token;
233
234
        return $this;
235
    }
236
}
237