Command   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 11

Importance

Changes 0
Metric Value
wmc 7
cbo 11
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A logHttp() 0 14 3
A setToken() 0 7 2
A setGithubToken() 0 8 1
A getBowerphp() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Massimiliano Arione <[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 Bowerphp\Command;
13
14
use Bowerphp\Bowerphp;
15
use Bowerphp\Config\Config;
16
use Bowerphp\Output\BowerphpConsoleOutput;
17
use Bowerphp\Repository\GithubRepository;
18
use Bowerphp\Util\Filesystem;
19
use Github\Client;
20
use Guzzle\Log\ClosureLogAdapter;
21
use Guzzle\Log\MessageFormatter;
22
use Guzzle\Plugin\Log\LogPlugin;
23
use Symfony\Component\Console\Command\Command as BaseCommand;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * Base class for Bowerphp commands
28
 * Inspired by Composer https://github.com/composer/composer
29
 */
30
abstract class Command extends BaseCommand
0 ignored issues
show
Complexity introduced by
The class Command has a coupling between objects value of 16. Consider to reduce the number of dependencies under 13.
Loading history...
31
{
32
    /**
33
     * @var Filesystem
34
     */
35
    protected $filesystem;
36
37
    /**
38
     * @var Config
39
     */
40
    protected $config;
41
42
    /**
43
     * @var Client
44
     */
45
    protected $githubClient;
46
47
    /**
48
     * @var BowerphpConsoleOutput
49
     */
50
    protected $consoleOutput;
51
52
    /**
53
     * Debug HTTP interactions
54
     *
55
     * @param Client          $client
56
     * @param OutputInterface $output
57
     */
58
    protected function logHttp(Client $client, OutputInterface $output)
59
    {
60
        $guzzle = $client->getHttpClient();
61
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
62
            $logger = function ($message) use ($output) {
63
                $finfo = new \finfo(FILEINFO_MIME);
64
                $msg = ('text' == substr($finfo->buffer($message), 0, 4)) ? $message : '(binary string)';
65
                $output->writeln('<info>Guzzle</info> ' . $msg);
66
            };
67
            $logAdapter = new ClosureLogAdapter($logger);
68
            $logPlugin = new LogPlugin($logAdapter, MessageFormatter::DEBUG_FORMAT);
69
            $guzzle->addSubscriber($logPlugin);
70
        }
71
    }
72
73
    /**
74
     * Set oauth token (to increase API limit to 5000 per hour, instead of default 60)
75
     *
76
     * @param Client $client
77
     */
78
    protected function setToken(Client $client)
79
    {
80
        $token = getenv('BOWERPHP_TOKEN');
81
        if (!empty($token)) {
82
            $client->authenticate($token, null, Client::AUTH_HTTP_TOKEN);
83
        }
84
    }
85
86
    /**
87
     * @param OutputInterface $output
88
     */
89
    protected function setGithubToken(OutputInterface $output)
90
    {
91
        $this->filesystem = new Filesystem();
92
        $this->githubClient = new Client();
93
        $this->config = new Config($this->filesystem);
94
        $this->logHttp($this->githubClient, $output);
95
        $this->setToken($this->githubClient);
96
    }
97
98
    /**
99
     * @param  OutputInterface $output
100
     * @return Bowerphp
101
     */
102
    protected function getBowerphp(OutputInterface $output)
103
    {
104
        $this->consoleOutput = new BowerphpConsoleOutput($output);
105
106
        return new Bowerphp($this->config, $this->filesystem, $this->githubClient, new GithubRepository(), $this->consoleOutput);
107
    }
108
}
109