Completed
Push — master ( 0c057f...83f3fd )
by Christopher
13:35
created

GithubApi::getRepos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 7
Bugs 7 Features 1
Metric Value
c 7
b 7
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 *  @author chris.vundi
4
 *  This class makes a call to Github and returns
5
 *  the number of repos one owns provided a username
6
 *  is provided.
7
 */
8
9
namespace Vundi\Checkpoint1;
10
use GuzzleHttp\Client;
11
use Vundi\Checkpoint1\EvangelizeException;
12
13
class GithubApi
14
{
15
    /**
16
     * Github username
17
     * @var string
18
     */
19
    protected $username;
20
21 5
    public function __construct($username = null)
22
    {
23 5
        $this->username = $username;
24 5
        if (is_null($username)) {
25 1
            throw new EvangelizeException("You have to pass in a username, Username cannot be null", 1);
26
        }
27 5
    }
28
29
    /**
30
     * Get username passed as the parameter
31
     * @return string
32
     */
33 1
    public function getUsername()
34
    {
35 1
        return $this->username;
36
    }
37
38
    /**
39
     * Return an integer representing number of public repos
40
     * the username provided has on github
41
     * @return int
42
     */
43 2
    public function getRepos()
44
    {
45 2
        $url = "https://api.github.com/users/{$this->username}/repos";
46 2
        $client = new Client();
47
        //will return http response with the body in json format
48 2
        $res = $client->request('GET', $url, ['exceptions' => false]);
49 2
        if ($res->getStatusCode() == 404) {
50 1
            throw new EvangelizeException("The username you passed is not a valid Github username", 1);
51
        }
52 1
        $decoded = json_decode($res->getBody(), true);
53 1
        $number = count($decoded);
54 1
        return $number;
55
    }
56
57
}