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

PISHPS-348: memory issue + phpstan #822

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/Resources/config/services/scheduled_tasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<tag name="messenger.message_handler"/>
</service>

<service id="Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder\RenewalReminderTaskDevHandler">
<argument type="service" id="Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepository"/>
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\SubscriptionManager"/>
<argument type="service" id="mollie_payments.logger"/>
<tag name="messenger.message_handler"/>
</service>

<service id="Kiener\MolliePayments\ScheduledTask\OrderStatus\ExpireOrderTask">
<tag name="shopware.scheduled.task"/>
Expand Down
3 changes: 3 additions & 0 deletions src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: ExpireOrderTask::class)]
Expand Down Expand Up @@ -40,6 +41,8 @@ public function run(): void
$criteria = new Criteria();
$criteria->addAssociation('transactions.stateMachineState');
$criteria->getAssociation('transactions.stateMachineState')->addFilter(new EqualsFilter('technicalName', OrderStates::STATE_IN_PROGRESS));
$criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING));
$criteria->setLimit(10);

$this->logger->debug('Search for orders which are in progress state');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
use Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepositoryInterface;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

abstract class AbstractRenewalReminderTaskHandler extends ScheduledTaskHandler
{
/**
* @var SubscriptionManager
*/
private $subscriptionManager;

/**
* @var LoggerInterface
*/
private $logger;


/**
* @param ScheduledTaskRepositoryInterface $scheduledTaskRepository
* @param SubscriptionManager $subscriptionManager
* @param LoggerInterface $logger
*/
public function __construct(ScheduledTaskRepositoryInterface $scheduledTaskRepository, SubscriptionManager $subscriptionManager, LoggerInterface $logger)
{
/** @phpstan-ignore-next-line */
parent::__construct($scheduledTaskRepository->getRepository(), $logger);

$this->subscriptionManager = $subscriptionManager;
$this->logger = $logger;
}

/**
* @return iterable<mixed>
*/
public static function getHandledMessages(): iterable
{
return [
RenewalReminderTask::class,
RenewalReminderTaskDev::class,
];
}

/**
*
*/
public function run(): void
{
try {
$this->logger->info('Starting Subscription Renewal Reminder from Scheduled Tasks.');

$context = new Context(new SystemSource());

$remindedCount = $this->subscriptionManager->remindSubscriptionRenewal($context);

$this->logger->debug($remindedCount . ' subscriptions renewal reminders have been processed successfully!');
} catch (\Throwable $ex) {
$this->logger->error(
'Error when running Scheduled Task for Subscription Renewal Reminders. ' . $ex->getMessage()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTaskDev::class)]
class RenewalReminderTaskDevHandler extends AbstractRenewalReminderTaskHandler
{
}
67 changes: 3 additions & 64 deletions src/ScheduledTask/Subscription/RenewalReminder/RenewalReminderTaskHandler.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
use Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepositoryInterface;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTask::class)]
#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTaskDev::class)]
class RenewalReminderTaskHandler extends ScheduledTaskHandler
class RenewalReminderTaskHandler extends AbstractRenewalReminderTaskHandler
{
/**
* @var SubscriptionManager
*/
private $subscriptionManager;

/**
* @var LoggerInterface
*/
private $logger;


/**
* @param ScheduledTaskRepositoryInterface $scheduledTaskRepository
* @param SubscriptionManager $subscriptionManager
* @param LoggerInterface $logger
*/
public function __construct(ScheduledTaskRepositoryInterface $scheduledTaskRepository, SubscriptionManager $subscriptionManager, LoggerInterface $logger)
{
/** @phpstan-ignore-next-line */
parent::__construct($scheduledTaskRepository->getRepository(), $logger);

$this->subscriptionManager = $subscriptionManager;
$this->logger = $logger;
}

/**
* @return iterable<mixed>
*/
public static function getHandledMessages(): iterable
{
return [
RenewalReminderTask::class,
RenewalReminderTaskDev::class,
];
}

/**
*
*/
public function run(): void
{
try {
$this->logger->info('Starting Subscription Renewal Reminder from Scheduled Tasks.');

$context = Context::createDefaultContext();

$remindedCount = $this->subscriptionManager->remindSubscriptionRenewal($context);

$this->logger->debug($remindedCount . ' subscriptions renewal reminders have been processed successfully!');
} catch (\Throwable $ex) {
$this->logger->error(
'Error when running Scheduled Task for Subscription Renewal Reminders. ' . $ex->getMessage()
);
}
}
}
Loading