|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Laztopaz\OpenSourceEvangelistStatus; |
|
4
|
|
|
|
|
5
|
|
|
error_reporting(0); |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Laztopaz\OpenSourceEvangelistStatus |
|
9
|
|
|
* @author Temitope Olotin <[email protected]> |
|
10
|
|
|
* @license <https://opensource.org/license/MIT> MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
use Dotenv\Dotenv; |
|
14
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
use Laztopaz\OpenSourceEvangelistStatus\EvangelistStatusInterface; |
|
16
|
|
|
use Laztopaz\OpenSourceEvangelistStatus\EvangelistStatusRanking; |
|
17
|
|
|
use Laztopaz\OpenSourceEvangelistStatus\EvangelistStatusException; |
|
18
|
|
|
use Laztopaz\OpenSourceEvangelistStatus\NullNoOfReposException; |
|
19
|
|
|
use Laztopaz\OpenSourceEvangelistStatus\ArgumentCheckException; |
|
20
|
|
|
use Exception; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class EvangelistStatus implements EvangelistStatusInterface |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
protected $githubApi; |
|
26
|
|
|
public $username; |
|
27
|
|
|
private $response; |
|
28
|
|
|
private $clientId; |
|
29
|
|
|
private $clientSecret; |
|
30
|
|
|
private $guzzleClient; |
|
31
|
|
|
private $githubResponse; |
|
32
|
|
|
private $noOfGitRepos; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($username) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->username = $username; |
|
37
|
|
|
|
|
38
|
|
|
$num_args = (int) func_num_args(); // get number of arguments passed to |
|
39
|
|
|
|
|
40
|
|
|
if ($num_args == 0 || $num_args > 1) { |
|
41
|
|
|
throw ArgumentCheckException::NullOfOverflowArgumentException("Argument missing: only one argument is allowed"); |
|
42
|
|
|
} |
|
43
|
|
|
<<<<<<< HEAD |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
======= |
|
46
|
|
|
|
|
47
|
|
|
$this->username = $username; |
|
48
|
|
|
|
|
49
|
|
|
>>>>>>> 5ae30be3b2ebb67e6de8fe512888cf961febb1ad |
|
50
|
|
|
$this->checkNullUsernameException(); |
|
51
|
|
|
|
|
52
|
|
|
$this->clientId = getenv('ClientID'); // get the GitHub client id |
|
53
|
|
|
$this->clientSecret = getenv('ClientSecret'); // get the GitHub client secret |
|
54
|
|
|
|
|
55
|
|
|
$this->guzzleClient = new Client(); |
|
56
|
|
|
|
|
57
|
|
|
$this->githubResponse = $this->getGitApiData(); // return GitHub jsonObject |
|
58
|
|
|
$this->noOfGitRepos = $this->getNumberOfRepos(); // return number of repo the user has |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* This method returns Github JSON data |
|
63
|
|
|
* @param void |
|
64
|
|
|
* @return response |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getGitApiData() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->response = $this->guzzleClient->get('https://api.github.com/users/'.$this->username.'?client_id='.$this->clientId .'&client_secret='.$this->clientSecret); |
|
69
|
|
|
return $this->response->getBody(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* This method returns number of the user repo on Github |
|
74
|
|
|
* @param void |
|
75
|
|
|
* @return public_repos |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getNumberOfRepos() |
|
78
|
|
|
{ |
|
79
|
|
|
$githubJson = json_decode($this->githubResponse, true); |
|
80
|
|
|
|
|
81
|
|
|
if (EvangelistStatusRanking::checkForNullRepos($githubJson)) { |
|
82
|
|
|
throw NullNoOfReposException::createNullReposException("Empty GitHub repository"); |
|
83
|
|
|
} |
|
84
|
|
|
return $githubJson['public_repos']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* This method returns a string depending on number of user repositories on Github |
|
89
|
|
|
* @param void |
|
90
|
|
|
* @return string evangelistRanking |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getStatus() |
|
93
|
|
|
{ |
|
94
|
|
|
return EvangelistStatusRanking::determineEvangelistLevel($this->noOfGitRepos); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* This method checks for empty GitHub username |
|
99
|
|
|
* @param string $username |
|
100
|
|
|
* @return boolean |
|
101
|
|
|
*/ |
|
102
|
|
|
public function checkEmptyGithubUsername($username) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($username == "") { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* This method checks for null argument |
|
112
|
|
|
* @return exception |
|
113
|
|
|
*/ |
|
114
|
|
|
public function checkNullUsernameException() |
|
115
|
|
|
{ |
|
116
|
|
|
if ($this->checkEmptyGithubUsername($this->username)) { |
|
117
|
|
|
throw EvangelistStatusException::createEvangelistStatusException("Username is Empty, please provide a GitHub valid username"); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|