PhlackClient::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 11
nc 2
nop 2
crap 4
1
<?php
2
3
namespace Crummy\Phlack\Bridge\Guzzle;
4
5
use Guzzle\Common\Collection;
6
use Guzzle\Service\Client;
7
use Guzzle\Service\Description\ServiceDescription;
8
9
class PhlackClient extends Client
10
{
11
    /** @var array */
12
    protected $defaultConfig = [
13
        'request.options' => [
14
            'exceptions'  => false,
15
        ],
16
    ];
17
18
    /** @var array */
19
    protected $legacyRequirements = ['username', 'token'];
20
21
    /**
22
     * @param string     $baseUrl
23
     * @param array|null $config
24
     */
25 24
    public function __construct($baseUrl = LegacyUrlPlugin::BASE_URL, $config = null)
26
    {
27 24
        $legacyMode = $this->isLegacyUrl($baseUrl);
28
29 24
        $config = Collection::fromConfig(
30 24
            $config ?: [],
31 24
            $this->defaultConfig,
32 24
            $legacyMode ? $this->legacyRequirements : []
33 24
        );
34
35 20
        if ($legacyMode) {
36 2
            $this->addSubscriber(new LegacyUrlPlugin($config['username'], $config['token']));
37 2
            unset($config['username'], $config['token']);
38 2
        }
39
40 20
        parent::__construct($baseUrl, $config);
41
42 20
        $this->setDescription(ServiceDescription::factory(__DIR__.'/Resources/slack.json'));
43 20
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 6
    public static function factory($config = [])
49
    {
50 6
        if (!is_array($config)) {
51 1
            return new self($config);
52
        }
53
54 5
        return new self(isset($config['url']) ? $config['url'] : null, $config);
55
    }
56
57
    /**
58
     * @param string $baseUrl
59
     *
60
     * @return bool
61
     */
62 24
    private function isLegacyUrl($baseUrl)
63
    {
64 24
        return null === $baseUrl || $baseUrl === LegacyUrlPlugin::BASE_URL;
65
    }
66
}
67