Skip to content

Commit

Permalink
Run pint --preset=laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
mtawil committed Aug 24, 2023
1 parent d8d36a3 commit 9164f6c
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 75 deletions.
12 changes: 6 additions & 6 deletions config/model-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
|
*/

'enabled' => env('MODEL_STATS_ENABLED', true),
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', false),
'enabled' => env('MODEL_STATS_ENABLED', true),
'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', false),

/*
|--------------------------------------------------------------------------
Expand All @@ -29,7 +29,7 @@
| the existing middleware. Or, you can simply stick with this list.
|
*/
'middleware' => [
'middleware' => [
'web',
\Jhumanj\LaravelModelStats\Http\Middleware\Authorize::class,
],
Expand All @@ -43,7 +43,7 @@
| this configures the table name based on your connection.
|
*/
'table_name' => 'model_stats_dashboards',
'table_name' => 'model_stats_dashboards',

/*
|--------------------------------------------------------------------------
Expand All @@ -54,7 +54,7 @@
| This can be used to ensure a read-only connection, by using a custom connection with a read-only user.
|
*/
'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')),
'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')),

/*
|--------------------------------------------------------------------------
Expand All @@ -65,6 +65,6 @@
| be starting the '/stats' prefix, and names will start with 'stats.'.
|
*/
'routes_prefix' => 'stats',
'routes_prefix' => 'stats',
'route_names_prefix' => 'stats.',
];
6 changes: 3 additions & 3 deletions database/factories/DashboardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Jhumanj\LaravelModelStats\Database\Factories;

use Jhumanj\LaravelModelStats\Models\Dashboard;
use Illuminate\Database\Eloquent\Factories\Factory;
use Jhumanj\LaravelModelStats\Models\Dashboard;

