Skip to content

Commit

Permalink
[Task][PD-92]Introduce bundle specific exceptions (#5)
Browse files Browse the repository at this point in the history
* introduce bundle specific exceptions

* Apply php-cs-fixer changes

---------

Co-authored-by: lukmzig <lukmzig@users.noreply.github.com>
  • Loading branch information
lukmzig and lukmzig authored Jan 29, 2024
1 parent 05877c8 commit 6673dad
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 78 deletions.
31 changes: 19 additions & 12 deletions src/Command/Update/IndexUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
namespace Pimcore\Bundle\GenericDataIndexBundle\Command\Update;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\CommandAlreadyRunningException;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\IdNotFoundException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueService;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexUpdateService;
use Pimcore\Console\AbstractCommand;
use Pimcore\Model\DataObject\ClassDefinition;
use RuntimeException;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -83,13 +84,15 @@ protected function configure(): void
}

/**
* @throws \Doctrine\DBAL\Exception
* @throws RuntimeException
* @throws CommandAlreadyRunningException
*
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->lock()) {
throw new RuntimeException('The command is already running in another process.');
throw new CommandAlreadyRunningException(
'The command is already running in another process.'
);
}

$this->indexUpdateService->setReCreateIndex($input->getOption(self::OPTION_RECREATE_INDEX));
Expand All @@ -105,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
try {
$classDefinition = ClassDefinition::getById($classDefinitionId);
if (!$classDefinition) {
throw new RuntimeException(
throw new IdNotFoundException(
sprintf('ClassDefinition with id %s not found', $classDefinitionId)
);
}
Expand Down Expand Up @@ -144,14 +147,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($updateAll) {
$this->output->writeln(
'<info>Update all mappings and indices for objects/assets</info>',
OutputInterface::VERBOSITY_VERBOSE
);
try {
$this->output->writeln(
'<info>Update all mappings and indices for objects/assets</info>',
OutputInterface::VERBOSITY_VERBOSE
);

$this
->indexUpdateService
->updateAll();
$this
->indexUpdateService
->updateAll();
} catch (Exception $e) {
$this->output->writeln('<error>' . $e->getMessage() . '</error>');
}
}

$this->output->writeln(
Expand Down
23 changes: 23 additions & 0 deletions src/Exception/BulkOperationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class BulkOperationException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/CommandAlreadyRunningException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class CommandAlreadyRunningException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/EnqueueAssetsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class EnqueueAssetsException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
19 changes: 19 additions & 0 deletions src/Exception/GenericDataIndexBundleExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

// @internal
interface GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/IdNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class IdNotFoundException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/IndexDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class IndexDataException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/InvalidElementTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class InvalidElementTypeException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
23 changes: 23 additions & 0 deletions src/Exception/SwitchIndexAliasException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Exception;

use RuntimeException;

/**
* @internal
*/
final class SwitchIndexAliasException extends RuntimeException implements GenericDataIndexBundleExceptionInterface
{
}
27 changes: 17 additions & 10 deletions src/Service/SearchIndex/IndexQueue/EnqueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\EnqueueAssetsException;
use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\ElementTypeAdapter\ElementTypeAdapterService;
use Pimcore\Bundle\GenericDataIndexBundle\Service\TimeService;
Expand Down Expand Up @@ -89,19 +90,25 @@ public function enqueueByClassDefinition(ClassDefinition $classDefinition): Enqu
}

/**
* @throws Exception
* @throws EnqueueAssetsException
*/
public function enqueueAssets(): EnqueueService
{
$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT id, '%s', '%s', '%s', '%s', 0 FROM %s",
ElementType::ASSET->value,
IndexName::ASSET->value,
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
'assets'
)
);
try {
$this->indexQueueRepository->enqueueBySelectQuery(
sprintf("SELECT id, '%s', '%s', '%s', '%s', 0 FROM %s",
ElementType::ASSET->value,
IndexName::ASSET->value,
IndexQueueOperation::UPDATE->value,
$this->timeService->getCurrentMillisecondTimestamp(),
'assets'
)
);
} catch (Exception $e) {
throw new EnqueueAssetsException(
$e->getMessage()
);
}

return $this;
}
Expand Down
14 changes: 6 additions & 8 deletions src/Service/SearchIndex/IndexQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

use Exception;
use InvalidArgumentException;
use JsonException;
use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\IndexDataException;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueService;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\QueueMessagesDispatcher;
Expand All @@ -29,8 +30,6 @@
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Element\ElementInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use UnhandledMatchError;

class IndexQueueService

Check notice on line 34 in src/Service/SearchIndex/IndexQueueService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Efferent coupling between objects

\[EA\] High efferent coupling (23).

Check notice on line 34 in src/Service/SearchIndex/IndexQueueService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Efferent coupling between objects

\[EA\] High efferent coupling (23).
{
Expand Down Expand Up @@ -77,7 +76,6 @@ public function updateIndexQueue(ElementInterface $element, string $operation, b
/**
* @param IndexQueue[] $entries
*
* @throws ExceptionInterface
*/
public function handleIndexQueueEntries(array $entries): IndexQueueService
{
Expand All @@ -96,7 +94,7 @@ public function handleIndexQueueEntries(array $entries): IndexQueueService
$this->indexQueueRepository->deleteQueueEntries($entries);

} catch (Exception $e) {
$this->logger->info('handleIndexQueueEntry failed! Error: ' . $e->getMessage());
$this->logger->warning('handleIndexQueueEntry failed! Error: ' . $e->getMessage());
}

return $this;
Expand Down Expand Up @@ -129,8 +127,7 @@ protected function updateAssetDependencies(Asset $asset): IndexQueueService
*
* @return $this
*
* @throws JsonException
* @throws ExceptionInterface
* @throws IndexDataException
*/
protected function doHandleIndexData(ElementInterface $element, string $operation): IndexQueueService
{
Expand Down Expand Up @@ -168,13 +165,14 @@ protected function checkOperationValid(string $operation): void
}

/**
* @throws UnhandledMatchError
* @throws InvalidElementTypeException
*/
public function getElement(int $id, string $type): Asset|AbstractObject|null
{
return match($type) {
ElementType::ASSET->value => Asset::getById($id),
ElementType::DATA_OBJECT->value => AbstractObject::getById($id),
default => throw new InvalidElementTypeException('Invalid element type: ' . $type)
};
}

Expand Down
Loading

0 comments on commit 6673dad

Please sign in to comment.