-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Migrated some support/exception classes from marketplace-skeleton
🔥 Deprecated JetBrains attributes in favor of psalm
- Loading branch information
1 parent
0666188
commit dddd048
Showing
13 changed files
with
334 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Throwable; | ||
|
||
abstract class AbstractRequestException extends InvalidArgumentException | ||
{ | ||
/** | ||
* @psalm-pure | ||
*/ | ||
public function __construct( | ||
protected string $title, | ||
string $detail, | ||
int $code = 0, | ||
Throwable $previous = null, | ||
) { | ||
parent::__construct($detail, $code, $previous); | ||
} | ||
|
||
public function render(): Response | ||
{ | ||
return response()->json([ | ||
'errors' => [ | ||
[ | ||
'status' => (string) $this->code, | ||
'title' => $this->title, | ||
'detail' => $this->message, | ||
], | ||
], | ||
], $this->code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Exceptions; | ||
|
||
use Throwable; | ||
|
||
class RequestInputException extends AbstractRequestException | ||
{ | ||
public function __construct( | ||
protected string $title, | ||
string $detail, | ||
int $code = 400, | ||
Throwable $previous = null, | ||
) { | ||
parent::__construct($title, $detail, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Exceptions; | ||
|
||
use Throwable; | ||
|
||
class RequestUnauthorizedException extends AbstractRequestException | ||
{ | ||
public function __construct( | ||
protected string $title, | ||
string $detail, | ||
int $code = 401, | ||
Throwable $previous = null, | ||
) { | ||
parent::__construct($title, $detail, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest as IlluminateFormRequest; | ||
use MyParcelCom\Integration\Exceptions\RequestInputException; | ||
use MyParcelCom\Integration\ShopId; | ||
use Ramsey\Uuid\Exception\InvalidUuidStringException; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class FormRequest extends IlluminateFormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function shopId(): ShopId | ||
{ | ||
$shopId = $this->query('shop_id'); | ||
|
||
if (!$shopId) { | ||
throw new RequestInputException('Bad request', 'No shop_id provided in the request query'); | ||
} | ||
|
||
try { | ||
$shopUuid = Uuid::fromString($shopId); | ||
} catch (InvalidUuidStringException) { | ||
throw new RequestInputException('Unprocessable entity', 'shop_id is not a valid UUID', 422); | ||
} | ||
|
||
return new ShopId($shopUuid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Support; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
use MyParcelCom\Integration\ShopId; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* @implements CastsAttributes<ShopId,ShopId> | ||
*/ | ||
class ShopIdCaster implements CastsAttributes | ||
{ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): ShopId | ||
{ | ||
return new ShopId(Uuid::fromString($value)); | ||
} | ||
|
||
public function set(Model $model, string $key, mixed $value, array $attributes): string | ||
{ | ||
return $value->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Http\Requests; | ||
|
||
use MyParcelCom\Integration\Http\Requests\FormRequest; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FormRequestTest extends TestCase | ||
{ | ||
public function testShopId(): void | ||
{ | ||
$request = new FormRequest(); | ||
$request->initialize(['shop_id' => '123e4567-e89b-12d3-a456-426614174000']); | ||
|
||
$this->assertEquals('123e4567-e89b-12d3-a456-426614174000', $request->shopId()->toString()); | ||
} | ||
|
||
public function testShopIdThrowsExceptionWhenNoShopIdProvided(): void | ||
{ | ||
$request = new FormRequest(); | ||
|
||
$this->expectExceptionMessage('No shop_id provided in the request query'); | ||
$request->shopId(); | ||
} | ||
|
||
public function testShopIdThrowsExceptionWhenShopIdIsNotValidUuid(): void | ||
{ | ||
$request = new FormRequest(); | ||
$request->initialize(['shop_id' => 'invalid-uuid']); | ||
|
||
$this->expectExceptionMessage('shop_id is not a valid UUID'); | ||
$request->shopId(); | ||
} | ||
} |