Completed
Pull Request — develop (#33)
by Michael
02:17
created

PhlackClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 9
c 3
b 1
f 1
lcom 0
cbo 4
dl 0
loc 58
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A isLegacyUrl() 0 4 2
A factory() 0 8 3
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