Skip to content

Commit

Permalink
chore: apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrisbee committed Dec 12, 2023
1 parent 4b2ba01 commit 5e98d4b
Show file tree
Hide file tree
Showing 25 changed files with 164 additions and 215 deletions.
9 changes: 7 additions & 2 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class BadRequestException extends HttpException
use Storipress\WordPress\Objects\ErrorException;

class BadRequestException extends WordPressException
{
//
public function __construct(ErrorException $error)
{
parent::__construct($error, 400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Storipress\WordPress\Exceptions;

use Exception as BaseException;

abstract class Exception extends BaseException
class CannotCreateException extends WordPressException
{
//
}
10 changes: 10 additions & 0 deletions src/Exceptions/CannotUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

class CannotUpdateException extends WordPressException
{
//
}
10 changes: 10 additions & 0 deletions src/Exceptions/DuplicateTermSlugException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

class DuplicateTermSlugException extends WordPressException
{
//
}
9 changes: 7 additions & 2 deletions src/Exceptions/ForbiddenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class ForbiddenException extends HttpException
use Storipress\WordPress\Objects\ErrorException;

class ForbiddenException extends WordPressException
{
//
public function __construct(ErrorException $error)
{
parent::__construct($error, 403);
}
}
9 changes: 7 additions & 2 deletions src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class NotFoundException extends HttpException
use Storipress\WordPress\Objects\ErrorException;

class NotFoundException extends WordPressException
{
//
public function __construct(ErrorException $error)
{
parent::__construct($error, 404);
}
}
10 changes: 0 additions & 10 deletions src/Exceptions/Rest/CannotCreateException.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Exceptions/Rest/CannotUpdateException.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Exceptions/Rest/DuplicateTermSlugException.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Exceptions/Rest/HttpException.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Exceptions/Rest/TermExistsException.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Exceptions/Rest/UnknownException.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Storipress\WordPress\Exceptions;

class HttpUnknownError extends HttpException
class TermExistsException extends WordPressException
{
//
}
9 changes: 7 additions & 2 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class UnauthorizedException extends HttpException
use Storipress\WordPress\Objects\ErrorException;

class UnauthorizedException extends WordPressException
{
//
public function __construct(ErrorException $error)
{
parent::__construct($error, 401);
}
}
9 changes: 7 additions & 2 deletions src/Exceptions/UnexpectedValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Storipress\WordPress\Exceptions;

class UnexpectedValueException extends Exception
use Storipress\WordPress\Objects\ErrorException;

class UnexpectedValueException extends WordPressException
{
//
public function __construct(ErrorException $error)
{
parent::__construct($error, 500);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Storipress\WordPress\Exceptions;

abstract class HttpException extends Exception
class UnknownException extends WordPressException
{
//
}
28 changes: 28 additions & 0 deletions src/Exceptions/WordPressException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

use Exception as BaseException;
use Storipress\WordPress\Objects\ErrorException;

abstract class WordPressException extends BaseException
{
/**
* Duplicate term id
*/
public ?int $term_id;

public function __construct(public ErrorException $error, int $code)
{
$this->term_id = $error->data->term_id ?? null;

parent::__construct($error->message, $code);
}

public function getTermId(): ?int
{
return $this->term_id;
}
}
2 changes: 0 additions & 2 deletions src/Objects/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ class ErrorException extends WordPressObject

public string $message;

public string $raw_message;

public stdClass $data;
}
26 changes: 10 additions & 16 deletions src/Requests/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Storipress\WordPress\Requests;

use Storipress\WordPress\Exceptions\HttpException;
use Storipress\WordPress\Exceptions\UnexpectedValueException;
use Storipress\WordPress\Exceptions\WordPressException;
use Storipress\WordPress\Objects\Category as CategoryObject;

class Category extends Request
Expand All @@ -15,15 +14,14 @@ class Category extends Request
*
* @return array<int, CategoryObject>
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function list(): array
{
$data = $this->request('get', '/categories');

if (!is_array($data)) {
throw new UnexpectedValueException();
throw $this->unexpectedValueException('Unexpected response format.');
}

return array_map(
Expand All @@ -37,15 +35,14 @@ public function list(): array
*
* @param array<string, mixed> $arguments
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function create(array $arguments): CategoryObject
{
$data = $this->request('post', '/categories', $arguments);

if (is_array($data)) {
throw new UnexpectedValueException();
throw $this->unexpectedValueException('Unexpected response format.');
}

return CategoryObject::from($data);
Expand All @@ -54,8 +51,7 @@ public function create(array $arguments): CategoryObject
/**
* https://developer.wordpress.org/rest-api/reference/categories/#retrieve-a-category
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function retrieve(int $categoryId, string $context = 'view'): CategoryObject
{
Expand All @@ -66,7 +62,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj
]);

if (is_array($data)) {
throw new UnexpectedValueException();
throw $this->unexpectedValueException('Unexpected response format.');
}

return CategoryObject::from($data);
Expand All @@ -77,8 +73,7 @@ public function retrieve(int $categoryId, string $context = 'view'): CategoryObj
*
* @param array<string, mixed> $arguments
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function update(int $categoryId, array $arguments): CategoryObject
{
Expand All @@ -87,7 +82,7 @@ public function update(int $categoryId, array $arguments): CategoryObject
$data = $this->request('patch', $uri, $arguments);

if (is_array($data)) {
throw new UnexpectedValueException();
throw $this->unexpectedValueException('Unexpected response format.');
}

return CategoryObject::from($data);
Expand All @@ -96,8 +91,7 @@ public function update(int $categoryId, array $arguments): CategoryObject
/**
* https://developer.wordpress.org/rest-api/reference/categories/#delete-a-category
*
* @throws HttpException
* @throws UnexpectedValueException
* @throws WordPressException
*/
public function delete(int $categoryId): bool
{
Expand Down
Loading

0 comments on commit 5e98d4b

Please sign in to comment.