Completed
Pull Request — master (#26)
by
unknown
02:04
created

FluentLogManager::createFluentHandler()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 26
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 9
nop 1
crap 30
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 Illuminate\Log\LogManager;
24
use Monolog\Handler\HandlerInterface;
25
use Monolog\Logger as Monolog;
26
use Psr\Log\LoggerInterface;
27
28
/**
29
 * Class FluentLogManager
30
 */
31
final class FluentLogManager extends LogManager
32
{
33
    /**
34
     * @var \Illuminate\Contracts\Container\Container
35
     */
36
    protected $app;
37
38
    /**
39
     * @param array $config
40
     *
41
     * @return LoggerInterface
42
     */
43
    protected function createFluentDriver(array $config): LoggerInterface
44
    {
45
        return new Monolog($this->parseChannel($config), [
46
            $this->prepareHandler(
47
                $this->createFluentHandler($config)
48
            ),
49
        ]);
50
    }
51
52
    private function createFluentHandler(array $config) : HandlerInterface
53
    {
54
        $configure = $this->app['config']['fluent'];
55
56
        $packer = null;
57
        if (!is_null($configure['packer'])) {
58
            if (class_exists($configure['packer'])) {
59
                $packer = $this->app->make($configure['packer']);
60
            }
61
        }
62
63
        $fluentHandler = FluentHandler::class;
64
        if (!is_null($configure['handler'])) {
65
            if (class_exists($configure['handler'])) {
66
                $fluentHandler = $configure['handler'];
67
            }
68
        }
69
70
71
        return new $fluentHandler(
72
            new FluentLogger(
73
                $configure['host'] ?? FluentLogger::DEFAULT_ADDRESS,
74
                (int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT,
75
                $configure['options'] ?? [],
76
                $packer
77
            ),
78
            $configure['tagFormat'] ?? null,
79
            $this->level($config)
80
        );
81
    }
82
83
    /**
84
     * @param array $config
85
     *
86
     * @return LoggerInterface
87
     */
88
    public function __invoke(array $config): LoggerInterface
89
    {
90
        return $this->createFluentDriver($config);
91
    }
92
}
93