Skip to content

Commit f054c27

Browse files
authored
Merge pull request #8 from spotlibs/feature/exploration
Feature/exploration
2 parents 3d248c1 + 91fe4d3 commit f054c27

File tree

12 files changed

+220
-96
lines changed

12 files changed

+220
-96
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
"rregeer/phpunit-coverage-check": "^0.3.1"
2424
},
2525
"scripts": {
26-
"test": "./vendor/bin/phpunit --configuration phpunit.xml --coverage-clover test-reports/cov.xml && php prepare-coverage-report.php && ./vendor/bin/coverage-check test-reports/cov.xml 40"
26+
"test": "./vendor/bin/phpunit --configuration phpunit.xml --coverage-clover report_coverage.xml && php prepare-coverage-report.php && ./vendor/bin/coverage-check report_coverage.xml 40"
2727
}
2828
}

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
<directory suffix=".php">src</directory>
2525
</include>
2626
<exclude>
27+
<file>src/Exceptions/Handler.php</file>
2728
<directory suffix=".php">src/Libraries</directory>
29+
<directory suffix=".php">src/Providers</directory>
2830
<directory suffix=".php">src/Validations</directory>
2931
</exclude>
3032
</coverage>

prepare-coverage-report.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
// Path to the Clover XML file
3-
$coverageFile = './test-reports/cov.xml';
3+
$coverageFile = './report_coverage.xml';
44

55
// Load the file contents
66
$xmlContent = file_get_contents($coverageFile);

test-reports/cov.xml renamed to report_coverage.xml

