Passed
Pull Request — master (#27)
by yuuki
07:28
created

FluentLogManager::detectHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2018 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelFluent;
21
22
use Fluent\Logger\FluentLogger;
23
use Fluent\Logger\PackerInterface;
24
use Illuminate\Log\LogManager;
25
use Monolog\Handler\HandlerInterface;
26
use Monolog\Logger as Monolog;
27
use Psr\Log\LoggerInterface;
28
29
use function is_null;
30
use function class_exists;
31
use function strval;
32
33
/**
34
 * Class FluentLogManager
35
 */
36
final class FluentLogManager extends LogManager
37
{
38
    /** @var \Illuminate\Contracts\Container\Container */
39
    protected $app;
40
41
    /**
42
     * @param array $config
43
     *
44
     * @return LoggerInterface
45
     */
46
    protected function createFluentDriver(array $config): LoggerInterface
47
    {
48
        return new Monolog($this->parseChannel($config), [
49
            $this->prepareHandler(
50
                $this->createFluentHandler($config)
51
            ),
52
        ]);
53
    }
54
55
    /**
56
     * @param array $config
57
     *
58
     * @return HandlerInterface
59
     */
60
    private function createFluentHandler(array $config): HandlerInterface
61
    {
62
        $configure = $this->app['config']['fluent'];
63
        $fluentHandler = $this->detectHandler($configure);
64
        return new $fluentHandler(
65
            new FluentLogger(
66
                $configure['host'] ?? FluentLogger::DEFAULT_ADDRESS,
67
                (int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT,
68
                $configure['options'] ?? [],
69
                $this->detectPacker($configure)
70
            ),
71
            $configure['tagFormat'] ?? null,
72
            $this->level($config)
73
        );
74
    }
75
76
    /**
77
     * @param array $config
78
     *
79
     * @return LoggerInterface
80
     */
81
    public function __invoke(array $config): LoggerInterface
82
    {
83
        return $this->createFluentDriver($config);
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    protected function defaultHandler(): string
90
    {
91
        return FluentHandler::class;
92
    }
93
94
    /**
95
     * @param array $configure
96
     *
97
     * @return PackerInterface|null
98
     */
99
    protected function detectPacker(array $configure): ?PackerInterface
100
    {
101
        if (!is_null($configure['packer'])) {
102
            if (class_exists($configure['packer'])) {
103
                return $this->app->make($configure['packer']);
104
            }
105
        }
106
        return null;
107
    }
108
109
    /**
110
     * @param array $configure
111
     *
112
     * @return string
113
     */
114
    protected function detectHandler(array $configure): string
115
    {
116
        $handler = $configure['handler'] ?? null;
117
        if (!is_null($handler)) {
118
            if (class_exists($handler)) {
119
                return strval($handler);
120
            }
121
        }
122
        return $this->defaultHandler();
123
    }
124
}
125