Passed
Push — main ( 492649...1b3827 )
by De Cramer
07:40
created

ChainExecutionLogger::write()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
4
namespace Oliverde8\PhpEtlBundle\Services;
5
6
use Monolog\Handler\AbstractProcessingHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\AbstractProcessingHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Monolog\Handler\StreamHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\StreamHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Monolog\Logger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Oliverde8\PhpEtlBundle\Entity\EtlExecution;
10
11
class ChainExecutionLogger extends AbstractProcessingHandler
12
{
13
    /** @var ChainWorkDirManager */
14
    protected $chainWorkDirManager;
15
16
    protected $level;
17
18
    /** @var EtlExecution */
19
    protected $currentExecution;
20
21
    /** @var AbstractProcessingHandler */
22
    protected $currentStreamHandler;
23
24
    /**
25
     * ChainExecutionLogger constructor.
26
     *
27
     * @param ChainProcessorsManager $chainProcessorManager
28
     * @param ChainWorkDirManager $chainWorkDirManager
29
     * @param string $level
30
     */
31
    public function __construct(ChainWorkDirManager $chainWorkDirManager, string $level = Logger::DEBUG)
32
    {
33
        $this->chainWorkDirManager = $chainWorkDirManager;
34
        $this->level = $level;
35
    }
36
37
38
    protected function write(array $record)
39
    {
40
        // TODO Optimize this by moving it into isHandling.
41
        if (is_null($this->currentExecution) || is_null($this->currentStreamHandler)) {
42
            return;
43
        }
44
45
        $this->currentStreamHandler->write($record);
46
    }
47
48
    /**
49
     * @param EtlExecution|null $currentExecution
50
     */
51
    public function setCurrentExecution(?EtlExecution $currentExecution): void
52
    {
53
        if ($this->currentStreamHandler) {
54
            $this->currentStreamHandler->close();
55
            $this->currentStreamHandler = null;
56
        }
57
58
        $this->currentExecution = $currentExecution;
59
        if ($currentExecution) {
60
            $logFile = $this->chainWorkDirManager->getWorkDir($currentExecution) . "/execution.log";
61
            $this->currentStreamHandler = new StreamHandler($logFile, $this->level);
62
        }
63
    }
64
}