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 $exceptionCheckInvalidUsername; |
|
|
|
|
32
|
|
|
private $githubResponse; |
33
|
|
|
private $noOfGitRepos; |
34
|
|
|
|
35
|
|
|
public function __construct($username) |
36
|
|
|
{ |
37
|
|
|
$num_args = (int) func_num_args(); |
38
|
|
|
|
39
|
|
|
if ($num_args == 0 || $num_args > 1) |
40
|
|
|
{ |
41
|
|
|
throw ArgumentCheckException::NullOfOverflowArgumentException("Argument missing: only one argument is allowed"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (self::checkEmptyGithubUsername($username)) |
45
|
|
|
{ |
46
|
|
|
throw EvangelistStatusException::createEvangelistStatusException("Username is Empty, please provide a GitHub valid username"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->username = $username; |
50
|
|
|
|
51
|
|
|
$this->clientId = getenv('ClientID'); // get the GitHub client id |
52
|
|
|
$this->clientSecret = getenv('ClientSecret'); // get the GitHub client secret |
53
|
|
|
|
54
|
|
|
$this->guzzleClient = new Client(); |
55
|
|
|
|
56
|
|
|
$this->githubResponse = $this->getGitApiData(); // return GitHub jsonObject |
57
|
|
|
$this->noOfGitRepos = $this->getNumberOfRepos(); // return number of repo the user has |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* This method returns Github JSON data |
62
|
|
|
* @param void |
63
|
|
|
* @return response |
64
|
|
|
*/ |
65
|
|
|
public function getGitApiData() |
66
|
|
|
{ |
67
|
|
|
$this->response = $this->guzzleClient->get('https://api.github.com/users/'.$this->username.'?client_id='.$this->clientId .'&client_secret='.$this->clientSecret); |
68
|
|
|
|
69
|
|
|
return $this->response->getBody(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This method returns number of the user repo on Github |
74
|
|
|
* @param void |
75
|
|
|
* @return int public_repos |
76
|
|
|
*/ |
77
|
|
|
public function getNumberOfRepos() |
78
|
|
|
{ |
79
|
|
|
$githubJson = json_decode($this->githubResponse, true); |
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
|
|
|
$evangelistRanking = new EvangelistStatusRanking(); |
92
|
|
|
|
93
|
|
|
if($evangelistRanking->checkForNullRepos($this->noOfGitRepos)) |
94
|
|
|
{ |
95
|
|
|
throw NullNoOfReposException::createNullReposException("Empty GitHub repository"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $evangelistRanking->determineEvangelistLevel($this->noOfGitRepos); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* This method checks for empty GitHub username |
103
|
|
|
* @param string $username |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
public function checkEmptyGithubUsername($username) |
107
|
|
|
{ |
108
|
|
|
if ($username == "") |
109
|
|
|
{ |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
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.