diff --git a/src/Repository/AbstractApiRepository.php b/src/Repository/AbstractApiRepository.php new file mode 100644 index 0000000..8fed8b7 --- /dev/null +++ b/src/Repository/AbstractApiRepository.php @@ -0,0 +1,152 @@ +toApiError($e); + } + } + + /** + * @template T + * + * @param callable(): (T|null) $callback + * @return T|null|ApiError + */ + protected function catchNotFound(callable $callback): mixed + { + try { + return $callback(); + } catch (ProblemDetails | ApiException $e) { + $status = $e instanceof ProblemDetails ? $e->getStatus() : $e->getResponseStatusCode(); + + return $status === 404 ? null : $this->toApiError($e); + } catch (\Throwable $e) { + return $this->toApiError($e); + } + } + + protected function toApiError(\Throwable $e): ApiError + { + return match (true) { + $e instanceof ValidationErrorProblemDetails => $this->mapValidationError($e), + $e instanceof ProblemDetails => $this->mapStatusToError( + (int) ($e->getStatus() ?? 0), + $e->getDetail() ?? $e->getTitle() ?? $e->getMessage(), + $e + ), + $e instanceof ApiException => $this->mapStatusToError( + (int) ($e->getResponseStatusCode() ?? 0), + $e->getMessage(), + $e + ), + $e instanceof ConnectException || $e instanceof GuzzleException => new NetworkError( + message: $e->getMessage(), + previous: $e, + ), + default => new UnexpectedApiError( + message: $e->getMessage(), + statusCode: (int) $e->getCode(), + previous: $e, + ), + }; + } + + private function mapValidationError(ValidationErrorProblemDetails $e): ValidationError + { + $violations = []; + foreach ($e->getInvalidParams() ?? [] as $param) { + $name = $param->getName(); + if ($name !== null) { + $violations[$name] = $param->getReason() ?? ''; + } + } + + return new ValidationError( + message: $e->getDetail() ?? $e->getMessage(), + violations: $violations, + previous: $e, + ); + } + + private function mapStatusToError(int $status, string $message, \Throwable $e): ApiError + { + return match (true) { + $status === 401 || $status === 403 => new AuthenticationError($message, $status, $e), + $status === 429 => new RateLimitError($message, null, $e), + $status >= 500 && $status <= 599 => new ServerError($message, $status, $e), + default => new UnexpectedApiError($message, $status, $e), + }; + } + + protected function toDateTime(?\DateTimeInterface $date): ?\DateTime + { + if ($date === null) { + return null; + } + + if ($date instanceof \DateTime) { + return $date; + } + + return new \DateTime($date->format(\DateTimeInterface::RFC3339)); + } + + protected function toPaginationDto(?PaginationMeta $meta, Pagination $requestedPagination): PaginationDto + { + if ($meta !== null) { + return new PaginationDto( + page: $meta->getPage() ?? $requestedPagination->pageNumber(), + pageSize: $meta->getPageSize() ?? $requestedPagination->pageSize(), + totalItems: $meta->getTotalItems() ?? 0, + totalPages: $meta->getTotalPages() ?? 0, + ); + } + + return new PaginationDto( + page: $requestedPagination->pageNumber(), + pageSize: $requestedPagination->pageSize(), + totalItems: 0, + totalPages: 0, + ); + } +} diff --git a/src/Repository/BrandApiRepository.php b/src/Repository/BrandApiRepository.php new file mode 100644 index 0000000..388fdf5 --- /dev/null +++ b/src/Repository/BrandApiRepository.php @@ -0,0 +1,70 @@ +|ApiError + */ + public function selectAll( + Pagination $pagination + ): PageOf | ApiError { + $config = new BrandsRequestBuilderGetRequestConfiguration(); + $params = new BrandsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\BrandListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->brands()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + public function find(BrandCode $code): Brand | null | ApiError + { + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\BrandItemResponse|null|ApiError $response */ + $response = $this->catchNotFound( + fn () => $this->apiClient->brands()->byBrandCode($code->value)->get()->wait() + ); + + if ($response instanceof ApiError || $response === null) { + return $response; + } + + $brandModel = $response->getBrand(); + if ($brandModel === null) { + return null; + } + + return $this->hydrator->toDto($brandModel); + } +} diff --git a/src/Repository/BrandRepository.php b/src/Repository/BrandRepository.php new file mode 100644 index 0000000..25bd48a --- /dev/null +++ b/src/Repository/BrandRepository.php @@ -0,0 +1,26 @@ +|ApiError + */ + public function selectAll( + Pagination $pagination + ): PageOf | ApiError; + + public function find(BrandCode $code): Brand | null | ApiError; +} diff --git a/src/Repository/EmployeeApiRepository.php b/src/Repository/EmployeeApiRepository.php new file mode 100644 index 0000000..dbe79ba --- /dev/null +++ b/src/Repository/EmployeeApiRepository.php @@ -0,0 +1,185 @@ +|ApiError + */ + public function selectAll( + Pagination $pagination, + ?EmployeeSorter $sorter = null + ): PageOf | ApiError { + $config = new EmployeesRequestBuilderGetRequestConfiguration(); + $params = new EmployeesRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + + if ($sorter !== null) { + $params->sortBy = $this->mapSortBy($sorter->sortBy); + $params->sortDirection = $this->mapSortDirection($sorter->direction); + } + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->employees()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @return PageOf|ApiError + */ + public function selectAllSince( + Pagination $pagination, + \DateTimeInterface $since, + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError { + $config = new EmployeesRequestBuilderGetRequestConfiguration(); + $params = new EmployeesRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->since = $this->toDateTime($since); + $params->sortBy = new GetSortByQueryParameterType(GetSortByQueryParameterType::UPDATED_AT); + $params->sortDirection = $this->mapSortDirection($sortDirection); + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->employees()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param SiteId[] $sites + * @return PageOf|ApiError + */ + public function selectAllHavingSitesSince( + Pagination $pagination, + \DateTimeInterface $since, + array $sites, + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError { + $config = new EmployeesRequestBuilderGetRequestConfiguration(); + $params = new EmployeesRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->since = $this->toDateTime($since); + $params->siteIds = array_map(fn (SiteId $site): string => $site->value, $sites); + $params->sortBy = new GetSortByQueryParameterType(GetSortByQueryParameterType::UPDATED_AT); + $params->sortDirection = $this->mapSortDirection($sortDirection); + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->employees()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param SiteId[] $sites + * @return PageOf|ApiError + */ + public function selectAllHavingSites( + Pagination $pagination, + array $sites, + ?EmployeeSorter $sorter = null + ): PageOf | ApiError { + $config = new EmployeesRequestBuilderGetRequestConfiguration(); + $params = new EmployeesRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->siteIds = array_map(fn (SiteId $site): string => $site->value, $sites); + + if ($sorter !== null) { + $params->sortBy = $this->mapSortBy($sorter->sortBy); + $params->sortDirection = $this->mapSortDirection($sorter->direction); + } + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->employees()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + public function find(ArcadiaEmployeeId $id): Employee | null | ApiError + { + return $this->catchNotFound(function () use ($id): ?Employee { + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeItemResponse|null $response */ + $response = $this->apiClient->employees()->byEmployeeId($id->value)->get()->wait(); + $employee = $response?->getEmployee(); + + return $employee !== null ? $this->hydrator->toDto($employee) : null; + }); + } + + private function mapSortBy(EmployeeSortColumn $column): GetSortByQueryParameterType + { + $val = match ($column) { + EmployeeSortColumn::arcadiaId => GetSortByQueryParameterType::ARCADIA_ID, + EmployeeSortColumn::startDate => GetSortByQueryParameterType::START_DATE, + EmployeeSortColumn::endDate => GetSortByQueryParameterType::END_DATE, + EmployeeSortColumn::plannedEndDate => GetSortByQueryParameterType::PLANNED_END_DATE, + EmployeeSortColumn::createdAt => GetSortByQueryParameterType::CREATED_AT, + EmployeeSortColumn::updatedAt => GetSortByQueryParameterType::UPDATED_AT, + }; + + return new GetSortByQueryParameterType($val); + } + + private function mapSortDirection(\SortDirection $direction): GetSortDirectionQueryParameterType + { + $val = $direction === \SortDirection::Ascending + ? GetSortDirectionQueryParameterType::ASCENDING + : GetSortDirectionQueryParameterType::DESCENDING; + + return new GetSortDirectionQueryParameterType($val); + } +} diff --git a/src/Repository/EmployeeRepository.php b/src/Repository/EmployeeRepository.php index 112ab8e..6d14f74 100644 --- a/src/Repository/EmployeeRepository.php +++ b/src/Repository/EmployeeRepository.php @@ -4,52 +4,55 @@ declare(strict_types=1); namespace Uwil\ArcadiaApiPhpClient\Repository; +use Uwil\ArcadiaApiPhpClient\Error\ApiError; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf; -use Uwil\ArcadiaApiPhpClient\Lib\ApiQueryBuilder\Direction; +use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\EmployeeSorter; use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId; -use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EmployeeSorter; use Uwil\ArcadiaApiPhpClient\Lib\Pagination; +/** + * @internal + */ interface EmployeeRepository extends Repository { /** - * @return PageOf + * @return PageOf|ApiError */ public function selectAll( Pagination $pagination, ?EmployeeSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; /** - * @return PageOf + * @return PageOf|ApiError */ public function selectAllSince( Pagination $pagination, \DateTimeInterface $since, - Direction $sortDirection = Direction::DESC - ): PageOf; + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId[] $sites - * @return PageOf + * @return PageOf|ApiError */ public function selectAllHavingSitesSince( Pagination $pagination, \DateTimeInterface $since, array $sites, - Direction $sortDirection = Direction::DESC - ): PageOf; + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId[] $sites - * @return PageOf + * @return PageOf|ApiError */ public function selectAllHavingSites( Pagination $pagination, array $sites, ?EmployeeSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; - public function find(ArcadiaEmployeeId $id): ?Employee; + public function find(ArcadiaEmployeeId $id): Employee | null | ApiError; } diff --git a/src/Repository/IncoApiRepository.php b/src/Repository/IncoApiRepository.php new file mode 100644 index 0000000..ea20570 --- /dev/null +++ b/src/Repository/IncoApiRepository.php @@ -0,0 +1,215 @@ +|ApiError + */ + public function selectAll( + Pagination $pagination, + ?IncoSorter $sorter = null + ): PageOf | ApiError { + $config = new IncoSheetsRequestBuilderGetRequestConfiguration(); + $params = new IncoSheetsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->short = false; + + if ($sorter !== null) { + $params->sortBy = $this->mapSortBy($sorter->sortBy); + $params->sortDirection = $this->mapSortDirection($sorter->direction); + } + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @return PageOf|ApiError + */ + public function selectAllSince( + Pagination $pagination, + \DateTimeInterface $since, + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError { + $config = new IncoSheetsRequestBuilderGetRequestConfiguration(); + $params = new IncoSheetsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->since = $this->toDateTime($since); + $params->short = false; + $params->sortBy = new GetSortByQueryParameterType(GetSortByQueryParameterType::UPDATED_AT); + $params->sortDirection = $this->mapSortDirection($sortDirection); + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param BrandCode[] $brands + * @return PageOf|ApiError + */ + public function selectAllHavingBrandsSince( + Pagination $pagination, + \DateTimeInterface $since, + array $brands, + \SortDirection $sortDirection = \SortDirection::Descending, + ): PageOf | ApiError { + $config = new IncoSheetsRequestBuilderGetRequestConfiguration(); + $params = new IncoSheetsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->since = $this->toDateTime($since); + $params->brands = array_map(fn (BrandCode $brand): string => (string) $brand->value, $brands); + $params->short = false; + $params->sortBy = new GetSortByQueryParameterType(GetSortByQueryParameterType::UPDATED_AT); + $params->sortDirection = $this->mapSortDirection($sortDirection); + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param BrandCode[] $brands + * @return PageOf|ApiError + */ + public function selectAllHavingBrands( + Pagination $pagination, + array $brands, + ?IncoSorter $sorter = null + ): PageOf | ApiError { + $config = new IncoSheetsRequestBuilderGetRequestConfiguration(); + $params = new IncoSheetsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->brands = array_map(fn (BrandCode $brand): string => (string) $brand->value, $brands); + $params->short = false; + + if ($sorter !== null) { + $params->sortBy = $this->mapSortBy($sorter->sortBy); + $params->sortDirection = $this->mapSortDirection($sorter->direction); + } + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param BrandCode[] $inBrands + * @return PageOf|ApiError + */ + public function findWhere( + string $incoIdLike, + array $inBrands, + Pagination $pagination, + ?IncoSorter $sorter = null + ): PageOf | ApiError { + $config = new SearchRequestBuilderGetRequestConfiguration(); + $params = new SearchRequestBuilderGetQueryParameters(); + $params->incoIdLike = $incoIdLike; + $params->brands = $inBrands !== [] ? array_map(fn (BrandCode $brand): string => (string) $brand->value, $inBrands) : null; + $params->short = false; + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->search()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + public function find(IncoId $id): Inco | null | ApiError + { + return $this->catchNotFound(function () use ($id): ?Inco { + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoItemResponse|null $response */ + $response = $this->apiClient->incoSheets()->byIncoId($id->value)->get()->wait(); + $sheet = $response?->getIncoSheet(); + + return $sheet !== null ? $this->hydrator->toDto($sheet) : null; + }); + } + + private function mapSortBy(IncoSortColumn $column): GetSortByQueryParameterType + { + $val = match ($column) { + IncoSortColumn::id => GetSortByQueryParameterType::INCO_SHEET_ID, + IncoSortColumn::createdAt => GetSortByQueryParameterType::CREATED_AT, + IncoSortColumn::updatedAt => GetSortByQueryParameterType::UPDATED_AT, + }; + + return new GetSortByQueryParameterType($val); + } + + private function mapSortDirection(\SortDirection $direction): GetSortDirectionQueryParameterType + { + $val = $direction === \SortDirection::Ascending + ? GetSortDirectionQueryParameterType::ASCENDING + : GetSortDirectionQueryParameterType::DESCENDING; + + return new GetSortDirectionQueryParameterType($val); + } +} diff --git a/src/Repository/IncoCompoApiRepository.php b/src/Repository/IncoCompoApiRepository.php new file mode 100644 index 0000000..3ebc674 --- /dev/null +++ b/src/Repository/IncoCompoApiRepository.php @@ -0,0 +1,41 @@ +findFor(new IncoId($id->value)); + } + + public function findFor(IncoId $id): IncoCompo | null | ApiError + { + return $this->catchNotFound(function () use ($id): ?IncoCompo { + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoItemResponse|null $response */ + $response = $this->apiClient->incoSheets()->byIncoId($id->value)->composition()->get()->wait(); + $composition = $response?->getComposition(); + + return $composition !== null ? $this->hydrator->toDto($composition) : null; + }); + } +} diff --git a/src/Repository/IncoCompoRepository.php b/src/Repository/IncoCompoRepository.php index 8378621..6c426ec 100644 --- a/src/Repository/IncoCompoRepository.php +++ b/src/Repository/IncoCompoRepository.php @@ -4,13 +4,17 @@ declare(strict_types=1); namespace Uwil\ArcadiaApiPhpClient\Repository; +use Uwil\ArcadiaApiPhpClient\Error\ApiError; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo; use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId; use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId; +/** + * @internal + */ interface IncoCompoRepository extends Repository { - public function find(IncoCompoId $id): ?IncoCompo; + public function find(IncoCompoId $id): IncoCompo | null | ApiError; - public function findFor(IncoId $id): ?IncoCompo; + public function findFor(IncoId $id): IncoCompo | null | ApiError; } diff --git a/src/Repository/IncoRepository.php b/src/Repository/IncoRepository.php index 04c96c3..ea42e66 100644 --- a/src/Repository/IncoRepository.php +++ b/src/Repository/IncoRepository.php @@ -4,63 +4,66 @@ declare(strict_types=1); namespace Uwil\ArcadiaApiPhpClient\Repository; +use Uwil\ArcadiaApiPhpClient\Error\ApiError; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf; -use Uwil\ArcadiaApiPhpClient\Lib\ApiQueryBuilder\Direction; +use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSorter; use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId; -use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoSorter; use Uwil\ArcadiaApiPhpClient\Lib\Pagination; +/** + * @internal + */ interface IncoRepository extends Repository { /** - * @return PageOf + * @return PageOf|ApiError */ public function selectAll( Pagination $pagination, ?IncoSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; /** - * @return PageOf + * @return PageOf|ApiError */ public function selectAllSince( Pagination $pagination, \DateTimeInterface $since, - Direction $sortDirection = Direction::DESC - ): PageOf; + \SortDirection $sortDirection = \SortDirection::Descending + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode[] $brands - * @return PageOf + * @return PageOf|ApiError */ public function selectAllHavingBrandsSince( Pagination $pagination, \DateTimeInterface $since, array $brands, - Direction $sortDirection = Direction::DESC, - ): PageOf; + \SortDirection $sortDirection = \SortDirection::Descending, + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode[] $brands - * @return PageOf + * @return PageOf|ApiError */ public function selectAllHavingBrands( Pagination $pagination, array $brands, ?IncoSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode[] $inBrands - * @return PageOf + * @return PageOf|ApiError */ public function findWhere( string $incoIdLike, array $inBrands, Pagination $pagination, ?IncoSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; - public function find(IncoId $id): ?Inco; + public function find(IncoId $id): Inco | null | ApiError; } diff --git a/src/Repository/IncoShortApiRepository.php b/src/Repository/IncoShortApiRepository.php new file mode 100644 index 0000000..4d18425 --- /dev/null +++ b/src/Repository/IncoShortApiRepository.php @@ -0,0 +1,113 @@ +|ApiError + */ + public function selectAllHavingBrands( + Pagination $pagination, + array $brands, + ?IncoSorter $sorter = null + ): PageOf | ApiError { + $config = new IncoSheetsRequestBuilderGetRequestConfiguration(); + $params = new IncoSheetsRequestBuilderGetQueryParameters(); + $params->page = $pagination->pageNumber(); + $params->pageSize = $pagination->pageSize(); + $params->brands = array_map(fn (BrandCode $brand): string => (string) $brand->value, $brands); + $params->short = true; + + if ($sorter !== null) { + $params->sortBy = $this->mapSortBy($sorter->sortBy); + $params->sortDirection = $this->mapSortDirection($sorter->direction); + } + + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + /** + * @param BrandCode[] $inBrands + * @return PageOf<\Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort>|ApiError + */ + public function findWhere( + string $incoIdLike, + array $inBrands, + Pagination $pagination, + ?IncoSorter $sorter = null + ): PageOf | ApiError { + $config = new SearchRequestBuilderGetRequestConfiguration(); + $params = new SearchRequestBuilderGetQueryParameters(); + $params->incoIdLike = $incoIdLike; + $params->brands = $inBrands !== [] ? array_map(fn (BrandCode $brand): string => (string) $brand->value, $inBrands) : null; + $params->short = true; + $config->queryParameters = $params; + + /** @var \Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse|ApiError $response */ + $response = $this->executeApiCall(fn () => $this->apiClient->incoSheets()->search()->get($config)->wait()); + + if ($response instanceof ApiError) { + return $response; + } + + return $this->hydrator->toPage($response, $pagination); + } + + private function mapSortBy(IncoSortColumn $column): GetSortByQueryParameterType + { + $val = match ($column) { + IncoSortColumn::id => GetSortByQueryParameterType::INCO_SHEET_ID, + IncoSortColumn::createdAt => GetSortByQueryParameterType::CREATED_AT, + IncoSortColumn::updatedAt => GetSortByQueryParameterType::UPDATED_AT, + }; + + return new GetSortByQueryParameterType($val); + } + + private function mapSortDirection(\SortDirection $direction): GetSortDirectionQueryParameterType + { + $val = $direction === \SortDirection::Ascending + ? GetSortDirectionQueryParameterType::ASCENDING + : GetSortDirectionQueryParameterType::DESCENDING; + + return new GetSortDirectionQueryParameterType($val); + } +} diff --git a/src/Repository/IncoShortRepository.php b/src/Repository/IncoShortRepository.php index 2d1e52a..49942e4 100644 --- a/src/Repository/IncoShortRepository.php +++ b/src/Repository/IncoShortRepository.php @@ -4,30 +4,34 @@ declare(strict_types=1); namespace Uwil\ArcadiaApiPhpClient\Repository; +use Uwil\ArcadiaApiPhpClient\Error\ApiError; use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf; -use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoSorter; +use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSorter; use Uwil\ArcadiaApiPhpClient\Lib\Pagination; +/** + * @internal + */ interface IncoShortRepository extends Repository { /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode[] $inBrands - * @return PageOf<\Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort> + * @return PageOf<\Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort>|ApiError */ public function findWhere( string $incoIdLike, array $inBrands, Pagination $pagination, ?IncoSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; /** * @param \Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode[] $brands - * @return PageOf<\Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort> + * @return PageOf<\Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort>|ApiError */ public function selectAllHavingBrands( Pagination $pagination, array $brands, ?IncoSorter $sorter = null - ): PageOf; + ): PageOf | ApiError; } diff --git a/src/Repository/Repository.php b/src/Repository/Repository.php index e6b511a..2afa202 100644 --- a/src/Repository/Repository.php +++ b/src/Repository/Repository.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Uwil\ArcadiaApiPhpClient\Repository; +/** + * @internal + */ interface Repository { }