Lines changed: 51 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<coverage generated="1724812740">
3-
<project timestamp="1724812740">
2+
<coverage generated="1725426490">
3+
<project timestamp="1725426490">
4+
<file name="./src/Dtos/TraitDtos.php">
5+
<class name="Spotlibs\PhpLib\Dtos\TraitDtos" namespace="global">
6+
<metrics complexity="5" methods="4" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="7" coveredstatements="0" elements="11" coveredelements="0"/>
7+
</class>
8+
<line num="20" type="method" name="__construct" visibility="public" complexity="2" crap="6" count="0"/>
9+
<line num="21" type="stmt" count="0"/>
10+
<line num="22" type="stmt" count="0"/>
11+
<line num="26" type="method" name="create" visibility="public" complexity="1" crap="2" count="0"/>
12+
<line num="28" type="stmt" count="0"/>
13+
<line num="30" type="stmt" count="0"/>
14+
<line num="33" type="method" name="toArray" visibility="public" complexity="1" crap="2" count="0"/>
15+
<line num="35" type="stmt" count="0"/>
16+
<line num="38" type="method" name="toJson" visibility="public" complexity="1" crap="2" count="0"/>
17+
<line num="40" type="stmt" count="0"/>
18+
<line num="41" type="stmt" count="0"/>
19+
<metrics loc="43" ncloc="33" classes="1" methods="4" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="7" coveredstatements="0" elements="11" coveredelements="0"/>
20+
</file>
421
<file name="./src/Exceptions/AccessException.php">
522
<class name="Spotlibs\PhpLib\Exceptions\AccessException" namespace="global">
623
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="5" coveredstatements="5" elements="6" coveredelements="6"/>
@@ -54,7 +71,7 @@
5471
</file>
5572
<file name="./src/Exceptions/ParameterException.php">
5673
<class name="Spotlibs\PhpLib\Exceptions\ParameterException" namespace="global">
57-
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="6" elements="7" coveredelements="7"/>
74+
<metrics complexity="2" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="7" coveredstatements="7" elements="9" coveredelements="9"/>
5875
</class>
5976
<line num="51" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="1"/>
6077
<line num="53" type="stmt" count="1"/>
@@ -63,7 +80,9 @@
6380
<line num="56" type="stmt" count="1"/>
6481
<line num="57" type="stmt" count="1"/>
6582
<line num="58" type="stmt" count="1"/>
66-
<metrics loc="61" ncloc="33" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="6" coveredstatements="6" elements="7" coveredelements="7"/>
83+
<line num="66" type="method" name="getValidationErrors" visibility="public" complexity="1" crap="1" count="1"/>
84+
<line num="68" type="stmt" count="1"/>
85+
<metrics loc="71" ncloc="38" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="7" coveredstatements="7" elements="9" coveredelements="9"/>
6786
</file>
6887
<file name="./src/Exceptions/RuntimeException.php">
6988
<class name="Spotlibs\PhpLib\Exceptions\RuntimeException" namespace="global">
@@ -82,26 +101,26 @@
82101
<metrics complexity="11" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="19" coveredstatements="18" elements="20" coveredelements="18"/>
83102
</class>
84103
<line num="41" type="method" name="create" visibility="public" complexity="11" crap="11.02" count="11"/>
85-
<line num="48" type="stmt" count="11"/>
86-
<line num="49" type="stmt" count="1"/>
87-
<line num="50" type="stmt" count="10"/>
88-
<line num="51" type="stmt" count="1"/>
89-
<line num="52" type="stmt" count="9"/>
90-
<line num="53" type="stmt" count="1"/>
91-
<line num="54" type="stmt" count="8"/>
92-
<line num="55" type="stmt" count="1"/>
93-
<line num="56" type="stmt" count="7"/>
94-
<line num="57" type="stmt" count="3"/>
95-
<line num="58" type="stmt" count="4"/>
96-
<line num="59" type="stmt" count="1"/>
97-
<line num="60" type="stmt" count="3"/>
98-
<line num="61" type="stmt" count="1"/>
99-
<line num="62" type="stmt" count="2"/>
100-
<line num="63" type="stmt" count="1"/>
104+
<line num="49" type="stmt" count="11"/>
105+
<line num="50" type="stmt" count="1"/>
106+
<line num="51" type="stmt" count="10"/>
107+
<line num="52" type="stmt" count="1"/>
108+
<line num="53" type="stmt" count="9"/>
109+
<line num="54" type="stmt" count="1"/>
110+
<line num="55" type="stmt" count="8"/>
111+
<line num="56" type="stmt" count="1"/>
112+
<line num="57" type="stmt" count="7"/>
113+
<line num="58" type="stmt" count="3"/>
114+
<line num="59" type="stmt" count="4"/>
115+
<line num="60" type="stmt" count="1"/>
116+
<line num="61" type="stmt" count="3"/>
117+
<line num="62" type="stmt" count="1"/>
118+
<line num="63" type="stmt" count="2"/>
101119
<line num="64" type="stmt" count="1"/>
102120
<line num="65" type="stmt" count="1"/>
103-
<line num="67" type="stmt" count="0"/>
104-
<metrics loc="70" ncloc="60" classes="1" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="19" coveredstatements="18" elements="20" coveredelements="18"/>
121+
<line num="66" type="stmt" count="1"/>
122+
<line num="68" type="stmt" count="0"/>
123+
<metrics loc="71" ncloc="61" classes="1" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="19" coveredstatements="18" elements="20" coveredelements="18"/>
105124
</file>
106125
<file name="./src/Exceptions/ThirdPartyServiceException.php">
107126
<class name="Spotlibs\PhpLib\Exceptions\ThirdPartyServiceException" namespace="global">
@@ -163,85 +182,28 @@
163182
</file>
164183
<file name="./src/Responses/StdResponse.php">
165184
<class name="Spotlibs\PhpLib\Responses\StdResponse" namespace="global">
166-
<metrics complexity="4" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="14" coveredstatements="14" elements="16" coveredelements="16"/>
185+
<metrics complexity="5" methods="2" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="16" coveredstatements="15" elements="18" coveredelements="16"/>
167186
</class>
168-
<line num="35" type="method" name="success" visibility="public" complexity="2" crap="2" count="1"/>
169-
<line num="37" type="stmt" count="1"/>
187+
<line num="36" type="method" name="success" visibility="public" complexity="2" crap="2" count="1"/>
170188
<line num="38" type="stmt" count="1"/>
171189
<line num="39" type="stmt" count="1"/>
172190
<line num="40" type="stmt" count="1"/>
173191
<line num="41" type="stmt" count="1"/>
174192
<line num="42" type="stmt" count="1"/>
175-
<line num="44" type="stmt" count="1"/>
176-
<line num="54" type="method" name="error" visibility="public" complexity="2" crap="2" count="2"/>
193+
<line num="43" type="stmt" count="1"/>
194+
<line num="45" type="stmt" count="1"/>
195+
<line num="54" type="method" name="error" visibility="public" complexity="3" crap="3.01" count="2"/>
177196
<line num="56" type="stmt" count="2"/>
178197
<line num="57" type="stmt" count="2"/>
179198
<line num="58" type="stmt" count="2"/>
180199
<line num="59" type="stmt" count="2"/>
181200
<line num="60" type="stmt" count="2"/>
182201
<line num="61" type="stmt" count="1"/>
183-
<line num="64" type="stmt" count="2"/>
184-
<metrics loc="66" ncloc="43" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="14" coveredstatements="14" elements="16" coveredelements="16"/>
185-
</file>
186-
<file name="./src/Validation/StdValidation.php">
187-
<class name="Spotlibs\PhpLib\Validation\StdValidation" namespace="global">
188-
<metrics complexity="10" methods="7" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="31" coveredstatements="0" elements="38" coveredelements="0"/>
189-
</class>
190-
<line num="12" type="method" name="getHeaderFromRules" visibility="public" complexity="4" crap="20" count="0"/>
191-
<line num="14" type="stmt" count="0"/>
192-
<line num="15" type="stmt" count="0"/>
193-
<line num="16" type="stmt" count="0"/>
194-
<line num="17" type="stmt" count="0"/>
195-
<line num="18" type="stmt" count="0"/>
196-
<line num="20" type="stmt" count="0"/>
197-
<line num="23" type="stmt" count="0"/>
198-
<line num="26" type="method" name="validateNIK" visibility="public" complexity="1" crap="2" count="0"/>
199-
<line num="28" type="stmt" count="0"/>
200-
<line num="29" type="stmt" count="0"/>
201-
<line num="30" type="stmt" count="0"/>
202-
<line num="31" type="stmt" count="0"/>
203-
<line num="34" type="method" name="validateNPWP" visibility="public" complexity="1" crap="2" count="0"/>
204-
<line num="36" type="stmt" count="0"/>
205-
<line num="37" type="stmt" count="0"/>
206-
<line num="38" type="stmt" count="0"/>
207-
<line num="39" type="stmt" count="0"/>
208-
<line num="42" type="method" name="validateTanggalLahir" visibility="public" complexity="1" crap="2" count="0"/>
209-
<line num="44" type="stmt" count="0"/>
210-
<line num="45" type="stmt" count="0"/>
211-
<line num="46" type="stmt" count="0"/>
212-
<line num="47" type="stmt" count="0"/>
213-
<line num="50" type="method" name="validateJenisKelamin" visibility="public" complexity="1" crap="2" count="0"/>
214-
<line num="52" type="stmt" count="0"/>
215-
<line num="53" type="stmt" count="0"/>
216-
<line num="54" type="stmt" count="0"/>
217-
<line num="55" type="stmt" count="0"/>
218-
<line num="58" type="method" name="validateAgama" visibility="public" complexity="1" crap="2" count="0"/>
219-
<line num="60" type="stmt" count="0"/>
220-
<line num="61" type="stmt" count="0"/>
221-
<line num="62" type="stmt" count="0"/>
222-
<line num="63" type="stmt" count="0"/>
223-
<line num="66" type="method" name="validateNoHP" visibility="public" complexity="1" crap="2" count="0"/>
224-
<line num="68" type="stmt" count="0"/>
225-
<line num="69" type="stmt" count="0"/>
226-
<line num="70" type="stmt" count="0"/>
227-
<line num="71" type="stmt" count="0"/>
228-
<metrics loc="73" ncloc="73" classes="1" methods="7" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="31" coveredstatements="0" elements="38" coveredelements="0"/>
229-
</file>
230-
<file name="./src/Validation/ValidationServiceProvider.php">
231-
<class name="Spotlibs\PhpLib\Validation\ValidationServiceProvider" namespace="global">
232-
<metrics complexity="1" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="8" coveredstatements="0" elements="9" coveredelements="0"/>
233-
</class>
234-
<line num="167" type="method" name="register" visibility="public" complexity="1" crap="2" count="0"/>
235-
<line num="169" type="stmt" count="0"/>
236-
<line num="170" type="stmt" count="0"/>
237-
<line num="171" type="stmt" count="0"/>
238-
<line num="172" type="stmt" count="0"/>
239-
<line num="173" type="stmt" count="0"/>
240-
<line num="174" type="stmt" count="0"/>
241-
<line num="175" type="stmt" count="0"/>
242-
<line num="176" type="stmt" count="0"/>
243-
<metrics loc="180" ncloc="145" classes="1" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="8" coveredstatements="0" elements="9" coveredelements="0"/>
202+
<line num="63" type="stmt" count="2"/>
203+
<line num="64" type="stmt" count="0"/>
204+
<line num="67" type="stmt" count="2"/>
205+
<metrics loc="69" ncloc="46" classes="1" methods="2" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="16" coveredstatements="15" elements="18" coveredelements="16"/>
244206
</file>
245-
<metrics files="16" loc="1055" ncloc="666" classes="15" methods="25" coveredmethods="15" conditionals="0" coveredconditionals="0" statements="123" coveredstatements="82" elements="148" coveredelements="97"/>
207+
<metrics files="15" loc="859" ncloc="490" classes="14" methods="22" coveredmethods="15" conditionals="0" coveredconditionals="0" statements="94" coveredstatements="84" elements="116" coveredelements="99"/>
246208
</project>
247209
</coverage>

