forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctrineProcessHandler.php
87 lines (75 loc) · 2.43 KB
/
DoctrineProcessHandler.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
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Monolog\Handler;
use CleverAge\UiProcessBundle\Entity\ProcessExecution;
use CleverAge\UiProcessBundle\Manager\ProcessExecutionManager;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Level;
use Monolog\LogRecord;
class DoctrineProcessHandler extends AbstractProcessingHandler
{
/** @var ArrayCollection<int, LogRecord> */
private ArrayCollection $records;
private ?EntityManagerInterface $em = null;
private ?ProcessExecutionManager $processExecutionManager = null;
private bool $enabled = true;
public function __construct(int|string|Level $level = Level::Debug, bool $bubble = true)
{
parent::__construct($level, $bubble);
$this->records = new ArrayCollection();
}
public function setEntityManager(EntityManagerInterface $em): void
{
$this->em = $em;
}
public function setProcessExecutionManager(ProcessExecutionManager $processExecutionManager): void
{
$this->processExecutionManager = $processExecutionManager;
}
public function disable(): void
{
$this->enabled = false;
}
public function __destruct()
{
$this->flush();
parent::__destruct();
}
public function flush(): void
{
if (!$this->enabled) {
return;
}
foreach ($this->records as $record) {
if (($currentProcessExecution = $this->processExecutionManager?->getCurrentProcessExecution()) instanceof ProcessExecution) {
$entity = new \CleverAge\UiProcessBundle\Entity\LogRecord($record, $currentProcessExecution);
$this->em?->persist($entity);
}
}
$this->em?->flush();
foreach ($this->records as $record) {
$this->em?->detach($record);
}
$this->records = new ArrayCollection();
}
protected function write(LogRecord $record): void
{
if (!$this->enabled) {
return;
}
$this->records->add($record);
if (500 === $this->records->count()) {
$this->flush();
}
}
}