Skip to content

Commit a22a15d

Browse files
Julien NahumJulien Nahum
Julien Nahum
authored and
Julien Nahum
committed
2 parents 5a274e9 + 39b6c16 commit a22a15d

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/Http/Controllers/CustomCodeController.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function executeCustomCode(Request $request, Tinker $tinker)
2424
{
2525
$validated = $request->validate([
2626
'code' => 'required',
27-
'chart_type' => ['required', Rule::in(self::CHART_TYPES)]
27+
'chart_type' => ['required', Rule::in(self::CHART_TYPES)],
2828
]);
2929

3030
$result = $tinker->injectDates(now()->subMonth(), now())
@@ -35,8 +35,10 @@ public function executeCustomCode(Request $request, Tinker $tinker)
3535
return $this->success([
3636
'output' => $result,
3737
'code_executed' => $codeExecuted,
38-
'valid_output' => $codeExecuted ? $this->isValidOutput($request->chart_type,
39-
$tinker->getCustomCodeResult()) : false
38+
'valid_output' => $codeExecuted ? $this->isValidOutput(
39+
$request->chart_type,
40+
$tinker->getCustomCodeResult()
41+
) : false,
4042
]);
4143
}
4244

@@ -65,8 +67,10 @@ public function widgetData(Request $request, Tinker $tinker)
6567
return $this->error([
6668
'output' => $result,
6769
'code_executed' => $codeExecuted,
68-
'valid_output' => $codeExecuted ? $this->isValidOutput($request->chart_type,
69-
$tinker->getCustomCodeResult()) : false
70+
'valid_output' => $codeExecuted ? $this->isValidOutput(
71+
$request->chart_type,
72+
$tinker->getCustomCodeResult()
73+
) : false,
7074
]);
7175
}
7276
}
@@ -79,32 +83,35 @@ private function isValidOutput(string $chartType, $data)
7983
case 'line_chart':
8084
return $this->validateLineChartData($data);
8185
}
86+
8287
return false;
8388
}
8489

8590
private function validateBarChartData($data)
8691
{
87-
if (!is_array($data)) {
92+
if (! is_array($data)) {
8893
return false;
8994
}
9095
foreach ($data as $key => $value) {
91-
if (!is_string($key) || !is_numeric($value)) {
96+
if (! is_string($key) || ! is_numeric($value)) {
9297
return false;
9398
}
9499
}
100+
95101
return true;
96102
}
97103

98104
private function validateLineChartData($data)
99105
{
100-
if (!is_array($data)) {
106+
if (! is_array($data)) {
101107
return false;
102108
}
103109
foreach ($data as $key => $value) {
104-
if (!is_string($key) || !is_numeric($value)) {
110+
if (! is_string($key) || ! is_numeric($value)) {
105111
return false;
106112
}
107113
}
114+
108115
return true;
109116
}
110117
}

src/Http/Middleware/CustomCodeEnabled.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace Jhumanj\LaravelModelStats\Http\Middleware;
55

6-
use Jhumanj\LaravelModelStats\LaravelModelStats;
7-
86
class CustomCodeEnabled
97
{
108
/**
@@ -16,11 +14,12 @@ class CustomCodeEnabled
1614
*/
1715
public function handle($request, $next)
1816
{
19-
if (!config('model-stats.allow_custom_code')) {
17+
if (! config('model-stats.allow_custom_code')) {
2018
return response([
2119
'message' => 'Custom code not enabled.',
22-
],403);
20+
], 403);
2321
}
22+
2423
return $next($request);
2524
}
2625
}

src/Services/Tinker.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Foundation\Application;
99
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Facades\Config;
1011
use Illuminate\Support\Facades\DB;
1112
use Illuminate\Support\Str;
1213
use Laravel\Tinker\ClassAliasAutoloader;
1314
use Psy\Configuration;
1415
use Psy\ExecutionLoopClosure;
1516
use Psy\Shell;
1617
use Symfony\Component\Console\Output\BufferedOutput;
17-
use Illuminate\Support\Facades\Config;
1818

1919
/**
2020
* Taken from https://github.com/spatie/laravel-web-tinker/blob/master/src/Tinker.php
@@ -52,7 +52,7 @@ public function execute(string $phpCode): string
5252
$resultVars = $this->shell->getScopeVariables();
5353

5454
// Detect db write exception
55-
if (!$this->lastExecSuccess() && isset($resultVars['_e'])) {
55+
if (! $this->lastExecSuccess() && isset($resultVars['_e'])) {
5656
$lastException = $resultVars['_e'];
5757
if (get_class($lastException) === 'Illuminate\Database\QueryException') {
5858
if (Str::of($lastException->getMessage())->contains(self::FAKE_WRITE_HOST)) {
@@ -72,18 +72,20 @@ public function execute(string $phpCode): string
7272
/**
7373
* Get the content of result variable
7474
*/
75-
public function getCustomCodeResult() {
76-
if (!$this->lastExecSuccess()) {
75+
public function getCustomCodeResult()
76+
{
77+
if (! $this->lastExecSuccess()) {
7778
return null;
7879
}
7980

8081
try {
8182
$result = $this->shell->getScopeVariable('result');
8283
} catch (\Exception $exception) {
8384
ray($exception);
85+
8486
return null;
8587
}
86-
if ($result && !empty($result) ){
88+
if ($result && ! empty($result)) {
8789
return $result;
8890
}
8991

@@ -93,26 +95,29 @@ public function getCustomCodeResult() {
9395
/**
9496
* Check if last execution worked without exceptions
9597
*/
96-
public function lastExecSuccess() {
98+
public function lastExecSuccess()
99+
{
97100
return $this->shell->getLastExecSuccess();
98101
}
99102

100103
/**
101104
* Prevents unwanted database modifications by enabling creating and using a readonly connection.
102105
*/
103-
public function readonly() {
106+
public function readonly()
107+
{
104108
$defaultConnection = config('database.default');
105109
$databaseConnection = Config::get('database.connections.'.$defaultConnection);
106110
$host = $databaseConnection['host'];
107111
unset($databaseConnection['host']);
108112
$databaseConnection['read'] = [
109-
'host' => $host
113+
'host' => $host,
110114
];
111115
$databaseConnection['write'] = [
112-
'host' => self::FAKE_WRITE_HOST
116+
'host' => self::FAKE_WRITE_HOST,
113117
];
114-
Config::set('database.connections.readonly',$databaseConnection);
118+
Config::set('database.connections.readonly', $databaseConnection);
115119
DB::setDefaultConnection('readonly');
120+
116121
return $this;
117122
}
118123

0 commit comments

Comments
 (0)