Skip to content

Commit

Permalink
style: some minor readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongerig committed May 22, 2024
1 parent 751a3a7 commit c713444
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 39 deletions.
9 changes: 7 additions & 2 deletions src/PimcoreElementManagerBundle/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
* files that are distributed with this source code.
*
* @copyright 2024 instride AG (https://instride.ch)
* @license https://github.com/instride-ch/pimcore-element-manager/blob/main/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
* @license https://github.com/instride-ch/pimcore-element-manager/blob/main/gpl-3.0.txt GNU General Public License
* version 3 (GPLv3)
*/

namespace Instride\Bundle\PimcoreElementManagerBundle\Command;

use CoreShop\Component\Pimcore\BatchProcessing\BatchListing;
use Doctrine\ORM\NonUniqueResultException;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Listing;
use Instride\Bundle\PimcoreElementManagerBundle\DuplicateIndex\DuplicateFinderInterface;
use Instride\Bundle\PimcoreElementManagerBundle\DuplicateIndex\DuplicatesIndexWorkerInterface;
Expand Down Expand Up @@ -64,13 +66,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$batchList = new BatchListing($list, $perLoop);

$output->writeln(\sprintf('<info>Processing %s Objects of class "%s"</info>', $batchList->count(), $class));
$output->writeln(
\sprintf('<info>Processing %s Objects of class "%s"</info>', $batchList->count(), $class)
);
$progress = new ProgressBar($output, $batchList->count());
$progress->setFormat(
'%current%/%max% [%bar%] %percent:3s%% (%elapsed:6s%/%estimated:-6s%) %memory:6s%: %message%'
);
$progress->start();

/** @var Concrete $object */
foreach ($batchList as $object) {
$progress->setMessage(\sprintf('Index %s (%s)', $object->getFullPath(), $object->getId()));
$progress->advance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@
use Instride\Bundle\PimcoreElementManagerBundle\Metadata\DuplicatesIndex\MetadataRegistryInterface;
use Instride\Bundle\PimcoreElementManagerBundle\Model\PotentialDuplicateInterface;
use Instride\Bundle\PimcoreElementManagerBundle\Repository\PotentialDuplicateRepositoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Webmozart\Assert\Assert;

class DuplicatesIndexController extends ResourceController
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function listAction(Request $request): JsonResponse
{
return $this->viewHandler->handle($this->getMetadataRegistry()->all(), [
'group' => 'List',
]);
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getAction(Request $request): JsonResponse
{
$this->isGrantedOr403();
Expand All @@ -55,6 +65,11 @@ public function getAction(Request $request): JsonResponse
);
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \Exception
*/
public function getPotentialDuplicatesAction(Request $request): JsonResponse
{
$declined = $request->get('declined', false);
Expand Down Expand Up @@ -124,7 +139,11 @@ public function getPotentialDuplicatesAction(Request $request): JsonResponse
$listResult[] = $toResult;
}

return $this->viewHandler->handle(['total' => $count * 2, 'data' => $listResult, 'success' => true], ['group' => 'Detailed']);
return $this->viewHandler->handle([
'total' => $count * 2,
'data' => $listResult,
'success' => true
], ['group' => 'Detailed']);
}

public function declineDuplicationAction(Request $request): JsonResponse
Expand Down Expand Up @@ -161,6 +180,10 @@ public function unDeclineDuplicationAction(Request $request): JsonResponse
return $this->viewHandler->handle(['success' => true]);
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function findByClassNameOr404(string $className): MetadataInterface
{
if (!$this->getMetadataRegistry()->has($className)) {
Expand All @@ -170,8 +193,12 @@ protected function findByClassNameOr404(string $className): MetadataInterface
return $this->getMetadataRegistry()->get($className);
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getMetadataRegistry(): MetadataRegistryInterface
{
return $this->get(MetadataRegistryInterface::class);
return $this->container->get(MetadataRegistryInterface::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ class AddSaveHandlerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container): void
{
foreach ($container->findTaggedServiceIds('pimcore_element_manager.save_handler', true) as $id => $attributes) {
$services = $container->findTaggedServiceIds('pimcore_element_manager.save_handler', true);

foreach ($services as $id => $attributes) {
if (!isset($attributes[0]['className'])) {
throw new \InvalidArgumentException(\sprintf('Tagged Service `%s` needs to have `className` attribute.', $id));
throw new \InvalidArgumentException(
\sprintf('Tagged Service `%s` needs to have `className` attribute.', $id)
);
}

$className = $attributes[0]['className'];
$saveManagerName = \sprintf('save_manager.%s', \strtolower($className));

if (!$container->hasDefinition(\sprintf('save_manager.%s', \strtolower($className)))) {
if (!$container->hasDefinition($saveManagerName)) {
continue;
}

$saveManagerDefinition = $container->getDefinition(\sprintf('save_manager.%s', \strtolower($className)));
$saveManagerDefinition = $container->getDefinition($saveManagerName);
$saveManagerDefinition->addMethodCall('addSaveHandler', [new Reference($id)]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setDefinition(ObjectSaveManagers::class, $objectSaveManagers);

foreach ($config['classes'] as $className => $classConfig) {
$this->registerSaveManagerConfiguration($container, $className, $classConfig ?? [], $loader, $objectSaveManagers);
$this->registerSaveManagerConfiguration(
$container,
$className,
$classConfig ?? [],
$loader,
$objectSaveManagers
);
$this->registerDuplicateIndexConfiguration(
$container,
$className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

namespace Instride\Bundle\PimcoreElementManagerBundle\DuplicateChecker\Constraints\Normalizer;

use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Listing\Concrete as Listing;
use Pimcore\Model\Element\ElementInterface;

class CompareConditionMySqlNormalizer
{
public function addForStringFields(
DataObject\Listing\Concrete $list,
Listing $list,
string $field,
string $value,
array $duplicateCheckTrimmedFields = []
Expand All @@ -35,17 +35,17 @@ public function addForStringFields(
}
}

public function addForDateFields(DataObject\Listing\Concrete $list, $field, \DateTime $value): void
public function addForDateFields(Listing $list, $field, \DateTime $value): void
{
$list->addConditionParam($field . ' = ?', $value->getTimestamp());
}

public function addForSingleRelationFields(DataObject\Listing\Concrete $list, $field, ElementInterface $value): void
public function addForSingleRelationFields(Listing $list, $field, ElementInterface $value): void
{
$list->addConditionParam($field . '__id = ?', $value->getId());
}

public function addForMultiRelationFields(DataObject\Listing\Concrete $list, $field, $value): void
public function addForMultiRelationFields(Listing $list, $field, $value): void
{
$ids = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class ContainerDataTransformerFactory implements DataTransformerFactoryInterface

public function __construct(private readonly ContainerInterface $container) {}

/**
* @inheritDoc
*/
public function getInstance(string $identifier): DataTransformerInterface
{
if (!isset($this->dataTransformers[$identifier])) {
Expand Down
22 changes: 10 additions & 12 deletions src/PimcoreElementManagerBundle/DuplicateIndex/DuplicateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use CoreShop\Component\Resource\Factory\FactoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Instride\Bundle\PimcoreElementManagerBundle\DuplicateIndex\Similarity\SimilarityCheckerFactoryInterface;
use Instride\Bundle\PimcoreElementManagerBundle\Metadata\DuplicatesIndex\GroupMetadataInterface;
use Instride\Bundle\PimcoreElementManagerBundle\Metadata\DuplicatesIndex\MetadataInterface;
Expand All @@ -40,6 +41,9 @@ public function __construct(
private readonly FactoryInterface $potentialDuplicateFactory
) {}

/**
* @inheritDoc
*/
public function findPotentialDuplicate(MetadataInterface $metadata): void
{
$this->potentialDuplicateRepository->deleteAll();
Expand Down Expand Up @@ -95,10 +99,8 @@ protected function findFuzzyDuplicates(MetadataInterface $metadata): array
return \array_merge($soundex, $metaphone);
}

protected function findFuzzyDuplicatesByAlgorithm(
MetadataInterface $metadata,
string $algorithm
): array {
protected function findFuzzyDuplicatesByAlgorithm(MetadataInterface $metadata, string $algorithm): array
{
$duplicates = $this->duplicateRepository->findExactByAlgorithm($metadata->getClassName(), $algorithm);
$result = [];

Expand All @@ -119,10 +121,8 @@ protected function findFuzzyDuplicatesByAlgorithm(
return $result;
}

protected function checkForDuplicate(
MetadataInterface $metadata,
array $duplicateObjects
): array {
protected function checkForDuplicate(MetadataInterface $metadata, array $duplicateObjects): array
{
$grouped = [];

foreach ($duplicateObjects as $duplicateObject) {
Expand All @@ -144,10 +144,8 @@ protected function checkForDuplicate(
return \array_merge(...$result);
}

private function checkForDuplicatesInGroup(
GroupMetadataInterface $group,
array $duplicateObjects
): array {
private function checkForDuplicatesInGroup(GroupMetadataInterface $group, array $duplicateObjects): array
{
$result = [];

foreach ($duplicateObjects as $duplicateObject1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

namespace Instride\Bundle\PimcoreElementManagerBundle\DuplicateIndex;

use Doctrine\ORM\NonUniqueResultException;
use Instride\Bundle\PimcoreElementManagerBundle\Metadata\DuplicatesIndex\MetadataInterface;

interface DuplicateFinderInterface
{
/**
* @throws NonUniqueResultException
*/
public function findPotentialDuplicate(MetadataInterface $metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ protected function transformData($value, FieldMetadataInterface $field): mixed

protected function isRelevantForIndex(Concrete $concrete): bool
{
return $concrete->getPublished();
return $concrete->isPublished();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

namespace Instride\Bundle\PimcoreElementManagerBundle\DuplicateIndex\Similarity;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

interface SimilarityCheckerFactoryInterface
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getInstance(string $identifier): SimilarityCheckerInterface;
}
15 changes: 9 additions & 6 deletions src/PimcoreElementManagerBundle/PimcoreElementManagerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ protected function getModelNamespace(): ?string
/**
* @inheritDoc
*/
public function build(ContainerBuilder $builder): void
public function build(ContainerBuilder $container): void
{
parent::build($builder);
parent::build($container);

$builder->addCompilerPass(new AddConstraintValidatorsPass('duplication_checker.validator_factory', 'duplication_checker.constraint_validator'));
$builder->addCompilerPass(new AddDataTransformersPass());
$builder->addCompilerPass(new AddSimilarityCheckerPass());
$builder->addCompilerPass(new AddSaveHandlerPass());
$container->addCompilerPass(new AddConstraintValidatorsPass(
'duplication_checker.validator_factory',
'duplication_checker.constraint_validator'
));
$container->addCompilerPass(new AddDataTransformersPass());
$container->addCompilerPass(new AddSimilarityCheckerPass());
$container->addCompilerPass(new AddSaveHandlerPass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public function apply(Concrete $object, array $options): void
if ($options['initial_key_mapping']) {
$request = $this->requestStack->getMainRequest();

if (null !== $request && $this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_ADMIN) &&
$object->getKey() && $object->getId() === 0
if (
null !== $request &&
$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_ADMIN) &&
$object->getKey() &&
$object->getId() === 0
) {
$setter = \sprintf('set%s', \ucfirst($options['initial_key_mapping']));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UniqueKeySaveHandler implements PostObjectSaveHandlerInterface
/**
* @throws \Exception
*/
public function preSave(Concrete $object, array $options): void
public function preSave(Concrete $object): void
{
$object->setKey(Service::getUniqueKey($object));
}
Expand Down

0 comments on commit c713444

Please sign in to comment.