class DashboardFactory extends Factory
{
Expand All @@ -12,9 +12,9 @@ class DashboardFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->name,
'name' => $this->faker->name,
'description' => $this->faker->sentence,
'body' => '{"widgets":[]}',
'body' => '{"widgets":[]}',
];
}
}
6 changes: 3 additions & 3 deletions database/migrations/create_model-stats_table.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use Illuminate\Support\Facades\Config;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {

return new class() extends Migration
{
public function up(): void
{
Schema::create(Config::get('model-stats.table_name'), function (Blueprint $table) {
Expand Down
18 changes: 12 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

Route::prefix('api')->name('api.')->group(function () {
Route::apiResource('dashboards', \Jhumanj\LaravelModelStats\Http\Controllers\DashboardController::class);
Route::post('widgets/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData']);
Route::post('widgets/custom-code/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData']);
Route::post(
'widgets/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData']
);
Route::post(
'widgets/custom-code/data',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData']
);

Route::post('widgets/custom-code/execute',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode']);
Route::post(
'widgets/custom-code/execute',
[\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode']
);
});

Route::get('/{view?}', [\Jhumanj\LaravelModelStats\Http\Controllers\HomeController::class, 'home'])
Expand Down
12 changes: 1 addition & 11 deletions src/AuthorizesRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,21 @@ trait AuthorizesRequests
{
/**
* The callback that should be used to authenticate ModelStats users.
*
* @var \Closure
*/
public static Closure $authUsing;

/**
* Register the ModelStats authentication callback.
*
* @param \Closure $callback
*
* @return static
*/
public static function auth(Closure $callback): static
{
static::$authUsing = $callback;

return new static;
return new static();
}

/**
* Determine if the given request can access the ModelStats dashboard.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public static function check(Request $request): bool
{
Expand Down
3 changes: 0 additions & 3 deletions src/Console/InstallModelStatsPackage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Jhumanj\LaravelModelStats\Console;

use Illuminate\Console\Command;
Expand Down Expand Up @@ -32,8 +31,6 @@ public function handle(): void

/**
* Register the ModelStats service provider in the application configuration file.
*
* @return void
*/
private function registerModelStatsServiceProvider(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function destroy(Dashboard $dashboard): JsonResponse
$dashboard->delete();

return $this->success([
'message' => 'Dashboard deleted',
'message' => 'Dashboard deleted',
]);
}
}
5 changes: 2 additions & 3 deletions src/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ private function getModels(): Collection
if (class_exists($class)) {
$reflection = new ReflectionClass($class);
$valid = $reflection->isSubclassOf(Model::class) &&
!$reflection->isAbstract();
! $reflection->isAbstract();
}

return $valid;
});


return $models->map(fn (string $class) => [
'class' => $class,
'fields' => $this->getClassFields($class),
Expand All @@ -65,6 +64,6 @@ private function getModels(): Collection

private function getClassFields(string $class)
{
return Schema::getColumnListing((new $class)->getTable());
return Schema::getColumnListing((new $class())->getTable());
}
}
5 changes: 2 additions & 3 deletions src/Http/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class Authorize
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*/
public function handle($request, $next)
Expand Down
8 changes: 3 additions & 5 deletions src/Http/Middleware/CustomCodeEnabled.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php


namespace Jhumanj\LaravelModelStats\Http\Middleware;

class CustomCodeEnabled
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function handle($request, $next)
{
if (!config('model-stats.allow_custom_code')) {
if (! config('model-stats.allow_custom_code')) {
return response([
'message' => 'Custom code not enabled.',
], 403);
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Requests/Dashboard/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class StoreRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Requests/Dashboard/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class UpdateRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand Down
4 changes: 1 addition & 3 deletions src/Http/Requests/Widgets/DataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class DataRequest extends FormRequest

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
Expand Down Expand Up @@ -63,7 +61,7 @@ private function getModels(): Collection
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$valid = $reflection->isSubclassOf(Model::class)
&& !$reflection->isAbstract();
&& ! $reflection->isAbstract();
}

return $valid;
Expand Down
8 changes: 2 additions & 6 deletions src/LaravelModelStatsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ public function configurePackage(Package $package): void

/**
* Register the package's publishable resources.
*
* @return void
*/
private function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../stubs/ModelStatsServiceProvider.stub' => app_path('Providers/ModelStatsServiceProvider.php'),
__DIR__.'/../stubs/ModelStatsServiceProvider.stub' => app_path('Providers/ModelStatsServiceProvider.php'),
], 'model-stats-provider');

$this->publishes([
Expand All @@ -60,8 +58,6 @@ private function registerPublishing(): void

/**
* Register the package's commands.
*
* @return void
*/
protected function registerCommands(): void
{
Expand All @@ -78,6 +74,6 @@ protected function registerCommands(): void
*/
protected function loadMigrations(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}
6 changes: 0 additions & 6 deletions src/ModelStatsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class ModelStatsServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
Expand All @@ -20,8 +18,6 @@ public function boot(): void

/**
* Configure the ModelStats authorization services.
*
* @return void
*/
protected function authorization(): void
{
Expand All @@ -37,8 +33,6 @@ protected function authorization(): void
* Register the ModelStats gate.
*
* This gate determines who can access ModelStats in non-local environments.
*
* @return void
*/
protected function gate(): void
{
Expand Down
10 changes: 5 additions & 5 deletions src/Services/ModelStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function getDailyHistogram(
}

$data = $dataQuery->select([
DB::raw('DATE(' . $dateFieldName . ') as date'),
DB::raw('DATE('.$dateFieldName.') as date'),
DB::raw('COUNT(*) as "count"'),
])->get()->pluck("count", "date");
])->get()->pluck('count', 'date');

$cumulatedSum = null;
if ($cumulated) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public function getGroupByCount(
string $dateFieldName,
string $aggregateColumn
): array {
$tableName = (new $this->class)->getTable();
$tableName = (new $this->class())->getTable();

$mapping = [];
DB::table($tableName)->where($dateFieldName, '>=', $from->startOfDay())
Expand All @@ -106,11 +106,11 @@ public static function fillMissingDays($data, Carbon $since, Carbon $to, $defaul

foreach (array_reverse(range(0, $daysSince)) as $number) {
$dateKey = $to->copy()->subDays($number)->format('Y-m-d');
if (!isset($data[$dateKey])) {
if (! isset($data[$dateKey])) {
$data[$dateKey] = $defaultValue;
}

if (!is_null($cumulatedSum)) {
if (! is_null($cumulatedSum)) {
$data[$dateKey] += $cumulatedSum;
$cumulatedSum = $data[$dateKey];
}
Expand Down
9 changes: 2 additions & 7 deletions src/Services/Tinker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Jhumanj\LaravelModelStats\Services;

use Carbon\Carbon;
Expand All @@ -21,15 +20,11 @@
* Taken from https://github.com/spatie/laravel-web-tinker/blob/master/src/Tinker.php
*
* Class Tinker
* @package Jhumanj\LaravelModelStats\Services
*/
class Tinker
{

/** @var \Symfony\Component\Console\Output\BufferedOutput */
protected BufferedOutput $output;

/** @var \Psy\Shell */
protected Shell $shell;

public function __construct()
Expand All @@ -56,8 +51,8 @@ public function execute(string $phpCode): string
$lastException = $resultVars['_e'];
if (($lastException instanceof QueryException)
&& Str::of($lastException->getMessage())
->contains("SQLSTATE[42501]: Insufficient privilege")) {
return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden.";
->contains('SQLSTATE[42501]: Insufficient privilege')) {
return 'For safety reasons, you can only query data with ModelStats. Write operations are forbidden.';
}
}

Expand Down

0 comments on commit 9164f6c

Please sign in to comment.