ConsoleController::getcontributorsAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 28
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
20
/**
21
 * This modified only, comes from https://github.com/zendframework/zf-web/blob/master/module/Application/src/Application/Controller/ConsoleController.php.
22
 */
23
namespace Application\Controller;
24
25
use Zend\Console\Adapter\AdapterInterface as Console;
26
use Zend\Console\ColorInterface as Color;
27
use Zend\Http\Client as HttpClient;
28
use Zend\Mvc\Controller\AbstractConsoleController;
29
30
/**
31
 * Console controller to get contributor list.
32
 */
33
class ConsoleController extends AbstractConsoleController
34
{
35
    /**
36
     * @var Console
37
     */
38
    protected $console;
39
40
    /**
41
     * @var array
42
     */
43
    protected $config;
44
45
    /**
46
     * @var HttpClient
47
     */
48
    protected $httpClient;
49
50
    /**
51
     * Construct console and config property.
52
     */
53
    public function __construct(Console $console, array $config, HttpClient $httpClient)
54
    {
55
        $this->console = $console;
56
        $this->config = $config;
57
58
        $this->httpClient = $httpClient;
59
    }
60
61
    protected function reportError($width, $length, $message, $e = null)
62
    {
63
        if (($length + 9) > $width) {
64
            $this->console->writeLine('');
65
            $length = 0;
66
        }
67
        $spaces = $width - $length - 9;
68
        $this->console->writeLine(str_repeat('.', $spaces).'[ ERROR ]', Color::RED);
69
        $this->console->writeLine($message);
70
        if ($e) {
71
            $this->console->writeLine($e->getTraceAsString());
72
        }
73
    }
74
75
    protected function reportSuccess($width, $length)
76
    {
77
        if (($length + 8) > $width) {
78
            $this->console->writeLine('');
79
            $length = 0;
80
        }
81
        $spaces = $width - $length - 8;
82
        $this->console->writeLine(str_repeat('.', $spaces).'[ DONE ]', Color::GREEN);
83
    }
84
85
    /**
86
     * Collect contributors info.
87
     *
88
     * @param array $contributors
89
     * @param int   $total
90
     * @param int   $width
91
     *
92
     * @return array
93
     */
94
    protected function collectContributorsInfo($contributors, $total, $width)
95
    {
96
        foreach ($contributors as $i => $contributor) {
97
            $message = sprintf('    Processing %d/%d', $i, $total);
98
            $this->console->write($message);
99
            $this->httpClient->setUri("https://api.github.com/users/{$contributor['login']}");
100
            $response = $this->httpClient->send();
101
            if (!$response->isSuccess()) {
102
                // report failure
103
                $error = $response->getStatusCode().': '.$response->getReasonPhrase();
104
                $this->reportError($width, strlen($message), $error);
105
            }
106
            $body = $response->getBody();
107
            $userInfo = json_decode($body, 1);
108
            $contributors[$i]['user_info'] = $userInfo;
109
            $this->reportSuccess($width, strlen($message));
110
        }
111
112
        return $contributors;
113
    }
114
115
    /**
116
     * route : get contributors.
117
     */
118
    public function getcontributorsAction()
119
    {
120
        $width = $this->console->getWidth();
121
        $this->console->writeLine('Fetching GitHub Contributors', Color::GREEN);
122
123
        $response = $this->httpClient->send();
124
        if (!$response->isSuccess()) {
125
            // report failure
126
            $message = $response->getStatusCode().': '.$response->getReasonPhrase();
127
            $this->reportError($width, 0, $message);
128
129
            return;
130
        }
131
132
        $body = $response->getBody();
133
        $contributors = json_decode($body, true);
134
        $total = count($contributors);
135
136
        $contributors = $this->collectContributorsInfo($contributors, $total, $width);
137
138
        $this->console->writeLine(str_repeat('-', $width));
139
        $message = 'Writing file';
140
        $this->console->write($message, Color::BLUE);
141
        $path = $this->config['console']['contributors']['output'];
142
        file_put_contents($path, serialize($contributors));
143
        $this->reportSuccess($width, strlen($message));
144
        $this->console->writeLine(sprintf('File written to %s', $path));
145
    }
146
}
147