GithubApi   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 9 Features 2
Metric Value
wmc 5
c 9
b 9
f 2
lcom 1
cbo 4
dl 0
loc 48
rs 10
ccs 16
cts 16
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getUsername() 0 4 1
A getRepos() 0 15 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
}