Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not process already processing feeds #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/Controller/Action/Admin/ProcessFeedActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function let(
MessageBusInterface $commandBus,
UrlGeneratorInterface $urlGenerator,
FlashBagInterface $flashBag,
TranslatorInterface $translator
TranslatorInterface $translator,
): void {
$this->beConstructedWith($commandBus, $urlGenerator, $flashBag, $translator);
}
Expand Down
16 changes: 15 additions & 1 deletion src/Controller/Action/Admin/ProcessFeedAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Action/Shop/ShowFeedAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
LocaleContextInterface $localeContext,
FeedPathGeneratorInterface $feedPathGenerator,
FilesystemInterface $filesystem,
MimeTypesInterface $mimeTypes
MimeTypesInterface $mimeTypes,
) {
$this->repository = $repository;
$this->channelContext = $channelContext;
Expand Down
5 changes: 3 additions & 2 deletions src/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
QueryRebuilderInterface $queryRebuilder,
EventDispatcherInterface $eventDispatcher,
ManagerRegistry $managerRegistry,
string $class
string $class,
) {
$this->batcherFactory = $batcherFactory;
$this->queryRebuilder = $queryRebuilder;
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container): void
$parameter,
$definitionClass,
FilesystemInterface::class,
FilesystemInterface::class
FilesystemInterface::class,
));
}

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->end();
->end()
;

$this->addResourcesSection($rootNode);

Expand Down
7 changes: 5 additions & 2 deletions src/Doctrine/ORM/FeedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
;
Expand Down
3 changes: 2 additions & 1 deletion src/Doctrine/ORM/ViolationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Event/QueryBuilderEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
DataProviderInterface $dataProvider,
QueryBuilder $queryBuilder,
ChannelInterface $channel,
LocaleInterface $locale
LocaleInterface $locale,
) {
$this->dataProvider = $dataProvider;
$this->queryBuilder = $queryBuilder;
Expand Down
8 changes: 4 additions & 4 deletions src/EventListener/MoveGeneratedFeedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
FilesystemInterface $temporaryFilesystem,
FilesystemInterface $filesystem,
FeedPathGeneratorInterface $temporaryFeedPathGenerator,
FeedPathGeneratorInterface $feedPathGenerator
FeedPathGeneratorInterface $feedPathGenerator,
) {
$this->temporaryFilesystem = $temporaryFilesystem;
$this->filesystem = $filesystem;
Expand Down Expand Up @@ -60,22 +60,22 @@ 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,
));
}

// move the file from the temporary location to a temp file in the *not* temporary directory
$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);
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/StartProcessingSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Factory/ViolationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Factory/ViolationFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function createFromConstraintViolation(
ConstraintViolationInterface $constraintViolation,
ChannelInterface $channel,
LocaleInterface $locale,
$data = null
$data = null,
): ViolationInterface;
}
8 changes: 4 additions & 4 deletions src/FeedContext/Google/Shopping/ProductItemContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
));
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/FeedType/FeedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
DataProviderInterface $dataProvider,
FeedContextInterface $feedContext,
ItemContextInterface $itemContext,
array $validationGroups = []
array $validationGroups = [],
) {
$this->code = $code;
$this->template = $template;
Expand Down
8 changes: 5 additions & 3 deletions src/Menu/FeedShowMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class FeedShowMenuBuilder
public function __construct(
FactoryInterface $factory,
EventDispatcherInterface $eventDispatcher,
Registry $workflowRegistry
Registry $workflowRegistry,
) {
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
Expand Down Expand Up @@ -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
Expand All @@ -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));

Expand Down
14 changes: 7 additions & 7 deletions src/Message/Handler/FinishGenerationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
Environment $twig,
FeedTypeRegistryInterface $feedTypeRegistry,
FeedPathGeneratorInterface $temporaryFeedPathGenerator,
LoggerInterface $logger
LoggerInterface $logger,
) {
$this->feedRepository = $feedRepository;
$this->feedManager = $feedManager;
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -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'],
));
}

Expand Down Expand Up @@ -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(),
));
}

Expand Down Expand Up @@ -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('<!-- ITEM_BOUNDARY -->', $content);
Expand Down
Loading