Completed
Push — master ( 3184f1...72fb6a )
by Christopher
16:36
created

GithubApi::getUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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