Passed
Push — master ( d25cd5...5cf75b )
by Nikolaos
03:08
created

Syslog::openlog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Logger\Adapter;
15
16
use LogicException;
17
use Phalcon\Helper\Arr;
18
use Phalcon\Logger;
19
use Phalcon\Logger\Item;
20
use function closelog;
21
use function openlog;
22
use function sprintf;
23
use const LOG_ALERT;
24
use const LOG_CRIT;
25
use const LOG_DEBUG;
26
use const LOG_EMERG;
27
use const LOG_ERR;
28
use const LOG_INFO;
29
use const LOG_NOTICE;
30
use const LOG_ODELAY;
31
use const LOG_USER;
32
use const LOG_WARNING;
33
34
/**
35
 * Class Syslog
36
 *
37
 * @property string $defaultFormatter
38
 * @property int    $facility
39
 * @property string $name
40
 * @property bool   $opened
41
 * @property int    $option
42
 */
43
class Syslog extends AbstractAdapter
44
{
45
    /**
46
     * Name of the default formatter class
47
     *
48
     * @var string
49
     */
50
    protected $defaultFormatter = "Line";
51
52
    /**
53
     * @var int
54
     */
55
    protected $facility = 0;
56
57
    /**
58
     * @var string
59
     */
60
    protected $name = "";
61
62
    /**
63
     * @var bool
64
     */
65
    protected $opened = false;
66
67
    /**
68
     * @var int
69
     */
70
    protected $option = 0;
71
72
    /**
73
     * Syslog constructor.
74
     *
75
     * @param string $name
76
     * @param array  $options
77
     */
78 26
    public function __construct(string $name, array $options = [])
79
    {
80 26
        $this->name     = $name;
81 26
        $this->option   = Arr::get($options, "option", LOG_ODELAY);
82 26
        $this->facility = Arr::get($options, "facility", LOG_USER);
83 26
    }
84
85
    /**
86
     * Closes the logger
87
     */
88 22
    public function close(): bool
89
    {
90 22
        if (true !== $this->opened) {
91 16
            return true;
92
        }
93
94 6
        return closelog();
95
    }
96
97
    /**
98
     * Processes the message i.e. writes it to the syslog
99
     *
100
     * @param Item $item
101
     *
102
     * @throws LogicException
103
     */
104 8
    public function process(Item $item): void
105
    {
106 8
        $formatter = $this->getFormatter();
107 8
        $message   = $formatter->format($item);
108 8
        $result    = $this->openlog($this->name, $this->option, $this->facility);
109
110 8
        if (!$result) {
111 2
            throw new LogicException(
112 2
                sprintf(
113 2
                    "Cannot open syslog for name [%s] and facility [%s]",
114 2
                    $this->name,
115 2
                    (string) $this->facility
116
                )
117
            );
118
        }
119
120 6
        $this->opened = true;
121 6
        $level        = $this->logLevelToSyslog($item->getType());
122
123 6
        \syslog($level, $message);
124 6
    }
125
126
    /**
127
     * Open connection to system logger
128
     *
129
     * @link https://php.net/manual/en/function.openlog.php
130
     *
131
     * @param string $ident
132
     * @param int    $option
133
     * @param int    $facility
134
     *
135
     * @return bool
136
     */
137 6
    protected function openlog($ident, $option, $facility)
138
    {
139 6
        return openlog($ident, $option, $facility);
140
    }
141
142
    /**
143
     * Translates a Logger level to a Syslog level
144
     *
145
     * @param int $level
146
     *
147
     * @return int
148
     */
149 6
    private function logLevelToSyslog(int $level): int
150
    {
151
        $levels = [
152 6
            Logger::ALERT     => LOG_ALERT,
153
            Logger::CRITICAL  => LOG_CRIT,
154
            Logger::CUSTOM    => LOG_ERR,
155
            Logger::DEBUG     => LOG_DEBUG,
156
            Logger::EMERGENCY => LOG_EMERG,
157
            Logger::ERROR     => LOG_ERR,
158
            Logger::INFO      => LOG_INFO,
159
            Logger::NOTICE    => LOG_NOTICE,
160
            Logger::WARNING   => LOG_WARNING,
161
        ];
162
163 6
        return Arr::get($levels, $level, LOG_ERR);
164
    }
165
}
166