Logger::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Yep\WorkflowLogger;
4
5
use Yep\WorkflowLogger\Exception\WorkflowIsLockedException;
6
use Yep\WorkflowLogger\Formatter\FormatterInterface;
7
8
/**
9
 * Class Logger
10
 *
11
 * @package Yep\WorkflowLogger
12
 * @author  Martin Zeman (Zemistr) <[email protected]>
13
 */
14
class Logger extends \Monolog\Logger implements LoggerInterface
15
{
16
    /**
17
     * Detailed workflow debug information
18
     */
19
    const WORKFLOW = 111;
20
21
    /**
22
     * @var FormatterInterface
23
     */
24
    protected $workflowFormatter;
25
26
    /**
27
     * Logging levels from syslog protocol defined in RFC 5424
28
     *
29
     * @var array $levels Logging levels
30
     */
31
    protected static $levels = [
32
      self::WORKFLOW => 'WORKFLOW',
33
    ];
34
35
    /**
36
     * @var array|Workflow[]
37
     */
38
    protected $workflows = [];
39
40
    /**
41
     * Logger constructor.
42
     *
43
     * @param string             $name
44
     * @param FormatterInterface $formatter
45
     * @param array              $handlers
46
     * @param array              $processors
47
     */
48 2
    public function __construct(
49
      $name,
50
      FormatterInterface $formatter,
51
      $handlers = [],
52
      $processors = []
53
    ) {
54 2
        parent::__construct($name, $handlers, $processors);
55 2
        static::$levels += parent::$levels;
56 2
        $this->workflowFormatter = $formatter;
57 2
    }
58
59
    /**
60
     * @return Workflow
61
     */
62 1
    protected function createWorkflow()
63
    {
64 1
        return new Workflow(
65 1
          $this,
66 1
          $this->workflowFormatter,
67 1
          $this->getDateTimeZone(),
68 1
          static::WORKFLOW
69
        );
70
    }
71
72
    /**
73
     * @return \DateTimeZone
74
     */
75 2
    protected function getDateTimeZone()
76
    {
77 2
        if (static::$timezone) {
78 2
            return static::$timezone;
79
        }
80
81 1
        $timezone = date_default_timezone_get() ?: 'UTC';
82
83 1
        return static::$timezone = new \DateTimeZone($timezone);
84
    }
85
86
    /**
87
     * @param string|null $key
88
     * @return Workflow
89
     * @throws WorkflowIsLockedException
90
     */
91 1
    public function workflow($key = null)
92
    {
93 1
        if ($key === null) {
94 1
            return $this->createWorkflow();
95
        }
96
97 1
        if (!isset($this->workflows[$key])) {
98 1
            $this->workflows[$key] = $this->createWorkflow();
99
        }
100
101 1
        if ($this->workflows[$key]->isLocked()) {
102 1
            throw WorkflowIsLockedException::create($key);
103
        }
104
105 1
        return $this->workflows[$key];
106
    }
107
}
108