src/Dtos/TraitDtos.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8
5+
*
6+
* @category Library
7+
* @package Exceptions
8+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.0.4
11+
* @link https://github.com/spotlibs
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Spotlibs\PhpLib\Dtos;
17+
18+
trait TraitDtos
19+
{
20+
public function __construct(array $data = []) {
21+
foreach ($data as $key => $value) {
22+
$this->{$key} = $value;
23+
}
24+
}
25+
26+
public static function create(array $data): mixed
27+
{
28+
$self = new self($data);
29+
30+
return $self;
31+
}
32+
33+
public function toArray(): array
34+
{
35+
return (array) $this;
36+
}
37+
38+
public function toJson()
39+
{
40+
$data = $this->toArray();
41+
return json_encode($data);
42+
}
43+
}

src/Exceptions/Handler.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spotlibs\PhpLib\Exceptions;
6+
7+
use Exception;
8+
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
9+
use Illuminate\Support\Facades\Log;
10+
use Illuminate\Auth\Access\AuthorizationException;
11+
use Illuminate\Database\Eloquent\ModelNotFoundException;
12+
use Illuminate\Database\QueryException;
13+
use Illuminate\Validation\ValidationException;
14+
use Illuminate\Http\Response;
15+
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17+
use Spotlibs\PhpLib\Exceptions\DataNotFoundException;
18+
use Spotlibs\PhpLib\Responses\StdResponse;
19+
use Throwable;
20+
21+
22+
class Handler extends ExceptionHandler
23+
{
24+
/**
25+
* A list of the exception types that should not be reported.
26+
*
27+
* @var array
28+
*/
29+
protected $dontReport = [
30+
AuthorizationException::class,
31+
HttpException::class,
32+
ValidationException::class
33+
];
34+
35+
/**
36+
* Report or log an exception.
37+
*
38+
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
39+
*
40+
* @param \Throwable $exception
41+
* @return void
42+
*
43+
* @throws \Exception
44+
*/
45+
public function report(Throwable $exception)
46+
{
47+
// parent::report($exception);
48+
if(!$exception instanceof ExceptionInterface && !$exception instanceof NotFoundHttpException && !$exception instanceof ValidationException) {
49+
if (!config('app.debug')) {
50+
Log::channel('runtime')->error('['.$exception->getCode().'] "'.$exception->getMessage().'" on line '.$exception->getLine().' of file '.$exception->getFile().' [requestID:'.(app()->request->header('X-Request-ID') ?? null).']');
51+
} else {
52+
parent::report($exception);
53+
}
54+
}
55+
}
56+
57+
/**
58+
* Render an exception into an HTTP response.
59+
*
60+
* @param mixed $request
61+
* @param \Throwable $exception
62+
* @return \Illuminate\Http\Response
63+
*
64+
* @throws \Throwable
65+
*/
66+
public function render($request, Throwable $exception): Response
67+
{
68+
if($exception instanceof NotFoundHttpException || $exception instanceof HttpException)
69+
{
70+
$exception = new UnsupportedException("Route --".$request->getPathInfo()."-- not found in ".ENV('APP_NAME')."!");
71+
}
72+
73+
if($exception instanceof ModelNotFoundException) {
74+
$exception = new DataNotFoundException("Data tidak ditemukan");
75+
}
76+
77+
$responseValidation = [];
78+
if($exception instanceof ValidationException) {
79+
foreach($exception->errors() as $value)
80+
{
81+
array_push($responseValidation, implode(",", $value));
82+
}
83+
$exception = new ParameterException("Parameter inputan anda salah! " . $responseValidation[0] ?? null, null, $responseValidation);
84+
}
85+
86+
if($exception instanceof QueryException && ENV('APP_ENV') === 'production') {
87+
$exception = new Exception("Query exception happens, see runtime error for more details.");
88+
}
89+
if(!$exception instanceof ExceptionInterface) {
90+
$exception = new RuntimeException(
91+
(ENV('APP_DEBUG') === true ? $exception->getMessage().' '.$exception->getFile().' Ln.'.$exception->getLine() : 'Terjadi kesalahan, mohon coba beberapa saat lagi yaa...')
92+
);
93+
}
94+
95+
return StdResponse::error($exception);
96+
}
97+
}

