diff --git a/spec/Controller/Action/Admin/ProcessFeedActionSpec.php b/spec/Controller/Action/Admin/ProcessFeedActionSpec.php index 8f5e8e2b..a5bb6c20 100644 --- a/spec/Controller/Action/Admin/ProcessFeedActionSpec.php +++ b/spec/Controller/Action/Admin/ProcessFeedActionSpec.php @@ -21,7 +21,7 @@ public function let( MessageBusInterface $commandBus, UrlGeneratorInterface $urlGenerator, FlashBagInterface $flashBag, - TranslatorInterface $translator + TranslatorInterface $translator, ): void { $this->beConstructedWith($commandBus, $urlGenerator, $flashBag, $translator); } diff --git a/src/Controller/Action/Admin/ProcessFeedAction.php b/src/Controller/Action/Admin/ProcessFeedAction.php index 73fedbee..45cc4608 100644 --- a/src/Controller/Action/Admin/ProcessFeedAction.php +++ b/src/Controller/Action/Admin/ProcessFeedAction.php @@ -5,11 +5,15 @@ namespace Setono\SyliusFeedPlugin\Controller\Action\Admin; use Setono\SyliusFeedPlugin\Message\Command\ProcessFeed; +use Setono\SyliusFeedPlugin\Model\Feed; +use Setono\SyliusFeedPlugin\Repository\FeedRepositoryInterface; +use Setono\SyliusFeedPlugin\Workflow\FeedGraph; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Contracts\Translation\TranslatorInterface; +use Webmozart\Assert\Assert; final class ProcessFeedAction { @@ -21,20 +25,30 @@ final class ProcessFeedAction private TranslatorInterface $translator; + private FeedRepositoryInterface $feedRepository; + public function __construct( MessageBusInterface $commandBus, UrlGeneratorInterface $urlGenerator, FlashBagInterface $flashBag, - TranslatorInterface $translator + FeedRepositoryInterface $feedRepository, + TranslatorInterface $translator, ) { $this->commandBus = $commandBus; $this->urlGenerator = $urlGenerator; $this->flashBag = $flashBag; $this->translator = $translator; + $this->feedRepository = $feedRepository; } public function __invoke(int $id): RedirectResponse { + /** @var Feed|null $feed */ + $feed = $this->feedRepository->find($id); + + Assert::notNull($feed); + Assert::notEq($feed->getState(), FeedGraph::STATE_PROCESSING); + $this->commandBus->dispatch(new ProcessFeed($id)); $this->flashBag->add('success', $this->translator->trans('setono_sylius_feed.feed_generation_triggered')); diff --git a/src/Controller/Action/Shop/ShowFeedAction.php b/src/Controller/Action/Shop/ShowFeedAction.php index 4aa6871a..03cc71e4 100644 --- a/src/Controller/Action/Shop/ShowFeedAction.php +++ b/src/Controller/Action/Shop/ShowFeedAction.php @@ -34,7 +34,7 @@ public function __construct( LocaleContextInterface $localeContext, FeedPathGeneratorInterface $feedPathGenerator, FilesystemInterface $filesystem, - MimeTypesInterface $mimeTypes + MimeTypesInterface $mimeTypes, ) { $this->repository = $repository; $this->channelContext = $channelContext; diff --git a/src/DataProvider/DataProvider.php b/src/DataProvider/DataProvider.php index 2e49967a..e500edac 100644 --- a/src/DataProvider/DataProvider.php +++ b/src/DataProvider/DataProvider.php @@ -40,7 +40,7 @@ public function __construct( QueryRebuilderInterface $queryRebuilder, EventDispatcherInterface $eventDispatcher, ManagerRegistry $managerRegistry, - string $class + string $class, ) { $this->batcherFactory = $batcherFactory; $this->queryRebuilder = $queryRebuilder; @@ -79,7 +79,8 @@ private function getQueryBuilder(ChannelInterface $channel, LocaleInterface $loc $manager = $this->getManager(); $qb = $manager->createQueryBuilder(); $qb->select('o') - ->from($this->class, 'o'); + ->from($this->class, 'o') + ; $this->eventDispatcher->dispatch(new QueryBuilderEvent($this, $qb, $channel, $locale)); diff --git a/src/DependencyInjection/Compiler/RegisterFilesystemPass.php b/src/DependencyInjection/Compiler/RegisterFilesystemPass.php index d3358b65..988248cd 100644 --- a/src/DependencyInjection/Compiler/RegisterFilesystemPass.php +++ b/src/DependencyInjection/Compiler/RegisterFilesystemPass.php @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container): void $parameter, $definitionClass, FilesystemInterface::class, - FilesystemInterface::class + FilesystemInterface::class, )); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d59a1b18..871ec3ee 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -46,7 +46,8 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() - ->end(); + ->end() + ; $this->addResourcesSection($rootNode); diff --git a/src/Doctrine/ORM/FeedRepository.php b/src/Doctrine/ORM/FeedRepository.php index b3f4e010..4c914663 100644 --- a/src/Doctrine/ORM/FeedRepository.php +++ b/src/Doctrine/ORM/FeedRepository.php @@ -6,6 +6,7 @@ use Setono\SyliusFeedPlugin\Model\FeedInterface; use Setono\SyliusFeedPlugin\Repository\FeedRepositoryInterface; +use Setono\SyliusFeedPlugin\Workflow\FeedGraph; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; class FeedRepository extends EntityRepository implements FeedRepositoryInterface @@ -20,10 +21,12 @@ public function findOneByCode(string $code): ?FeedInterface ; } - public function findEnabled(): array + public function findReadyToBeProcessed(): array { return $this->createQueryBuilder('o') - ->where('o.enabled = true') + ->where('o.state = :state') + ->andWhere('o.enabled = true') + ->setParameter('state', FeedGraph::STATE_READY) ->getQuery() ->getResult() ; diff --git a/src/Doctrine/ORM/ViolationRepository.php b/src/Doctrine/ORM/ViolationRepository.php index ba2b4177..5db8438d 100644 --- a/src/Doctrine/ORM/ViolationRepository.php +++ b/src/Doctrine/ORM/ViolationRepository.php @@ -27,7 +27,8 @@ public function findCountsGroupedBySeverity($feed = null): array if (null !== $feed) { $qb->andWhere('o.feed = :feed') - ->setParameter('feed', $feed); + ->setParameter('feed', $feed) + ; } return $qb->getQuery()->getResult(); diff --git a/src/Event/QueryBuilderEvent.php b/src/Event/QueryBuilderEvent.php index 7e7206a3..1e0455c2 100644 --- a/src/Event/QueryBuilderEvent.php +++ b/src/Event/QueryBuilderEvent.php @@ -23,7 +23,7 @@ public function __construct( DataProviderInterface $dataProvider, QueryBuilder $queryBuilder, ChannelInterface $channel, - LocaleInterface $locale + LocaleInterface $locale, ) { $this->dataProvider = $dataProvider; $this->queryBuilder = $queryBuilder; diff --git a/src/EventListener/MoveGeneratedFeedSubscriber.php b/src/EventListener/MoveGeneratedFeedSubscriber.php index 273b975a..4e4fd69c 100644 --- a/src/EventListener/MoveGeneratedFeedSubscriber.php +++ b/src/EventListener/MoveGeneratedFeedSubscriber.php @@ -29,7 +29,7 @@ public function __construct( FilesystemInterface $temporaryFilesystem, FilesystemInterface $filesystem, FeedPathGeneratorInterface $temporaryFeedPathGenerator, - FeedPathGeneratorInterface $feedPathGenerator + FeedPathGeneratorInterface $feedPathGenerator, ) { $this->temporaryFilesystem = $temporaryFilesystem; $this->filesystem = $filesystem; @@ -60,14 +60,14 @@ public function move(TransitionEvent $event): void $temporaryDir = $this->temporaryFeedPathGenerator->generate( $feed, (string) $channel->getCode(), - (string) $locale->getCode() + (string) $locale->getCode(), ); $temporaryPath = TemporaryFeedPathGenerator::getBaseFile($temporaryDir); $tempFile = $this->temporaryFilesystem->readStream((string) $temporaryPath); if (false === $tempFile) { throw new \RuntimeException(sprintf( 'The file with path "%s" could not be found', - $temporaryPath + $temporaryPath, )); } @@ -75,7 +75,7 @@ public function move(TransitionEvent $event): void $newPath = $this->feedPathGenerator->generate( $feed, (string) $channel->getCode(), - (string) $locale->getCode() + (string) $locale->getCode(), ); $path = sprintf('%s/%s', $newPath->getPath(), uniqid('feed-', true)); $res = $this->filesystem->writeStream($path, $tempFile); diff --git a/src/EventListener/StartProcessingSubscriber.php b/src/EventListener/StartProcessingSubscriber.php index 426d73c0..d5f2035c 100644 --- a/src/EventListener/StartProcessingSubscriber.php +++ b/src/EventListener/StartProcessingSubscriber.php @@ -37,7 +37,7 @@ public function start(TransitionEvent $event): void if (!$feed instanceof FeedInterface) { throw new InvalidArgumentException(sprintf( 'Unexpected type. Expected %s', - FeedInterface::class + FeedInterface::class, )); } diff --git a/src/Factory/ViolationFactory.php b/src/Factory/ViolationFactory.php index 108afd55..ebb7d97e 100644 --- a/src/Factory/ViolationFactory.php +++ b/src/Factory/ViolationFactory.php @@ -35,14 +35,14 @@ public function createFromConstraintViolation( ConstraintViolationInterface $constraintViolation, ChannelInterface $channel, LocaleInterface $locale, - $data = null + $data = null, ): ViolationInterface { $violation = $this->createNew(); $violation->setChannel($channel); $violation->setLocale($locale); $violation->setMessage( - $constraintViolation->getPropertyPath() . ': ' . sprintf((string) $constraintViolation->getMessage(), $constraintViolation->getInvalidValue()) + $constraintViolation->getPropertyPath() . ': ' . sprintf((string) $constraintViolation->getMessage(), $constraintViolation->getInvalidValue()), ); $violation->setData($data); diff --git a/src/Factory/ViolationFactoryInterface.php b/src/Factory/ViolationFactoryInterface.php index b01a865d..0daf4b8d 100644 --- a/src/Factory/ViolationFactoryInterface.php +++ b/src/Factory/ViolationFactoryInterface.php @@ -19,6 +19,6 @@ public function createFromConstraintViolation( ConstraintViolationInterface $constraintViolation, ChannelInterface $channel, LocaleInterface $locale, - $data = null + $data = null, ): ViolationInterface; } diff --git a/src/FeedContext/Google/Shopping/ProductItemContext.php b/src/FeedContext/Google/Shopping/ProductItemContext.php index 12efe872..ebf4e06b 100644 --- a/src/FeedContext/Google/Shopping/ProductItemContext.php +++ b/src/FeedContext/Google/Shopping/ProductItemContext.php @@ -50,7 +50,7 @@ class ProductItemContext implements ItemContextInterface public function __construct( RouterInterface $router, CacheManager $cacheManager, - AvailabilityCheckerInterface $availabilityChecker + AvailabilityCheckerInterface $availabilityChecker, ) { $this->router = $router; $this->cacheManager = $cacheManager; @@ -63,7 +63,7 @@ public function getContextList(object $product, ChannelInterface $channel, Local throw new InvalidArgumentException(sprintf( 'The class %s is not an instance of %s', get_class($product), - ProductInterface::class + ProductInterface::class, )); } @@ -97,7 +97,7 @@ public function getContextList(object $product, ChannelInterface $channel, Local $data->setCondition( $product instanceof ConditionAwareInterface ? - new Condition((string) $product->getCondition()) : Condition::new() + new Condition((string) $product->getCondition()) : Condition::new(), ); if (null !== $productType) { @@ -173,7 +173,7 @@ private function getLink(LocaleInterface $locale, ProductTranslationInterface $t return $this->router->generate( 'sylius_shop_product_show', ['slug' => $translation->getSlug(), '_locale' => $locale->getCode()], - UrlGeneratorInterface::ABSOLUTE_URL + UrlGeneratorInterface::ABSOLUTE_URL, ); } diff --git a/src/FeedType/FeedType.php b/src/FeedType/FeedType.php index 605a2dfa..1d1451df 100644 --- a/src/FeedType/FeedType.php +++ b/src/FeedType/FeedType.php @@ -29,7 +29,7 @@ public function __construct( DataProviderInterface $dataProvider, FeedContextInterface $feedContext, ItemContextInterface $itemContext, - array $validationGroups = [] + array $validationGroups = [], ) { $this->code = $code; $this->template = $template; diff --git a/src/Menu/FeedShowMenuBuilder.php b/src/Menu/FeedShowMenuBuilder.php index 4ce11cc7..a413898f 100644 --- a/src/Menu/FeedShowMenuBuilder.php +++ b/src/Menu/FeedShowMenuBuilder.php @@ -23,7 +23,7 @@ final class FeedShowMenuBuilder public function __construct( FactoryInterface $factory, EventDispatcherInterface $eventDispatcher, - Registry $workflowRegistry + Registry $workflowRegistry, ) { $this->factory = $factory; $this->eventDispatcher = $eventDispatcher; @@ -53,7 +53,8 @@ public function createMenu(array $options): ItemInterface ]) ->setAttribute('type', 'link') ->setLabel('setono_sylius_feed.ui.generate_feed') - ->setLabelAttribute('icon', 'redo'); + ->setLabelAttribute('icon', 'redo') + ; } $menu @@ -63,7 +64,8 @@ public function createMenu(array $options): ItemInterface ]) ->setAttribute('type', 'link') ->setLabel('setono_sylius_feed.ui.edit_feed') - ->setLabelAttribute('icon', 'pencil'); + ->setLabelAttribute('icon', 'pencil') + ; $this->eventDispatcher->dispatch(new FeedShowMenuBuilderEvent($this->factory, $menu, $feed)); diff --git a/src/Message/Handler/FinishGenerationHandler.php b/src/Message/Handler/FinishGenerationHandler.php index 23ea657b..c67c53d6 100644 --- a/src/Message/Handler/FinishGenerationHandler.php +++ b/src/Message/Handler/FinishGenerationHandler.php @@ -52,7 +52,7 @@ public function __construct( Environment $twig, FeedTypeRegistryInterface $feedTypeRegistry, FeedPathGeneratorInterface $temporaryFeedPathGenerator, - LoggerInterface $logger + LoggerInterface $logger, ) { $this->feedRepository = $feedRepository; $this->feedManager = $feedManager; @@ -74,7 +74,7 @@ public function __invoke(FinishGeneration $message): void throw new UnrecoverableMessageHandlingException( 'An error occurred when trying to get the workflow for the feed', 0, - $e + $e, ); } @@ -107,7 +107,7 @@ public function __invoke(FinishGeneration $message): void if (false === $fp) { throw new \RuntimeException(sprintf( 'The file "%s" could not be opened as a resource', - $file['path'] + $file['path'], )); } @@ -137,7 +137,7 @@ public function __invoke(FinishGeneration $message): void throw new RuntimeException(sprintf( 'The feed with id: %d can not be marked as ready because the feed is in a wrong state (%s)', (int) $feed->getId(), - $feed->getState() + $feed->getState(), )); } @@ -169,15 +169,15 @@ private function getFeedParts( FeedInterface $feed, FeedTypeInterface $feedType, ChannelInterface $channel, - LocaleInterface $locale + LocaleInterface $locale, ): array { $template = $this->twig->load('@SetonoSyliusFeedPlugin/Feed/feed.txt.twig'); $content = $template->render( array_merge( $feedType->getFeedContext()->getContext($feed, $channel, $locale), - ['feed' => $feedType->getTemplate()] - ) + ['feed' => $feedType->getTemplate()], + ), ); return explode('', $content); diff --git a/src/Message/Handler/GenerateBatchHandler.php b/src/Message/Handler/GenerateBatchHandler.php index 5a8235d0..41eea4b9 100644 --- a/src/Message/Handler/GenerateBatchHandler.php +++ b/src/Message/Handler/GenerateBatchHandler.php @@ -91,7 +91,7 @@ public function __construct( ViolationFactoryInterface $violationFactory, SerializerInterface $serializer, UrlGeneratorInterface $urlGenerator, - LoggerInterface $logger + LoggerInterface $logger, ) { $this->feedRepository = $feedRepository; $this->channelRepository = $channelRepository; @@ -147,13 +147,13 @@ public function __invoke(GenerateBatch $message): void $feedType, $channel, $locale, - $context + $context, )); $constraintViolationList = $this->validator->validate( $context, null, - $feedType->getValidationGroups() + $feedType->getValidationGroups(), ); $hasErrorViolation = false; @@ -168,7 +168,7 @@ public function __invoke(GenerateBatch $message): void $this->serializer->serialize($context, 'json', [ JsonEncode::OPTIONS => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_IGNORE, 'setono_sylius_feed_data' => true, - ]) + ]), ); if ($violation->getSeverity() === ViolationInterface::SEVERITY_ERROR) { @@ -184,7 +184,7 @@ public function __invoke(GenerateBatch $message): void $channel, $locale, $context, - $constraintViolationList + $constraintViolationList, )); } @@ -279,7 +279,7 @@ private function getWorkflow(FeedInterface $feed): Workflow throw new UnrecoverableMessageHandlingException( 'An error occurred when trying to get the workflow for the feed', 0, - $e + $e, ); } @@ -306,7 +306,7 @@ private function applyErrorTransition(Workflow $workflow, FeedInterface $feed): throw new InvalidArgumentException(sprintf( 'The transition "%s" could not be applied. State was: "%s"', FeedGraph::TRANSITION_ERRORED, - $feed->getState() + $feed->getState(), )); } diff --git a/src/Message/Handler/GenerateFeedHandler.php b/src/Message/Handler/GenerateFeedHandler.php index c962acf5..d454e660 100644 --- a/src/Message/Handler/GenerateFeedHandler.php +++ b/src/Message/Handler/GenerateFeedHandler.php @@ -32,7 +32,7 @@ public function __construct( ChannelRepositoryInterface $channelRepository, RepositoryInterface $localeRepository, FeedTypeRegistryInterface $feedTypeRegistry, - MessageBusInterface $commandBus + MessageBusInterface $commandBus, ) { $this->feedRepository = $feedRepository; $this->channelRepository = $channelRepository; @@ -63,7 +63,7 @@ private function getFeedType(FeedInterface $feed): FeedTypeInterface } catch (InvalidArgumentException $e) { throw new UnrecoverableMessageHandlingException(sprintf( 'Feed type with code "%s" does not exist', - (string) $feed->getFeedType() + (string) $feed->getFeedType(), ), 0, $e); } } diff --git a/src/Message/Handler/ProcessFeedHandler.php b/src/Message/Handler/ProcessFeedHandler.php index c1de2225..c95d5675 100644 --- a/src/Message/Handler/ProcessFeedHandler.php +++ b/src/Message/Handler/ProcessFeedHandler.php @@ -39,7 +39,7 @@ public function __construct( FeedTypeRegistryInterface $feedTypeRegistry, MessageBusInterface $commandBus, Registry $workflowRegistry, - TemplateValidatorInterface $templateValidator + TemplateValidatorInterface $templateValidator, ) { $this->feedRepository = $feedRepository; $this->feedManager = $feedManager; diff --git a/src/Processor/FeedProcessor.php b/src/Processor/FeedProcessor.php index 3cbefb01..758f0431 100644 --- a/src/Processor/FeedProcessor.php +++ b/src/Processor/FeedProcessor.php @@ -27,10 +27,16 @@ public function __construct(FeedRepositoryInterface $feedRepository, MessageBusI public function process(): void { - $feeds = $this->feedRepository->findEnabled(); + $feeds = $this->feedRepository->findReadyToBeProcessed(); foreach ($feeds as $feed) { - $this->logger->info(sprintf('Triggering processing for feed "%s" (id: %d)', (string) $feed->getName(), (int) $feed->getId())); + $this->logger->info( + sprintf( + 'Triggering processing for feed "%s" (id: %d)', + (string) $feed->getName(), + (int) $feed->getId(), + ), + ); $this->commandBus->dispatch(new ProcessFeed((int) $feed->getId())); } } diff --git a/src/Repository/FeedRepositoryInterface.php b/src/Repository/FeedRepositoryInterface.php index 031f4ea0..1d336316 100644 --- a/src/Repository/FeedRepositoryInterface.php +++ b/src/Repository/FeedRepositoryInterface.php @@ -12,11 +12,11 @@ interface FeedRepositoryInterface extends RepositoryInterface public function findOneByCode(string $code): ?FeedInterface; /** - * Returns all enabled feeds + * Returns all feeds that are ready to be processed feeds * * @return FeedInterface[] */ - public function findEnabled(): array; + public function findReadyToBeProcessed(): array; /** * Increments the finished batches count by 1 diff --git a/src/Resources/config/services/controller_action.xml b/src/Resources/config/services/controller_action.xml index 7f4347e0..cfd0add8 100644 --- a/src/Resources/config/services/controller_action.xml +++ b/src/Resources/config/services/controller_action.xml @@ -18,6 +18,7 @@ + diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php index 7ed7c355..60e15295 100644 --- a/src/Twig/Extension.php +++ b/src/Twig/Extension.php @@ -21,7 +21,7 @@ final class Extension extends AbstractExtension public function __construct( RequestStack $requestStack, - UrlGeneratorInterface $urlGenerator + UrlGeneratorInterface $urlGenerator, ) { $this->requestStack = $requestStack; $this->urlGenerator = $urlGenerator; diff --git a/tests/Behat/Context/Cli/ProcessFeedsContext.php b/tests/Behat/Context/Cli/ProcessFeedsContext.php index 6b2c1b0f..049aaa0e 100644 --- a/tests/Behat/Context/Cli/ProcessFeedsContext.php +++ b/tests/Behat/Context/Cli/ProcessFeedsContext.php @@ -49,7 +49,7 @@ public function __construct( FilesystemInterface $filesystem, FeedProcessorInterface $processor, FeedPathGeneratorInterface $feedPathGenerator, - RepositoryInterface $feedRepository + RepositoryInterface $feedRepository, ) { $this->kernel = $kernel; $this->filesystem = $filesystem; @@ -171,7 +171,7 @@ private function normalizeImageLink(string $actualContent): ?string return \preg_replace( '/sylius_shop_product_large_thumbnail\/.*?\.jpe?g/', 'sylius_shop_product_large_thumbnail/%image_path%', - $actualContent + $actualContent, ); } } diff --git a/tests/Behat/Context/Setup/FeedContext.php b/tests/Behat/Context/Setup/FeedContext.php index 2cc06652..58b724b3 100644 --- a/tests/Behat/Context/Setup/FeedContext.php +++ b/tests/Behat/Context/Setup/FeedContext.php @@ -25,7 +25,7 @@ final class FeedContext implements Context public function __construct( FactoryInterface $feedFactory, RepositoryInterface $feedRepository, - ChannelRepositoryInterface $channelRepository + ChannelRepositoryInterface $channelRepository, ) { $this->feedFactory = $feedFactory; $this->feedRepository = $feedRepository;