Skip to content

Commit 666d259

Browse files
committed
2 parents 96961ac + 14b64e7 commit 666d259

24 files changed

+199
-174
lines changed

config/model-stats.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
|
1717
*/
1818

19-
'enabled' => env('MODEL_STATS_ENABLED', true),
20-
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', true),
19+
'enabled' => env('MODEL_STATS_ENABLED', true),
20+
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', true),
2121

2222
/*
2323
|--------------------------------------------------------------------------
@@ -29,20 +29,31 @@
2929
| the existing middleware. Or, you can simply stick with this list.
3030
|
3131
*/
32-
'middleware' => [
32+
'middleware' => [
3333
'web',
3434
\Jhumanj\LaravelModelStats\Http\Middleware\Authorize::class,
3535
],
3636

37+
/*
38+
|--------------------------------------------------------------------------
39+
| ModelStats table name
40+
|--------------------------------------------------------------------------
41+
|
42+
| As PostgreSQL table names seems to use dashes instead of underscore
43+
| this configures the table name based on your connection.
44+
|
45+
*/
46+
'table_name' => 'model_stats_dashboards',
47+
3748
/*
3849
|--------------------------------------------------------------------------
3950
| Route Prefixes
4051
|--------------------------------------------------------------------------
4152
|
42-
| You can change the route where your dashboards are. By default routes will
53+
| You can change the route where your dashboards are. By default, routes will
4354
| be starting the '/stats' prefix, and names will start with 'stats.'.
4455
|
4556
*/
46-
'routes_prefix' => 'stats',
57+
'routes_prefix' => 'stats',
4758
'route_names_prefix' => 'stats.',
4859
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Jhumanj\LaravelModelStats\Database\Factories;
4+
5+
use Jhumanj\LaravelModelStats\Models\Dashboard;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class DashboardFactory extends Factory
9+
{
10+
protected $model = Dashboard::class;
11+
12+
public function definition(): array
13+
{
14+
return [
15+
'name' => $this->faker->name,
16+
'description' => $this->faker->sentence,
17+
'body' => '{"widgets":[]}',
18+
];
19+
}
20+
}

database/factories/ModelFactory.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Config;
34
use Illuminate\Database\Migrations\Migration;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Support\Facades\Schema;
67

7-
return new class extends Migration
8-
{
9-
public function up()
8+
return new class extends Migration {
9+
10+
public function up(): void
1011
{
11-
Schema::create('model-stats-dashboards', function (Blueprint $table) {
12+
Schema::create(Config::get('model-stats.table_name'), function (Blueprint $table) {
1213
$table->id();
1314
$table->string('name');
1415
$table->text('description');
15-
$table->jsonb('body')->default('{"widgets":[]}');
16+
if (Config::get('database.defaults') === 'pgsql') {
17+
$table->jsonb('body')->default('{"widgets":[]}');
18+
} else {
19+
$table->json('body');
20+
}
1621
$table->timestamps();
1722
});
1823
}
1924

20-
public function down()
25+
public function down(): void
2126
{
22-
Schema::dropIfExists('model-stats-dashboards');
27+
Schema::dropIfExists(Config::get('model-stats.table_name'));
2328
}
2429
};

src/AuthorizesRequests.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
<?php
22

3-
43
namespace Jhumanj\LaravelModelStats;
54

5+
use Closure;
6+
use Illuminate\Http\Request;
7+
68
trait AuthorizesRequests
79
{
810
/**
911
* The callback that should be used to authenticate ModelStats users.
1012
*
1113
* @var \Closure
1214
*/
13-
public static $authUsing;
15+
public static Closure $authUsing;
1416

1517
/**
1618
* Register the ModelStats authentication callback.
1719
*
18-
* @param \Closure $callback
20+
* @param \Closure $callback
21+
*
1922
* @return static
2023
*/
21-
public static function auth($callback)
24+
public static function auth(Closure $callback): static
2225
{
2326
static::$authUsing = $callback;
2427

@@ -28,13 +31,12 @@ public static function auth($callback)
2831
/**
2932
* Determine if the given request can access the ModelStats dashboard.
3033
*
31-
* @param \Illuminate\Http\Request $request
34+
* @param \Illuminate\Http\Request $request
35+
*
3236
* @return bool
3337
*/
34-
public static function check($request)
38+
public static function check(Request $request): bool
3539
{
36-
return (static::$authUsing ?: function () {
37-
return app()->environment('local');
38-
})($request);
40+
return (static::$authUsing ?: fn () => app()->environment('local'))($request);
3941
}
4042
}

src/Console/InstallModelStatsPackage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class InstallModelStatsPackage extends Command
1212

1313
protected $description = 'Install the ModelStatsPackage';
1414

15-
public function handle()
15+
public function handle(): void
1616
{
1717
$this->info('Installing ModelStats package...');
1818

@@ -35,7 +35,7 @@ public function handle()
3535
*
3636
* @return void
3737
*/
38-
private function registerModelStatsServiceProvider()
38+
private function registerModelStatsServiceProvider(): void
3939
{
4040
$namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace());
4141

src/Console/PublishCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Jhumanj\LaravelModelStats\Console;
54

65
use Illuminate\Console\Command;
@@ -11,7 +10,7 @@ class PublishCommand extends Command
1110

1211
protected $description = 'Publish all of the ModelStats resources';
1312

14-
public function handle()
13+
public function handle(): void
1514
{
1615
$this->call('vendor:publish', [
1716
'--tag' => 'model-stats-config',

src/Http/Controllers/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
22

3-
43
namespace Jhumanj\LaravelModelStats\Http\Controllers;
54

65
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
76
use Illuminate\Foundation\Validation\ValidatesRequests;
7+
use Illuminate\Http\JsonResponse;
88
use Illuminate\Routing\Controller as BaseController;
99

1010
class Controller extends BaseController
1111
{
1212
use ValidatesRequests;
1313
use AuthorizesRequests;
1414

15-
public function success($data = [])
15+
public function success($data = []): JsonResponse
1616
{
1717
return response()->json(array_merge([
1818
'type' => 'success',
1919
], $data));
2020
}
2121

22-
public function error($data = [], $statusCode = 400)
22+
public function error($data = [], $statusCode = 400): JsonResponse
2323
{
2424
return response()->json(array_merge([
2525
'type' => 'error',

src/Http/Controllers/CustomCodeController.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Jhumanj\LaravelModelStats\Http\Controllers;
44

55
use Carbon\Carbon;
6+
use Illuminate\Http\JsonResponse;
67
use Illuminate\Http\Request;
78
use Illuminate\Validation\Rule;
89
use Jhumanj\LaravelModelStats\Http\Middleware\CustomCodeEnabled;
910
use Jhumanj\LaravelModelStats\Services\Tinker;
1011

1112
class CustomCodeController extends Controller
1213
{
13-
const CHART_TYPES = ['line_chart', 'bar_chart'];
14+
public const CHART_TYPES = ['line_chart', 'bar_chart'];
1415

1516
public function __construct()
1617
{
@@ -20,7 +21,7 @@ public function __construct()
2021
/**
2122
* Endpoint used to test customCode when creating widgets.
2223
*/
23-
public function executeCustomCode(Request $request, Tinker $tinker)
24+
public function executeCustomCode(Request $request, Tinker $tinker): JsonResponse
2425
{
2526
$validated = $request->validate([
2627
'code' => 'required',
@@ -35,10 +36,8 @@ public function executeCustomCode(Request $request, Tinker $tinker)
3536
return $this->success([
3637
'output' => $result,
3738
'code_executed' => $codeExecuted,
38-
'valid_output' => $codeExecuted ? $this->isValidOutput(
39-
$request->chart_type,
40-
$tinker->getCustomCodeResult()
41-
) : false,
39+
'valid_output' => $codeExecuted
40+
&& $this->isValidOutput($request->get('chart_type'), $tinker->getCustomCodeResult()),
4241
]);
4342
}
4443

@@ -51,8 +50,8 @@ public function widgetData(Request $request, Tinker $tinker)
5150
'date_to' => 'required|date_format:Y-m-d|after:date_from',
5251
]);
5352

54-
$dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from);
55-
$dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to);
53+
$dateFrom = Carbon::createFromFormat('Y-m-d', $request->get('date_from'));
54+
$dateTo = Carbon::createFromFormat('Y-m-d', $request->get('date_to'));
5655

5756
$result = $tinker->injectDates($dateFrom, $dateTo)
5857
->readonly()
@@ -61,33 +60,28 @@ public function widgetData(Request $request, Tinker $tinker)
6160
$codeExecuted = $tinker->lastExecSuccess();
6261
$dataResult = $tinker->getCustomCodeResult();
6362

64-
if ($codeExecuted && $this->isValidOutput($request->chart_type, $dataResult)) {
63+
if ($codeExecuted && $this->isValidOutput($request->get('chart_type'), $dataResult)) {
6564
return $tinker->getCustomCodeResult();
66-
} else {
67-
return $this->error([
68-
'output' => $result,
69-
'code_executed' => $codeExecuted,
70-
'valid_output' => $codeExecuted ? $this->isValidOutput(
71-
$request->chart_type,
72-
$tinker->getCustomCodeResult()
73-
) : false,
74-
]);
7565
}
66+
67+
return $this->error([
68+
'output' => $result,
69+
'code_executed' => $codeExecuted,
70+
'valid_output' => $codeExecuted
71+
&& $this->isValidOutput($request->get('chart_type'), $tinker->getCustomCodeResult()),
72+
]);
7673
}
7774

78-
private function isValidOutput(string $chartType, $data)
75+
private function isValidOutput(string $chartType, $data): bool
7976
{
80-
switch ($chartType) {
81-
case 'bar_chart':
82-
return $this->validateBarChartData($data);
83-
case 'line_chart':
84-
return $this->validateLineChartData($data);
85-
}
86-
87-
return false;
77+
return match ($chartType) {
78+
'bar_chart' => $this->validateBarChartData($data),
79+
'line_chart' => $this->validateLineChartData($data),
80+
default => false,
81+
};
8882
}
8983

90-
private function validateBarChartData($data)
84+
private function validateBarChartData($data): bool
9185
{
9286
if (! is_array($data)) {
9387
return false;
@@ -101,7 +95,7 @@ private function validateBarChartData($data)
10195
return true;
10296
}
10397

104-
private function validateLineChartData($data)
98+
private function validateLineChartData($data): bool
10599
{
106100
if (! is_array($data)) {
107101
return false;

src/Http/Controllers/DashboardController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,39 @@
22

33
namespace Jhumanj\LaravelModelStats\Http\Controllers;
44

5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Collection;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Http\JsonResponse;
59
use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\StoreRequest;
610
use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\UpdateRequest;
711
use Jhumanj\LaravelModelStats\Models\Dashboard;
812

913
class DashboardController extends Controller
1014
{
11-
public function index()
15+
public function index(): Collection|array
1216
{
1317
return Dashboard::all();
1418
}
1519

16-
public function show(Dashboard $dashboard)
20+
public function show(Dashboard $dashboard): Dashboard
1721
{
1822
return $dashboard;
1923
}
2024

21-
public function store(StoreRequest $request)
25+
public function store(StoreRequest $request): Model|Builder
2226
{
23-
return Dashboard::create($request->validated());
27+
return Dashboard::query()->create($request->validated());
2428
}
2529

26-
public function update(Dashboard $dashboard, UpdateRequest $request)
30+
public function update(Dashboard $dashboard, UpdateRequest $request): Dashboard
2731
{
2832
$dashboard->update($request->validated());
2933

3034
return $dashboard;
3135
}
3236

33-
public function destroy(Dashboard $dashboard)
37+
public function destroy(Dashboard $dashboard): JsonResponse
3438
{
3539
$dashboard->delete();
3640

0 commit comments

Comments
 (0)