-
Notifications
You must be signed in to change notification settings - Fork 3
/
CronTab.php
157 lines (140 loc) · 4.5 KB
/
CronTab.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Edgar\EzUICron\Tab\Dashboard;
use Edgar\Cron\Repository\EdgarCronRepository;
use Edgar\CronBundle\Entity\EdgarCron;
use Edgar\EzUICronBundle\Service\EzCronService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Repository;
use EzSystems\EzPlatformAdminUi\Tab\AbstractTab;
use EzSystems\EzPlatformAdminUi\Tab\OrderedTabInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;
/**
* Class CronTab.
*/
class CronTab extends AbstractTab implements OrderedTabInterface
{
/** @var EzCronService $cronService */
protected $cronService;
/** @var Repository $repository */
protected $repository;
/** @var PermissionResolver */
private $permissionResolver;
/**
* CronTab constructor.
*
* @param Environment $twig
* @param TranslatorInterface $translator
* @param Repository $repository
* @param EzCronService $cronService
* @param PermissionResolver $permissionResolver
*/
public function __construct(
Environment $twig,
TranslatorInterface $translator,
Repository $repository,
EzCronService $cronService,
PermissionResolver $permissionResolver
) {
parent::__construct($twig, $translator);
$this->repository = $repository;
$this->cronService = $cronService;
$this->permissionResolver = $permissionResolver;
}
/**
* @return string
*/
public function getIdentifier(): string
{
return 'cron';
}
/**
* @return string
*/
public function getName(): string
{
return
$this->translator->trans('tab.name.cron', [], 'dashboard');
}
/**
* @return int
*/
public function getOrder(): int
{
return 300;
}
/**
* @param array $parameters
*
* @return string
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function renderView(array $parameters): string
{
if (!$this->permissionAccess('uicron', 'dashboard')) {
return $this->twig->render('@EdgarEzUICron/dashboard/tab/cron.html.twig', [
'crons' => [],
]);
}
$crons = $this->cronService->listCronsStatus();
$cronRows = [];
foreach ($crons as $cron) {
$cronStatus = '';
$cronReset = false;
if ($cron instanceof EdgarCron) {
switch ($cron->getStatus()) {
case EdgarCronRepository::STATUS_INIT:
$cronStatus = $this->translator->trans('Init', [], 'edgarezcron');
$cronReset = true;
break;
case EdgarCronRepository::STATUS_OK:
$cronStatus = $this->translator->trans('OK', [], 'edgarezcron');
break;
case EdgarCronRepository::STATUS_ERROR:
$cronStatus = $this->translator->trans('Error', [], 'edgarezcron');
$cronReset = true;
break;
default:
$cronStatus = '';
break;
}
}
$cronRows[] = [
'alias' => $cron->getAlias(),
'queued' => $cron instanceof EdgarCron
? ($cron->getQueued() ? $cron->getQueued() : false)
: false,
'started' => $cron instanceof EdgarCron ? ($cron->getStarted()
? $cron->getStarted() : false)
: false,
'ended' => $cron instanceof EdgarCron ? ($cron->getEnded()
? $cron->getEnded() : false)
: false,
'status' => $cronStatus,
'reset' => $cronReset,
];
}
return $this->twig->render('@EdgarEzUICron/dashboard/tab/cron.html.twig', [
'crons' => $cronRows,
]);
}
/**
* @param string $module
* @param string $function
*
* @return bool
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
protected function permissionAccess(string $module, string $function): bool
{
if (!$this->permissionResolver->hasAccess($module, $function)) {
return false;
}
return true;
}
}