-
Notifications
You must be signed in to change notification settings - Fork 88
/
MyFeaturePostActivityListLoadEventSubscriber.php
56 lines (47 loc) · 1.74 KB
/
MyFeaturePostActivityListLoadEventSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types=1);
namespace App\EventSubscriber;
use App\MyFeature\MyFeature;
use App\MyFeature\MyFeatureService;
use Ibexa\Contracts\ActivityLog\Event\PostActivityGroupListLoadEvent;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyFeaturePostActivityListLoadEventSubscriber implements EventSubscriberInterface
{
private MyFeatureService $myFeatureService;
public function __construct(
MyFeatureService $myFeatureService
) {
$this->myFeatureService = $myFeatureService;
}
public static function getSubscribedEvents(): array
{
return [
PostActivityGroupListLoadEvent::class => ['loadMyFeature'],
];
}
public function loadMyFeature(PostActivityGroupListLoadEvent $event): void
{
$visitedIds = [];
$list = $event->getList();
foreach ($list as $logGroup) {
foreach ($logGroup->getActivityLogs() as $log) {
if ($log->getObjectClass() !== MyFeature::class) {
continue;
}
$id = (int)$log->getObjectId();
try {
if (!array_key_exists($id, $visitedIds)) {
$visitedIds[$id] = $this->myFeatureService->load($id);
}
if ($visitedIds[$id] === null) {
continue;
}
$log->setRelatedObject($visitedIds[$id]);
} catch (NotFoundException|UnauthorizedException $e) {
$visitedIds[$id] = null;
}
}
}
}
}