1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Monolog\Logger;
15use Monolog\ResettableInterface;
16use Psr\Log\LogLevel;
17
18/**
19 * Base Handler class providing basic level/bubble support
20 *
21 * @author Jordi Boggiano <j.boggiano@seld.be>
22 *
23 * @phpstan-import-type Level from \Monolog\Logger
24 * @phpstan-import-type LevelName from \Monolog\Logger
25 */
26abstract class AbstractHandler extends Handler implements ResettableInterface
27{
28    /**
29     * @var int
30     * @phpstan-var Level
31     */
32    protected $level = Logger::DEBUG;
33    /** @var bool */
34    protected $bubble = true;
35
36    /**
37     * @param int|string $level  The minimum logging level at which this handler will be triggered
38     * @param bool       $bubble Whether the messages that are handled can bubble up the stack or not
39     *
40     * @phpstan-param Level|LevelName|LogLevel::* $level
41     */
42    public function __construct($level = Logger::DEBUG, bool $bubble = true)
43    {
44        $this->setLevel($level);
45        $this->bubble = $bubble;
46    }
47
48    /**
49     * {@inheritDoc}
50     */
51    public function isHandling(array $record): bool
52    {
53        return $record['level'] >= $this->level;
54    }
55
56    /**
57     * Sets minimum logging level at which this handler will be triggered.
58     *
59     * @param  Level|LevelName|LogLevel::* $level Level or level name
60     * @return self
61     */
62    public function setLevel($level): self
63    {
64        $this->level = Logger::toMonologLevel($level);
65
66        return $this;
67    }
68
69    /**
70     * Gets minimum logging level at which this handler will be triggered.
71     *
72     * @return int
73     *
74     * @phpstan-return Level
75     */
76    public function getLevel(): int
77    {
78        return $this->level;
79    }
80
81    /**
82     * Sets the bubbling behavior.
83     *
84     * @param  bool $bubble true means that this handler allows bubbling.
85     *                      false means that bubbling is not permitted.
86     * @return self
87     */
88    public function setBubble(bool $bubble): self
89    {
90        $this->bubble = $bubble;
91
92        return $this;
93    }
94
95    /**
96     * Gets the bubbling behavior.
97     *
98     * @return bool true means that this handler allows bubbling.
99     *              false means that bubbling is not permitted.
100     */
101    public function getBubble(): bool
102    {
103        return $this->bubble;
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    public function reset()
110    {
111    }
112}
113