Skip to content

Commit 86e7085

Browse files
committed
Cleaned up the controller and prepared for tests
1 parent 095d85b commit 86e7085

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,11 @@ public function insert(array $data): bool;
200200
* @return bool
201201
*/
202202
public function insertIgnore(array $values): bool;
203+
204+
/**
205+
* Get the amount of entries in the database
206+
*
207+
* @return int
208+
*/
209+
public function count(): int;
203210
}

app/Http/Controllers/Admin/StatisticsController.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,75 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\DB;
77
use JavaScript;
8+
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
9+
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
10+
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
11+
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
12+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
13+
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
814
use Pterodactyl\Http\Controllers\Controller;
9-
use Pterodactyl\Models\Allocation;
10-
use Pterodactyl\Models\Database;
11-
use Pterodactyl\Models\Egg;
12-
use Pterodactyl\Models\Node;
13-
use Pterodactyl\Models\Server;
14-
use Pterodactyl\Models\User;
1515
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
16+
use Pterodactyl\Traits\Controllers\JavascriptInjection;
1617

1718
class StatisticsController extends Controller
1819
{
20+
use JavascriptInjection;
21+
22+
private $allocationRepository;
23+
24+
private $databaseRepository;
1925

2026
private $keyProviderService;
2127

22-
function __construct(DaemonKeyProviderService $keyProviderService)
28+
private $eggRepository;
29+
30+
private $nodeRepository;
31+
32+
private $serverRepository;
33+
34+
private $userRepository;
35+
36+
function __construct(
37+
AllocationRepositoryInterface $allocationRepository,
38+
DatabaseRepositoryInterface $databaseRepository,
39+
DaemonKeyProviderService $keyProviderService,
40+
EggRepositoryInterface $eggRepository,
41+
NodeRepositoryInterface $nodeRepository,
42+
ServerRepositoryInterface $serverRepository,
43+
UserRepositoryInterface $userRepository
44+
)
2345
{
46+
$this->allocationRepository = $allocationRepository;
47+
$this->databaseRepository = $databaseRepository;
2448
$this->keyProviderService = $keyProviderService;
49+
$this->eggRepository = $eggRepository;
50+
$this->nodeRepository = $nodeRepository;
51+
$this->serverRepository = $serverRepository;
52+
$this->userRepository = $userRepository;
2553
}
2654

2755
public function index(Request $request)
2856
{
29-
$servers = Server::all();
30-
$nodes = Node::all();
57+
$servers = $this->serverRepository->all();
3158
$serversCount = count($servers);
59+
$nodes = $this->nodeRepository->all();
3260
$nodesCount = count($nodes);
33-
$usersCount = User::count();
34-
$eggsCount = Egg::count();
35-
$databasesCount = Database::count();
61+
$usersCount = $this->userRepository->count();
62+
$eggsCount = $this->eggRepository->count();
63+
$databasesCount = $this->databaseRepository->count();
3664
$totalServerRam = DB::table('servers')->sum('memory');
3765
$totalNodeRam = DB::table('nodes')->sum('memory');
3866
$totalServerDisk = DB::table('servers')->sum('disk');
3967
$totalNodeDisk = DB::table('nodes')->sum('disk');
40-
$totalAllocations = Allocation::count();
41-
42-
$suspendedServersCount = Server::where('suspended', true)->count();
68+
$totalAllocations = $this->allocationRepository->count();
69+
$suspendedServersCount = $this->serverRepository->getBuilder()->where('suspended', true)->count();
4370

4471
$tokens = [];
4572
foreach ($nodes as $node) {
46-
$server = Server::where('node_id', $node->id)->first();
47-
if ($server == null)
48-
continue;
49-
50-
$tokens[$node->id] = $this->keyProviderService->handle($server, $request->user());
73+
$tokens[$node->id] = $node->daemonSecret;
5174
}
5275

53-
Javascript::put([
76+
$this->injectJavascript([
5477
'servers' => $servers,
5578
'serverCount' => $serversCount,
5679
'suspendedServers' => $suspendedServersCount,

app/Repositories/Eloquent/EloquentRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,14 @@ public function insertIgnore(array $values): bool
296296

297297
return $this->getBuilder()->getConnection()->statement($statement, $bindings);
298298
}
299+
300+
/**
301+
* Get the amount of entries in the database
302+
*
303+
* @return int
304+
*/
305+
public function count(): int
306+
{
307+
return $this->getBuilder()->count();
308+
}
299309
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Stan
5+
* Date: 26-5-2018
6+
* Time: 20:56
7+
*/
8+
9+
namespace Pterodactyl\Traits\Controllers;
10+
11+
use JavaScript;
12+
13+
class JavascriptStatisticsInjection
14+
{
15+
16+
/**
17+
* Injects statistics into javascript
18+
*/
19+
public function injectJavascript($data)
20+
{
21+
Javascript::put($data);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)