|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Reflex\Challonge; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Reflex\Challonge\Models\Match; |
| 7 | +use Reflex\Challonge\Models\Tournament; |
| 8 | +use Reflex\Challonge\Models\Participant; |
| 9 | +use Reflex\Challonge\Exceptions\ServerException; |
| 10 | +use Reflex\Challonge\Exceptions\NotFoundException; |
| 11 | +use Reflex\Challonge\Exceptions\ValidationException; |
| 12 | +use Reflex\Challonge\Exceptions\UnauthorizedException; |
| 13 | +use Reflex\Challonge\Exceptions\InvalidFormatException; |
| 14 | + |
| 15 | +class Challonge |
| 16 | +{ |
| 17 | + /** |
| 18 | + * ChallongePHP version. |
| 19 | + */ |
| 20 | + const VERSION = '1.0'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Challonge API key. |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private $api_key; |
| 27 | + |
| 28 | + /** |
| 29 | + * Instantiate an instance with the API key. |
| 30 | + * @param string $api_key |
| 31 | + */ |
| 32 | + public function __construct($api_key = '') |
| 33 | + { |
| 34 | + $this->api_key = $api_key; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Build any headers the requests need. |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + private function buildHeaders() |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'User-Agent' => 'ChallongePHP/' . self::VERSION . ' ChallongePHP (https://github.com/teamreflex/ChallongePHP, ' . self::VERSION . ')' |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Base function for all API requests. |
| 50 | + * @param string $path |
| 51 | + * @param array $params |
| 52 | + * @param string $method |
| 53 | + * @return Response |
| 54 | + */ |
| 55 | + private function makeCall($path, $params = [], $method = 'get') |
| 56 | + { |
| 57 | + if (empty($this->api_key)) { |
| 58 | + throw new UnauthorizedException('Must set an API key.'); |
| 59 | + } |
| 60 | + |
| 61 | + $base_uri = "https://api.challonge.com/v1/{$path}.json"; |
| 62 | + $client = new Client(); |
| 63 | + |
| 64 | + $request = $client->createRequest($method, $base_uri, [ |
| 65 | + 'headers' => $this->buildHeaders(), |
| 66 | + 'exceptions' => false, |
| 67 | + ]); |
| 68 | + |
| 69 | + $query = $request->getQuery(); |
| 70 | + $query->set('api_key', $this->api_key); |
| 71 | + |
| 72 | + $response = $client->send($request); |
| 73 | + |
| 74 | + return $this->handleErrors($response); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Handles the response and throws errors accordingly. |
| 79 | + */ |
| 80 | + private function handleErrors($response) |
| 81 | + { |
| 82 | + switch ($response->getStatusCode()) { |
| 83 | + case 200: |
| 84 | + return $response->json(); |
| 85 | + break; |
| 86 | + case 401: |
| 87 | + throw new UnauthorizedException('Unauthorized (Invalid API key or insufficient permissions)'); |
| 88 | + break; |
| 89 | + case 404: |
| 90 | + throw new NotFoundException('Object not found within your account scope'); |
| 91 | + break; |
| 92 | + case 406: |
| 93 | + throw new InvalidFormatException('Requested format is not supported - request JSON or XML only'); |
| 94 | + break; |
| 95 | + case 422: |
| 96 | + throw new ValidationException('Validation error(s) for create or update method'); |
| 97 | + break; |
| 98 | + case 500: |
| 99 | + throw new ServerException('Something went wrong on Challonge\'s end'); |
| 100 | + break; |
| 101 | + default: |
| 102 | + $errors = $response->json()['errors']; |
| 103 | + throw new UnexpectedErrorException($errors); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Retrieve a set of tournaments created with your account. |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function getTournaments() { |
| 113 | + $response = $this->makeCall('tournaments'); |
| 114 | + |
| 115 | + $tournaments = []; |
| 116 | + foreach ($response as $tourney) { |
| 117 | + $tournaments[] = new Tournament($tourney['tournament']); |
| 118 | + } |
| 119 | + |
| 120 | + return $tournaments; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Retrieve a single tournament record created with your account. |
| 125 | + * @param string $tournament |
| 126 | + * @return Tournament |
| 127 | + */ |
| 128 | + public function getTournament($tournament) |
| 129 | + { |
| 130 | + $response = $this->makeCall("tournaments/{$tournament}"); |
| 131 | + return new Tournament($response['tournament']); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Retrieve a tournament's participant list. |
| 136 | + * @param string $tournament |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function getParticipants($tournament) |
| 140 | + { |
| 141 | + $response = $this->makeCall("tournaments/{$tournament}/participants"); |
| 142 | + |
| 143 | + $participants = []; |
| 144 | + foreach ($response as $team) { |
| 145 | + $participants[] = new Participant($team['participant']); |
| 146 | + } |
| 147 | + |
| 148 | + return $participants; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Retrieve a single participant record for a tournament. |
| 153 | + * @param string $tournament |
| 154 | + * @param string $participant |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + public function getParticipant($tournament, $participant) |
| 158 | + { |
| 159 | + $response = $this->makeCall("tournaments/{$tournament}/participants/{$participant}"); |
| 160 | + |
| 161 | + $participant = new Participant($response['participant']); |
| 162 | + |
| 163 | + return $participant; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Retrieve a tournament's match list. |
| 168 | + * @param string $tournament |
| 169 | + * @return array |
| 170 | + */ |
| 171 | + public function getMatches($tournament) |
| 172 | + { |
| 173 | + $response = $this->makeCall("tournaments/{$tournament}/matches"); |
| 174 | + |
| 175 | + $matches = []; |
| 176 | + foreach ($response as $match) { |
| 177 | + $matches[] = new Match($match['match']); |
| 178 | + } |
| 179 | + |
| 180 | + return $matches; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Retrieve a single match record for a tournament. |
| 185 | + * @param string $tournament |
| 186 | + * @param string $match |
| 187 | + * @return array |
| 188 | + */ |
| 189 | + public function getMatch($tournament, $match) |
| 190 | + { |
| 191 | + $response = $this->makeCall("tournaments/{$tournament}/matches/{$match}"); |
| 192 | + |
| 193 | + $match = new Match($response['match']); |
| 194 | + |
| 195 | + return $match; |
| 196 | + } |
| 197 | +} |
0 commit comments