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\ResettableInterface;
15use Monolog\Formatter\FormatterInterface;
16
17/**
18 * This simple wrapper class can be used to extend handlers functionality.
19 *
20 * Example: A custom filtering that can be applied to any handler.
21 *
22 * Inherit from this class and override handle() like this:
23 *
24 *   public function handle(array $record)
25 *   {
26 *        if ($record meets certain conditions) {
27 *            return false;
28 *        }
29 *        return $this->handler->handle($record);
30 *   }
31 *
32 * @author Alexey Karapetov <alexey@karapetov.com>
33 */
34class HandlerWrapper implements HandlerInterface, ProcessableHandlerInterface, FormattableHandlerInterface, ResettableInterface
35{
36    /**
37     * @var HandlerInterface
38     */
39    protected $handler;
40
41    public function __construct(HandlerInterface $handler)
42    {
43        $this->handler = $handler;
44    }
45
46    /**
47     * {@inheritDoc}
48     */
49    public function isHandling(array $record): bool
50    {
51        return $this->handler->isHandling($record);
52    }
53
54    /**
55     * {@inheritDoc}
56     */
57    public function handle(array $record): bool
58    {
59        return $this->handler->handle($record);
60    }
61
62    /**
63     * {@inheritDoc}
64     */
65    public function handleBatch(array $records): void
66    {
67        $this->handler->handleBatch($records);
68    }
69
70    /**
71     * {@inheritDoc}
72     */
73    public function close(): void
74    {
75        $this->handler->close();
76    }
77
78    /**
79     * {@inheritDoc}
80     */
81    public function pushProcessor(callable $callback): HandlerInterface
82    {
83        if ($this->handler instanceof ProcessableHandlerInterface) {
84            $this->handler->pushProcessor($callback);
85
86            return $this;
87        }
88
89        throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
90    }
91
92    /**
93     * {@inheritDoc}
94     */
95    public function popProcessor(): callable
96    {
97        if ($this->handler instanceof ProcessableHandlerInterface) {
98            return $this->handler->popProcessor();
99        }
100
101        throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    public function setFormatter(FormatterInterface $formatter): HandlerInterface
108    {
109        if ($this->handler instanceof FormattableHandlerInterface) {
110            $this->handler->setFormatter($formatter);
111
112            return $this;
113        }
114
115        throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
116    }
117
118    /**
119     * {@inheritDoc}
120     */
121    public function getFormatter(): FormatterInterface
122    {
123        if ($this->handler instanceof FormattableHandlerInterface) {
124            return $this->handler->getFormatter();
125        }
126
127        throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
128    }
129
130    public function reset()
131    {
132        if ($this->handler instanceof ResettableInterface) {
133            $this->handler->reset();
134        }
135    }
136}
137