Skip to content

Commit

Permalink
Updated exception handling, added IntParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Jan 14, 2025
1 parent f7d98a0 commit 0e0b11a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
2 changes: 0 additions & 2 deletions src/CustomReport/Controller/Chart/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,11 @@ public function __construct(
#[FilterParameter('chart data')]
#[IntParameter(
name: 'reportOffset',
example: 1,
description: 'custom_reports_chart_report_offset_parameter',
required: false
)]
#[IntParameter(
name: 'reportLimit',
example: 10,
description: 'custom_reports_chart_report_limit_parameter',
required: false
)]
Expand Down
28 changes: 22 additions & 6 deletions src/CustomReport/Repository/CustomReportRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Pimcore\Bundle\CustomReportsBundle\Tool\Config\Listing;
use Pimcore\Bundle\CustomReportsBundle\Tool\Config\Listing\Dao;
use Pimcore\Bundle\StaticResolverBundle\Models\Tool\CustomReportResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Model\User;

Expand All @@ -31,16 +32,12 @@
public function __construct(
private SecurityServiceInterface $securityService,
private CustomReportResolverInterface $customReportResolver

) {
}

public function loadForUser(User $user): array
{
/** @var Dao $dao */
$dao = (new Listing())->getDao();

return $dao->loadForGivenUser(
return (new Listing())->getDao()->loadForGivenUser(
$user
);
}
Expand All @@ -57,6 +54,25 @@ public function loadForCurrentUser(): array

public function loadByName(string $name): ?Config
{
return $this->customReportResolver->getByName($name);
$report = null;
$exception = null;

try {
$report = $this->customReportResolver->getByName($name);
}
catch(\Exception $e){
$exception = $e;
}

if(!$report || $exception) {
throw new NotFoundException(
'Report',
$name,
'name',
$exception
);
}

return $report;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

namespace Pimcore\Bundle\StudioBackendBundle\CustomReport\Repository;

use Exception;
use Pimcore\Bundle\CustomReportsBundle\Tool\Config;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Model\User;

/**
Expand All @@ -30,7 +30,7 @@ public function loadForUser(User $user): array;
public function loadForCurrentUser(): array;

/**
* @throws Exception
* @throws NotFoundException
*/
public function loadByName(string $name): ?Config;
}
8 changes: 2 additions & 6 deletions src/CustomReport/Service/CustomReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Pimcore\Bundle\StudioBackendBundle\CustomReport\Repository\CustomReportRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Model\User;
use Exception;

/**
* @internal
Expand Down Expand Up @@ -55,12 +56,7 @@ public function getCustomReportConfigTree(?User $user = null): array

public function getCustomReportByName(string $reportName): Config
{
$report = $this->customReportRepository->loadByName($reportName);
if (!$report) {
throw new NotFoundException('Report', $reportName, 'name');
}

return $report;
return $this->customReportRepository->loadByName($reportName);
}

public function getChartData(string $reportName, ChartDataParameter $chartDataParameter): array
Expand Down
6 changes: 4 additions & 2 deletions src/Exception/Api/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
namespace Pimcore\Bundle\StudioBackendBundle\Exception\Api;

use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Throwable;
use function sprintf;

/**
* @internal
*/
final class NotFoundException extends AbstractApiException
{
public function __construct(string $type, int|string $id, string $parameter = 'ID')
public function __construct(string $type, int|string $id, string $parameter = 'ID', ?Throwable $previous = null)
{
parent::__construct(
HttpResponseCodes::NOT_FOUND->value,
sprintf('%s with %s: %s not found', ucfirst($type), $parameter, $id)
sprintf('%s with %s: %s not found', ucfirst($type), $parameter, $id),
previous: $previous,
);
}
}
2 changes: 1 addition & 1 deletion src/OpenApi/Attribute/Parameter/Query/IntParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ final class IntParameter extends QueryParameter
{
public function __construct(
string $name,
int $example,
string $description,
bool $required = true,
?int $example = null,
) {
parent::__construct(
name: $name,
Expand Down

0 comments on commit 0e0b11a

Please sign in to comment.