GithubApi::getUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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