Skip to content

Commit

Permalink
Merge pull request #13 from iMi-digital/8-business-events
Browse files Browse the repository at this point in the history
Support flow event log
  • Loading branch information
shyim authored Oct 4, 2024
2 parents 5a788dc + e7b01c2 commit d31694d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Exception/FlowEventException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Frosh\SentryBundle\Exception;

use Monolog\Level;
use Throwable;

class FlowEventException extends \Exception
{
public function __construct(readonly string $eventName = '', ?Level $level = null, ?Throwable $previous = null, readonly mixed $context = null)
{
$message = sprintf('Flow event %s level %s occurred', $this->eventName, $level?->toPsrLogLevel() ?? 'unknown');
parent::__construct($message, $level?->value ?? 0, $previous);
}

}
5 changes: 5 additions & 0 deletions src/ShopwareSentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Frosh\SentryBundle\Integration\UseShopwareExceptionIgnores;
use Frosh\SentryBundle\Listener\FixRequestUrlListener;
use Frosh\SentryBundle\Listener\SalesChannelContextListener;
use Frosh\SentryBundle\Subscriber\FlowLogSubscriber;
use Frosh\SentryBundle\Subscriber\ScheduledTaskSubscriber;
use Shopware\Core\System\SalesChannel\Event\SalesChannelContextCreatedEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -44,6 +45,10 @@ public function build(ContainerBuilder $container): void
->addArgument('%frosh_sentry.report_scheduled_tasks%')
->addTag('kernel.event_subscriber');

$container
->register(FlowLogSubscriber::class)
->addTag('kernel.event_subscriber');

$container->addCompilerPass(new CompilerPass\ExceptionConfigCompilerPass());
}

Expand Down
48 changes: 48 additions & 0 deletions src/Subscriber/FlowLogSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Frosh\SentryBundle\Subscriber;

use Frosh\SentryBundle\Exception\FlowEventException;
use Monolog\Level;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\Event\FlowLogEvent;
use Shopware\Core\Framework\Log\LogAware;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskCollection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use function Sentry\captureException;

class FlowLogSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FlowLogEvent::NAME => 'sendFlowEvent'];
}

public function sendFlowEvent(FlowLogEvent $event): void
{
$innerEvent = $event->getEvent();

$additionalData = [];
$logLevel = Level::Debug;

if ($innerEvent instanceof LogAware) {
$logLevel = $innerEvent->getLogLevel();
$additionalData = $innerEvent->getLogData();
}

if ($logLevel->isLowerThan(Level::Warning)) {
return;
}

$nestedException = null;
if (method_exists($innerEvent, 'getThrowable')) {
$nestedException = $innerEvent->getThrowable();
}

captureException(new FlowEventException($innerEvent->getName(), $logLevel, $nestedException, $additionalData));
}

}
4 changes: 2 additions & 2 deletions src/Subscriber/ScheduledTaskSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function onScheduledTaskWritten(EntityWrittenEvent $event): void

private function captureCheckIn(ScheduledTaskEntity $scheduledTask, CheckInStatus $status): void
{
if($status === CheckInStatus::inProgress()) {
if ($status === CheckInStatus::inProgress()) {
$scheduledTask->removeExtension('sentryCheckInId');
$checkInId = $this->getCheckInId($scheduledTask);
$scheduledTask->addArrayExtension('sentryCheckInId', [$checkInId]);
Expand All @@ -116,7 +116,7 @@ private function captureCheckIn(ScheduledTaskEntity $scheduledTask, CheckInStatu
private function getCheckInId(ScheduledTaskEntity $scheduledTask): ?string
{
$extension = $scheduledTask->getExtension('sentryCheckInId');
if($extension instanceof ArrayStruct) {
if ($extension instanceof ArrayStruct) {
return \is_string($extension->get(0)) ? $extension->get(0) : null;
}

Expand Down

0 comments on commit d31694d

Please sign in to comment.