Skip to content
This repository was archived by the owner on Jun 16, 2019. It is now read-only.

Commit ccf8f1f

Browse files
Merge pull request #13 from avengerweb/master
Work under version managing
2 parents b8b454c + 5a6c7dc commit ccf8f1f

11 files changed

Lines changed: 285 additions & 55 deletions

File tree

app/Exceptions/Handler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Exceptions;
44

55
use Exception;
6+
use Illuminate\Http\Response;
67
use Illuminate\Validation\ValidationException;
78
use Illuminate\Auth\Access\AuthorizationException;
89
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -21,6 +22,7 @@ class Handler extends ExceptionHandler
2122
HttpException::class,
2223
ModelNotFoundException::class,
2324
ValidationException::class,
25+
UnsupportedVersion::class
2426
];
2527

2628
/**
@@ -45,6 +47,12 @@ public function report(Exception $e)
4547
*/
4648
public function render($request, Exception $e)
4749
{
48-
return parent::render($request, $e);
50+
switch ($e) {
51+
case ($e instanceof UnsupportedVersion): {
52+
return new Response($e->getMessage(), $e->getCode());
53+
}
54+
default:
55+
return parent::render($request, $e);
56+
}
4957
}
5058
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Illuminate\Http\Response;
6+
use RuntimeException;
7+
8+
class UnsupportedVersion extends RuntimeException
9+
{
10+
protected $code = Response::HTTP_BAD_REQUEST;
11+
protected $message = ["error" => "Unsupported version"];
12+
}

app/Helpers/TCAPI.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace App\Helpers;
3+
4+
/**
5+
* Information about API
6+
*
7+
* Class TCAPI
8+
* @package App\Helpers
9+
*/
10+
class TCAPI
11+
{
12+
/**
13+
* Game version for current request, available versions in api config
14+
*
15+
* @var string
16+
*/
17+
private $gameVersion = "";
18+
19+
/**
20+
* @var string
21+
*/
22+
private $apiVersion = "0.0.0";
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getGameVersion()
28+
{
29+
return $this->gameVersion;
30+
}
31+
32+
/**
33+
* @param string $gameVersion
34+
*/
35+
public function setGameVersion($gameVersion)
36+
{
37+
$this->gameVersion = $gameVersion;
38+
}
39+
40+
/**
41+
* Checking version
42+
*
43+
* @param $version
44+
* @return mixed
45+
*/
46+
public static function is($version) {
47+
return hash_equals(app("App\Helpers\TCAPI")->getGameVersion(), $version);
48+
}
49+
}

app/Http/Controllers/Achievements/AchievementsController.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace App\Http\Controllers\Achievements;
33

4+
use App\Exceptions\UnsupportedVersion;
5+
use App\Helpers\TCAPI;
46
use App\Models\Achievements\Achievement;
57
use App\Models\Achievements\AchievementCategory;
68
use Illuminate\Routing\Controller;
@@ -12,6 +14,18 @@
1214
*/
1315
class AchievementsController extends Controller
1416
{
17+
private $api;
18+
19+
/**
20+
* AchievementsController constructor.
21+
*
22+
* @param TCAPI $api
23+
*/
24+
public function __construct(TCAPI $api)
25+
{
26+
$this->api = $api;
27+
}
28+
1529
/**
1630
* Get achievements list or achievement by ID
1731
*
@@ -68,4 +82,38 @@ public function getAchievementCategories(Request $request, $id = 0) {
6882

6983
return $result;
7084
}
71-
}
85+
86+
/**
87+
* Get achievements dbc information
88+
*
89+
* @param Request $request
90+
* @param int $id
91+
* @return AchievementCategory|array
92+
*/
93+
public function getDbcAchievements(Request $request, $id = 0) {
94+
switch ($this->api->getGameVersion()) {
95+
case "wod": {
96+
if ($id)
97+
$result = \App\Models\WOD\DBC\Achievements\Achievement::findOrFail($id);
98+
else
99+
$result = \App\Models\WOD\DBC\Achievements\Achievement::all();
100+
101+
break;
102+
}
103+
case "wotlk": {
104+
if ($id)
105+
$result = \App\Models\DBC\Achievements\Achievement::findOrFail($id);
106+
else
107+
$result = \App\Models\DBC\Achievements\Achievement::all();
108+
109+
break;
110+
}
111+
default:
112+
throw new UnsupportedVersion();
113+
}
114+
115+
//TODO::Search by name and supporting translate
116+
117+
return $result;
118+
}
119+
}

app/Http/Kernel.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class Kernel extends HttpKernel
1414
* @var array
1515
*/
1616
protected $middleware = [
17+
\App\Http\Middleware\CheckVersion::class,
1718
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
18-
\App\Http\Middleware\CORSAccess::class
19+
\App\Http\Middleware\CORSAccess::class,
1920
];
2021

2122
/**
@@ -34,7 +35,9 @@ class Kernel extends HttpKernel
3435
],
3536

3637
'api' => [
37-
'throttle:60,1',
38+
\App\Http\Middleware\CheckVersion::class,
39+
\App\Http\Middleware\CORSAccess::class,
40+
'throttle:60,1'
3841
],
3942
];
4043

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use App\Exceptions\UnsupportedVersion;
6+
use App\Helpers\TCAPI;
7+
use Closure;
8+
9+
class CheckVersion
10+
{
11+
/**
12+
* CheckVersion constructor.
13+
* @param TCAPI $api
14+
*/
15+
public function __construct(TCAPI $api)
16+
{
17+
$this->api = $api;
18+
}
19+
20+
/**
21+
* Checking version
22+
*
23+
* @param \Illuminate\Http\Request $request
24+
* @param \Closure $next
25+
* @return mixed
26+
*/
27+
public function handle($request, Closure $next)
28+
{
29+
$versions = \Config::get("api.version");
30+
31+
if ($version = $request->get("version")) {
32+
33+
$foundVersion = false;
34+
array_walk($versions['versions'], function($value, $key) use (&$foundVersion, $version) {
35+
if (in_array($version, $value))
36+
$foundVersion = $key;
37+
});
38+
39+
if (!$foundVersion)
40+
throw new UnsupportedVersion();
41+
42+
app("App\Helpers\TCAPI")->setGameVersion($foundVersion);
43+
} else
44+
$this->api->setGameVersion($versions['default']);
45+
46+
return $next($request);
47+
}
48+
}
49+

0 commit comments

Comments
 (0)