Skip to content

Commit

Permalink
Merge pull request #14 from homeappcorporate/task/PHP-1167
Browse files Browse the repository at this point in the history
[php-1167] Рефактор JsonApi Exceptions
  • Loading branch information
OlgaKrasheninnikova committed Feb 23, 2021
2 parents c56a1d4 + e59464a commit db957a5
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 45 deletions.
11 changes: 10 additions & 1 deletion HomeappData/Registry/Dto/RealtyImage/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ class Relationships implements RelationshipsInterface
*/
public RelationshipsBlockSingle $image;

public function __construct(ResourceIdentifierObject $image)
/**
* НЕ обязательное, для обратной совместмости пакета. Нужно будет потом сделать обязательным.
* @var RelationshipsBlockSingle|null
* @Serializer\Type("Homeapp\JsonApi\Relationships\RelationshipsBlockSingle")
* @Serializer\Groups({"API"})
*/
public ?RelationshipsBlockSingle $realty = null;

public function __construct(ResourceIdentifierObject $image, ?ResourceIdentifierObject $realty = null)
{
$this->image = new RelationshipsBlockSingle($image);
$this->realty = new RelationshipsBlockSingle($realty);
}
}
23 changes: 4 additions & 19 deletions JsonApi/Exception/JsonApiBadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,10 @@
use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiBadRequestException extends InvalidArgumentException
class JsonApiBadRequestException extends JsonApiBaseException
{
private string $parameter;

public static function create(string $message, string $parameter): self
{
$exception = new self();
$exception->message = $message;
$exception->parameter = $parameter;
$exception->code = Response::HTTP_BAD_REQUEST;

return $exception;
}

/**
* @return string
*/
public function getParameter(): string
protected static function getHttpCode(): int
{
return $this->parameter;
return Response::HTTP_BAD_REQUEST;
}
}
}
45 changes: 45 additions & 0 deletions JsonApi/Exception/JsonApiBaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);
namespace Homeapp\JsonApi\Exception;

use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

abstract class JsonApiBaseException extends InvalidArgumentException
{
protected ?string $parameter = null;
protected ?string $pointer = null;

public static function create(string $message, ?string $parameter = null): self
{
$exception = new static();
$exception->message = $message;
$exception->parameter = $parameter;
$exception->code = static::getHttpCode();

return $exception;
}

public static function createWithPointer(string $message, string $pointer): self
{
$exception = new static();
$exception->message = $message;
$exception->pointer = $pointer;
$exception->code = static::getHttpCode();

return $exception;
}

abstract protected static function getHttpCode(): int;

public function getParameter(): ?string
{
return $this->parameter;
}

public function getPointer(): ?string
{
return $this->pointer;
}
}
15 changes: 15 additions & 0 deletions JsonApi/Exception/JsonApiConflictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);
namespace Homeapp\JsonApi\Exception;

use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiConflictException extends JsonApiBaseException
{
protected static function getHttpCode(): int
{
return Response::HTTP_CONFLICT;
}
}
12 changes: 4 additions & 8 deletions JsonApi/Exception/JsonApiNotAcceptableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiNotAcceptableException extends InvalidArgumentException
class JsonApiNotAcceptableException extends JsonApiBaseException
{
public static function create(string $message): self
protected static function getHttpCode(): int
{
$exception = new self();
$exception->message = $message;
$exception->code = Response::HTTP_NOT_ACCEPTABLE;

return $exception;
return Response::HTTP_NOT_ACCEPTABLE;
}
}
}
14 changes: 6 additions & 8 deletions JsonApi/Exception/JsonApiNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php
<?php

declare(strict_types=1);
namespace Homeapp\JsonApi\Exception;

use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiNotFoundException extends InvalidArgumentException
class JsonApiNotFoundException extends JsonApiBaseException
{
public static function create(string $message): self
protected static function getHttpCode(): int
{
$exception = new self();
$exception->message = $message;
$exception->code = Response::HTTP_NOT_FOUND;

return $exception;
return Response::HTTP_NOT_FOUND;
}
}

16 changes: 16 additions & 0 deletions JsonApi/Exception/JsonApiServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);
namespace Homeapp\JsonApi\Exception;

use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiServerException extends JsonApiBaseException
{
protected static function getHttpCode(): int
{
return Response::HTTP_INTERNAL_SERVER_ERROR;
}
}

15 changes: 6 additions & 9 deletions JsonApi/Exception/JsonApiUnsupportedMediaTypeException.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<?php
<?php

declare(strict_types=1);
namespace Homeapp\JsonApi\Exception;

use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response;

class JsonApiUnsupportedMediaTypeException extends InvalidArgumentException
class JsonApiUnsupportedMediaTypeException extends JsonApiBaseException
{
public static function create(string $message): self
protected static function getHttpCode(): int
{
$exception = new self();
$exception->message = $message;
$exception->code = Response::HTTP_UNSUPPORTED_MEDIA_TYPE;

return $exception;
return Response::HTTP_UNSUPPORTED_MEDIA_TYPE;
}
}
}

0 comments on commit db957a5

Please sign in to comment.