src/Exceptions/ParameterException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ParameterException extends Exception implements ExceptionInterface
3838
private string $errorMessage;
3939
private int $httpCode;
4040
private mixed $data;
41-
public array $validationErrors;
41+
private array $validationErrors;
4242

4343
/**
4444
* Create a new ParameterException instance.
@@ -57,4 +57,14 @@ public function __construct(?string $message, mixed $data = null, array $validat
5757
$this->validationErrors = $validationErrors;
5858
parent::__construct($this->errorMessage, 1, null);
5959
}
60+
61+
/**
62+
* Get validation errors only for Parameter Exception type
63+
*
64+
* @return mixed
65+
*/
66+
public function getValidationErrors(): mixed
67+
{
68+
return $this->validationErrors;
69+
}
6070
}

src/Exceptions/StdException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class StdException
4141
public static function create(
4242
string $responseCode,
4343
?string $responseDesc = null,
44-
mixed $responseData = null
44+
mixed $responseData = null,
45+
array $validationErrors = []
4546
): ExceptionInterface
4647
{
4748
switch ($responseCode) {
@@ -50,7 +51,7 @@ public static function create(
5051
case self::ACCESS_EXCEPTION:
5152
return new AccessException($responseDesc, $responseData);
5253
case self::PARAMETER_EXCEPTION:
53-
return new ParameterException($responseDesc, $responseData);
54+
return new ParameterException($responseDesc, $responseData, $validationErrors);
5455
case self::NOTFOUND_EXCEPTION:
5556
return new DataNotFoundException($responseDesc, $responseData);
5657
case self::INVALIDRULE_EXCEPTION:

0 commit comments

Comments
 (0)