Skip to content

Commit

Permalink
chore: rector fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
edodusi committed Jan 28, 2025
1 parent e60b104 commit f784e4f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 69 deletions.
15 changes: 4 additions & 11 deletions src/Data/StoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,13 @@ public function uuid(): string
}

/**
* Validates if the story data contains all required fields and valid values
*
* @return bool
*/
* Validates if the story data contains all required fields and valid values
*/
public function isValid(): bool
{
if (!$this->hasKey('name') || empty($this->getString('name'))) {
if (!$this->hasKey('name') || in_array($this->getString('name'), ['', '0'], true)) {
return false;
}

if (!$this->hasKey('slug') || empty($this->getString('slug'))) {
return false;
}

return true;
return $this->hasKey('slug') && !in_array($this->getString('slug'), ['', '0'], true);
}
}
52 changes: 11 additions & 41 deletions src/Endpoints/StoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,25 @@
*/
class StoryApi extends EndpointSpace
{
private const DEFAULT_ITEMS_PER_PAGE = 25;
private const DEFAULT_PAGE = 1;
private const RATE_LIMIT_STATUS_CODE = 429;
private const RETRY_DELAY = 1;
private const MAX_RETRIES = 3;
private const int DEFAULT_ITEMS_PER_PAGE = 25;

private LoggerInterface $logger;
private const int DEFAULT_PAGE = 1;

private const int RATE_LIMIT_STATUS_CODE = 429;

private const int RETRY_DELAY = 1;

private const int MAX_RETRIES = 3;

/**
* StoryApi constructor.
*
* @param HttpClientInterface $httpClient
* @param string|int $spaceId
* @param LoggerInterface $logger
*/
public function __construct(
HttpClientInterface $httpClient,
string|int $spaceId,
LoggerInterface $logger,
private readonly LoggerInterface $logger,
) {
parent::__construct($httpClient, $spaceId);
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -82,10 +79,6 @@ public function all(int $itemsPerPage = 5): \Generator

/**
* Retrieves a specific page of stories
*
* @param int $page
* @param int $perPage
* @return StoryblokResponseInterface
*/
public function page(
int $page = self::DEFAULT_PAGE,
Expand All @@ -111,8 +104,6 @@ public function page(
/**
* Retrieves a specific story by ID
*
* @param string $storyId
* @return StoryblokResponseInterface
* @throws StoryblokApiException
*/
public function get(string $storyId): StoryblokResponseInterface
Expand All @@ -129,8 +120,6 @@ public function get(string $storyId): StoryblokResponseInterface
/**
* Creates a new story
*
* @param StoryData $storyData
* @return StoryblokResponseInterface
* @throws InvalidStoryDataException
*/
public function create(StoryData $storyData): StoryblokResponseInterface
Expand All @@ -156,9 +145,6 @@ public function create(StoryData $storyData): StoryblokResponseInterface
/**
* Updates an existing story
*
* @param string $storyId
* @param StoryData $storyData
* @return StoryblokResponseInterface
* @throws InvalidStoryDataException
*/
public function update(string $storyId, StoryData $storyData): StoryblokResponseInterface
Expand All @@ -178,11 +164,6 @@ public function update(string $storyId, StoryData $storyData): StoryblokResponse

/**
* Handles successful API response
*
* @param StoryblokResponseInterface $response
* @param int|null $totalPages
* @param int $itemsPerPage
* @return int
*/
private function handleSuccessfulResponse(
StoryblokResponseInterface $response,
Expand All @@ -200,8 +181,6 @@ private function handleSuccessfulResponse(
/**
* Handles error responses from the API
*
* @param StoryblokResponseInterface $response
* @param int $retryCount
* @throws StoryblokApiException
*/
private function handleErrorResponse(StoryblokResponseInterface $response, int $retryCount): void
Expand Down Expand Up @@ -233,7 +212,6 @@ private function handleRateLimit(): void
/**
* Extracts stories from the API response
*
* @param StoryblokResponseInterface $response
* @return \Generator<int, StoryData>
*/
private function getStoriesFromResponse(StoryblokResponseInterface $response): \Generator
Expand All @@ -249,15 +227,14 @@ private function getStoriesFromResponse(StoryblokResponseInterface $response): \
/**
* Validates pagination parameters
*
* @param int $page
* @param int $perPage
* @throws \InvalidArgumentException
*/
private function validatePaginationParams(int $page, int $perPage): void
{
if ($page < 1) {
throw new \InvalidArgumentException('Page number must be greater than 0');
}

if ($perPage < 1) {
throw new \InvalidArgumentException('Items per page must be greater than 0');
}
Expand All @@ -266,20 +243,18 @@ private function validatePaginationParams(int $page, int $perPage): void
/**
* Validates story ID
*
* @param string $storyId
* @throws \InvalidArgumentException
*/
private function validateStoryId(string $storyId): void
{
if (empty($storyId)) {
if ($storyId === '' || $storyId === '0') {
throw new \InvalidArgumentException('Story ID cannot be empty');
}
}

/**
* Validates story data
*
* @param StoryData $storyData
* @throws InvalidStoryDataException
*/
private function validateStoryData(StoryData $storyData): void
Expand All @@ -291,8 +266,6 @@ private function validateStoryData(StoryData $storyData): void

/**
* Builds the base endpoint for stories
*
* @return string
*/
private function buildStoriesEndpoint(): string
{
Expand All @@ -301,9 +274,6 @@ private function buildStoriesEndpoint(): string

/**
* Builds the endpoint for a specific story
*
* @param string $storyId
* @return string
*/
private function buildStoryEndpoint(string $storyId): string
{
Expand Down
14 changes: 0 additions & 14 deletions src/MapiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class MapiClient

/**
* MapiClient constructor.
*
* @param string $personalAccessToken
* @param Region $region
* @param string|null $baseUri
*/
public function __construct(
string $personalAccessToken,
Expand All @@ -51,11 +47,6 @@ public function __construct(

/**
* Initialize the MapiClient
*
* @param string $personalAccessToken
* @param Region $region
* @param string|null $baseUri
* @return MapiClient
*/
public static function init(
string $personalAccessToken,
Expand Down Expand Up @@ -84,11 +75,6 @@ public function spaceApi(): SpaceApi
return new SpaceApi($this->httpClient);
}

/**
* @param string|int $spaceId
* @param LoggerInterface|null $logger
* @return StoryApi
*/
public function storyApi(string|int $spaceId, ?LoggerInterface $logger = null): StoryApi
{
return new StoryApi(
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/StoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
];

// Create a callback to verify the request
$callback = function ($method, $url, $options) use ($expectedStoryData): void {
$callback = function ($method, $url, array $options) use ($expectedStoryData): void {
expect($method)->toBe('POST');
expect($url)->toContain('/stories');

// Decode the request body and verify it matches expected structure
$requestBody = json_decode($options['body'], true);
$requestBody = json_decode((string) $options['body'], true);
expect($requestBody)->toBeArray();
expect($requestBody)->toHaveKey('story');
expect($requestBody['story'])->toEqual($expectedStoryData);
Expand Down Expand Up @@ -118,7 +118,7 @@ public function error(string|\Stringable $message, array $context = []): void
// Use the all() method which we know triggers logging
try {
iterator_to_array($storyApi->all());
} catch (\Exception $e) {
} catch (\Exception) {
// Expected exception
}

Expand Down

0 comments on commit f784e4f

Please sign in to comment.