1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TopMcFrance\Api; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Check a code to vote on the site of TopMcFrance before giving a reward for his vote. |
7
|
|
|
* |
8
|
|
|
* For each vote on the site TopMcFrance with enabled API. |
9
|
|
|
* The site generates a verification code to enter the site. |
10
|
|
|
* This class allows interogger TopMcFrance the validity of a code. |
11
|
|
|
* |
12
|
|
|
* Each generated code is unique to the server and can only be used one time. |
13
|
|
|
* |
14
|
|
|
* @author Jérôme Desjardins <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class VoteChecker { |
17
|
|
|
|
18
|
|
|
const HOST_API = 'https://topmcfrance.fr/api.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Server Id |
22
|
|
|
* @var integer |
23
|
|
|
*/ |
24
|
|
|
protected $serverId; |
25
|
|
|
|
26
|
3 |
|
public function __construct($serverId) { |
27
|
3 |
|
$this->serverId = $serverId; |
28
|
3 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check if code is valid on TopMcFrance |
32
|
|
|
* @param string $code |
33
|
|
|
* @return boolean |
34
|
|
|
* @throws type |
35
|
|
|
*/ |
36
|
3 |
|
public function checkCode($code) { |
37
|
|
|
|
38
|
3 |
|
$response = $this->getResponse($code); |
39
|
|
|
|
40
|
3 |
|
if ($response->etat === 'true') { |
41
|
1 |
|
return true; |
42
|
|
|
} else { |
43
|
2 |
|
switch ($response->erreur) { |
44
|
2 |
|
case '1' : |
|
|
|
|
45
|
1 |
|
throw ApiException::alreadyUsed($code, $response); |
46
|
1 |
|
case '2' : |
|
|
|
|
47
|
1 |
|
throw ApiException::invalidCode($code, $response); |
48
|
|
|
default: |
49
|
|
|
throw ApiException::unknowException($code, $response); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve the URI access for checking code |
56
|
|
|
* @param string $code |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
3 |
|
protected function buildURI($code) { |
60
|
3 |
|
return self::HOST_API . '?id_serv=' . $this->serverId . '&code=' . $code; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the response for TopMcFrance |
65
|
|
|
* @param string $code |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
protected function getResponse($code) { |
69
|
|
|
|
70
|
3 |
|
$result = file_get_contents($this->buildURI($code)); |
71
|
|
|
|
72
|
3 |
|
if ($result === false) { |
73
|
|
|
throw new \Exception("Impossible to access to TopMcFrance"); |
74
|
|
|
} |
75
|
3 |
|
return json_decode($result); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.