-
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 3849689
Showing
18 changed files
with
543 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Http\Requests; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class OrdersCountRequest extends FormRequest | ||
{ | ||
private const DATE_FORMAT = 'Y-m-d'; | ||
|
||
public function dateFrom(): Carbon | ||
{ | ||
$from = $this->query('date_from'); | ||
if (!$from) { | ||
return Carbon::now()->subDay(); | ||
} | ||
|
||
$this->validateDateFormat($from); | ||
|
||
return $this->toCarbon($from); | ||
} | ||
|
||
public function dateTo(): Carbon | ||
{ | ||
$to = $this->query('date_to'); | ||
if (!$to) { | ||
return Carbon::now(); | ||
} | ||
|
||
$this->validateDateFormat($to); | ||
|
||
return $this->toCarbon($to); | ||
} | ||
|
||
private function validateDateFormat(string $date): void | ||
{ | ||
Validator::validate( | ||
['date' => $date], | ||
['date' => 'date_format:' . self::DATE_FORMAT], | ||
); | ||
} | ||
|
||
private function toCarbon(string $date): Carbon | ||
{ | ||
return Carbon::createFromFormat(self::DATE_FORMAT, $date)->startOfDay(); | ||
} | ||
} |
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 MyParcelCom\Integration\Http\Requests; | ||
|
||
use Carbon\Carbon; | ||
|
||
class ShipmentRequest extends FormRequest | ||
{ | ||
public function startDate(): Carbon | ||
{ | ||
return Carbon::createFromFormat('Y-m-d H:i:s', $this->input('filter.start_date') . ' 00:00:00'); | ||
} | ||
|
||
public function endDate(): Carbon | ||
{ | ||
return Carbon::createFromFormat('Y-m-d H:i:s', $this->input('filter.end_date') . ' 23:59:59'); | ||
} | ||
|
||
/** | ||
* @return int|null | ||
*/ | ||
public function pageNumber(): ?int | ||
{ | ||
return $this->input('page.number') ? (int) $this->input('page.number') : null; | ||
} | ||
|
||
/** | ||
* @return int|null | ||
*/ | ||
public function pageSize(): ?int | ||
{ | ||
return $this->input('page.size') ? (int) $this->input('page.size') : null; | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Arr; | ||
use MyParcelCom\Integration\Exceptions\RequestInputException; | ||
use MyParcelCom\Integration\ShopId; | ||
use Ramsey\Uuid\Exception\InvalidUuidStringException; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class ShipmentStatusCallbackRequest extends FormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function getShipmentData(): array | ||
{ | ||
$included = $this->get('included'); | ||
|
||
return collect($included)->first(fn ($include) => $include['type'] === 'shipments'); | ||
} | ||
|
||
public function getStatusData(): array | ||
{ | ||
$included = $this->get('included'); | ||
|
||
return collect($included)->first(fn ($include) => $include['type'] === 'statuses'); | ||
} | ||
|
||
public function shopId(): ShopId | ||
{ | ||
$shopId = Arr::get($this->getShipmentData(), 'relationships.shop.data.id'); | ||
|
||
if (!$shopId) { | ||
throw new RequestInputException('Bad request', 'No shop_id provided in the request body'); | ||
} | ||
|
||
try { | ||
$shopUuid = Uuid::fromString($shopId); | ||
} catch (InvalidUuidStringException $exception) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class ShopSetupRequest extends FormRequest | ||
{ | ||
public function authorize(): true | ||
{ | ||
return true; | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
return [ | ||
'data.settings' => 'array', | ||
'data.redirect_url' => 'url', | ||
]; | ||
} | ||
|
||
public function redirectUrl(): ?string | ||
{ | ||
return $this->input('data.redirect_url'); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelCom\Integration\Http\Responses; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Response; | ||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; | ||
|
||
class ShopSetupResponse implements Responsable | ||
{ | ||
public function __construct( | ||
private readonly ?string $authorizationUrl = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function toResponse($request): SymfonyResponse | ||
{ | ||
if (!$this->authorizationUrl) { | ||
return new Response('', SymfonyResponse::HTTP_NO_CONTENT); | ||
} | ||
|
||
return new JsonResponse([ | ||
'data' => [ | ||
'authorization_url' => $this->authorizationUrl, | ||
] | ||
]); | ||
} | ||
} |
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
Oops, something went wrong.