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
|
|
|
private $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
|
|
|
$num_args = (int) func_num_args(); // get number of arguments passed to |
37
|
|
|
|
38
|
|
|
if ($num_args == 0 || $num_args > 1) |
39
|
|
|
{ |
40
|
|
|
throw ArgumentCheckException::NullOfOverflowArgumentException("Argument missing: only one argument is allowed"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->username = $username; |
44
|
|
|
|
45
|
|
|
$this->checkNullUsernameException(); |
46
|
|
|
|
47
|
|
|
$this->clientId = getenv('ClientID'); // get the GitHub client id |
48
|
|
|
$this->clientSecret = getenv('ClientSecret'); // get the GitHub client secret |
49
|
|
|
|
50
|
|
|
$this->guzzleClient = new Client(); |
51
|
|
|
|
52
|
|
|
$this->githubResponse = $this->getGitApiData(); // return GitHub jsonObject |
53
|
|
|
$this->noOfGitRepos = $this->getNumberOfRepos(); // return number of repo the user has |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This method returns Github JSON data |
58
|
|
|
* @param void |
59
|
|
|
* @return response |
60
|
|
|
*/ |
61
|
|
|
public function getGitApiData() |
62
|
|
|
{ |
63
|
|
|
$this->response = $this->guzzleClient->get('https://api.github.com/users/'.$this->username.'?client_id='.$this->clientId .'&client_secret='.$this->clientSecret); |
64
|
|
|
return $this->response->getBody(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This method returns number of the user repo on Github |
69
|
|
|
* @param void |
70
|
|
|
* @return public_repos |
71
|
|
|
*/ |
72
|
|
|
public function getNumberOfRepos() |
73
|
|
|
{ |
74
|
|
|
$githubJson = json_decode($this->githubResponse, true); |
75
|
|
|
|
76
|
|
|
if(EvangelistStatusRanking::checkForNullRepos($githubJson)) |
77
|
|
|
{ |
78
|
|
|
throw NullNoOfReposException::createNullReposException("Empty GitHub repository"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $githubJson['public_repos']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This method returns a string depending on number of user repositories on Github |
86
|
|
|
* @param void |
87
|
|
|
* @return string evangelistRanking |
88
|
|
|
*/ |
89
|
|
|
public function getStatus() |
90
|
|
|
{ |
91
|
|
|
return EvangelistStatusRanking::determineEvangelistLevel($this->noOfGitRepos); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This method checks for empty GitHub username |
96
|
|
|
* @param string $username |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
public function checkEmptyGithubUsername($username) |
100
|
|
|
{ |
101
|
|
|
if ($username == "") |
102
|
|
|
{ |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* This method checks for null argument |
110
|
|
|
* @return exception |
111
|
|
|
*/ |
112
|
|
|
public function checkNullUsernameException() |
113
|
|
|
{ |
114
|
|
|
if ($this->checkEmptyGithubUsername($this->username)) |
115
|
|
|
{ |
116
|
|
|
throw EvangelistStatusException::createEvangelistStatusException("Username is Empty, please provide a GitHub valid username"); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.