Skip to content

Commit

Permalink
✨ Migrated some support/exception classes from marketplace-skeleton
Browse files Browse the repository at this point in the history
🔥 Deprecated JetBrains attributes in favor of psalm
  • Loading branch information
yoan-myparcel committed Apr 29, 2024
1 parent 0666188 commit dddd048
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 140 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# integration-commons
Library with common classes used for constructing MyParcel.com eCommerce Integrations
# marketplace-commons
Library with common classes used for constructing MyParcel.com marketplace integrations

## PHP 8
The minimum PHP version is `8.2`. To update dependencies on a system without PHP 8 use:
Expand Down
242 changes: 124 additions & 118 deletions composer.lock

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/Exceptions/AbstractRequestException.php
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);
}
}
19 changes: 19 additions & 0 deletions src/Exceptions/RequestInputException.php
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);
}
}
19 changes: 19 additions & 0 deletions src/Exceptions/RequestUnauthorizedException.php
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);
}
}
46 changes: 46 additions & 0 deletions src/Http/Requests/FormRequest.php
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);
}
}
10 changes: 7 additions & 3 deletions src/Order/Items/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MyParcelCom\Integration\Order\Items;

use JetBrains\PhpStorm\ArrayShape;

class Feature
{
public function __construct(
Expand All @@ -15,7 +13,13 @@ public function __construct(
) {
}

#[ArrayShape(['key' => 'string', 'value' => 'string|int|float|bool', 'annotation' => 'string|null'])]
/**
* @return array{
* key: string,
* value: string|int|float|bool,
* annotation: string|null
* }
*/
public function toArray(): array
{
return array_filter([
Expand Down
18 changes: 9 additions & 9 deletions src/PhysicalProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MyParcelCom\Integration;

use JetBrains\PhpStorm\ArrayShape;

class PhysicalProperties
{
public function __construct(
Expand All @@ -17,13 +15,15 @@ public function __construct(
) {
}

#[ArrayShape([
'weight' => 'int',
'height' => 'int|null',
'width' => 'int|null',
'length' => 'int|null',
'volume' => 'float|null',
])]
/**
* @return array{
* weight: int,
* height: int|null,
* width: int|null,
* length: int|null,
* volume: float|null
* }
*/
public function toArray(): array
{
return array_filter([
Expand Down
6 changes: 3 additions & 3 deletions src/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MyParcelCom\Integration;

use JetBrains\PhpStorm\ArrayShape;

class Price
{
public function __construct(
Expand All @@ -14,7 +12,9 @@ public function __construct(
) {
}

#[ArrayShape(['amount' => 'int', 'currency' => 'string'])]
/**
* @return array{amount: int, currency: string}
*/
public function toArray(): array
{
return [
Expand Down
5 changes: 3 additions & 2 deletions src/ShopId.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace MyParcelCom\Integration;

use JetBrains\PhpStorm\Immutable;
use Ramsey\Uuid\UuidInterface;

#[Immutable]
/**
* @psalm-immutable
*/
class ShopId
{
public function __construct(
Expand Down
26 changes: 26 additions & 0 deletions src/Support/ShopIdCaster.php
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();
}
}
6 changes: 3 additions & 3 deletions src/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MyParcelCom\Integration;

use JetBrains\PhpStorm\ArrayShape;

class Weight
{
public function __construct(
Expand All @@ -14,7 +12,9 @@ public function __construct(
) {
}

#[ArrayShape(['amount' => 'int', 'unit' => 'string'])]
/**
* @return array{amount: int, unit: string}
*/
public function toArray(): array
{
return [
Expand Down
36 changes: 36 additions & 0 deletions tests/Http/Requests/FormRequestTest.php
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();
}
}

0 comments on commit dddd048

Please sign in to comment.