/
MonitorRecentContentCreationCommand.php
80 lines (67 loc) · 3.36 KB
/
MonitorRecentContentCreationCommand.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\ActivityLog\ActivityLogServiceInterface;
use Ibexa\Contracts\ActivityLog\Values\ActivityLog\Criterion;
use Ibexa\Contracts\ActivityLog\Values\ActivityLog\Query;
use Ibexa\Contracts\ActivityLog\Values\ActivityLog\SortClause\LoggedAtSortClause;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class MonitorRecentContentCreationCommand extends Command
{
protected static $defaultName = 'app:monitor-content-creation';
protected static $defaultDescription = 'List last 10 log entry groups with creations in the last hour';
private ActivityLogServiceInterface $activityLogService;
private PermissionResolver $permissionResolver;
private UserService $userService;
public function __construct(ActivityLogServiceInterface $activityLogService, PermissionResolver $permissionResolver, UserService $userService)
{
$this->permissionResolver = $permissionResolver;
$this->userService = $userService;
$this->activityLogService = $activityLogService;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$query = new Query([
new Criterion\ObjectCriterion(Content::class),
new Criterion\ActionCriterion([ActivityLogServiceInterface::ACTION_CREATE]),
new Criterion\LoggedAtCriterion(new \DateTime('- 1 hour'), Criterion\LoggedAtCriterion::GTE),
], [new LoggedAtSortClause(LoggedAtSortClause::DESC)], 0, 10);
$io = new SymfonyStyle($input, $output);
$this->permissionResolver->setCurrentUserReference($this->userService->loadUserByLogin('admin'));
foreach ($this->activityLogService->findGroups($query) as $activityLogGroup) {
if ($activityLogGroup->getSource()) {
$io->section($activityLogGroup->getSource()->getName());
}
if ($activityLogGroup->getDescription()) {
$io->text($activityLogGroup->getDescription());
}
$table = [];
foreach ($activityLogGroup->getActivityLogs() as $activityLog) {
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
$content = $activityLog->getRelatedObject();
$name = $content && $content->getName() && $content->getName() !== $activityLog->getObjectName() ? "“{$content->getName()}” (formerly “{$activityLog->getObjectName()}”)" : "“{$activityLog->getObjectName()}”";
$table[] = [
$activityLogGroup->getLoggedAt()->format(\DateTime::ATOM),
$activityLog->getObjectId(),
$name,
$activityLog->getAction(),
$activityLogGroup->getUser()->login,
];
}
$io->table([
'Logged at',
'Obj. ID',
'Object Name',
'Action',
'User',
], $table);
}
return Command::SUCCESS;
}
}