Completed
Push — master ( 5c7609...7cde34 )
by Marco
02:41
created

Manager::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php namespace Comodojo\Foundation\Logging;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Monolog\Logger;
5
use \Monolog\Handler\HandlerInterface;
6
7
/**
8
 * @package     Comodojo Foundation
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class Manager {
24
25
    const DEFAULT_LOGGER_NAME = 'comodojo';
26
27
    private $logger;
28
29
    private $base_path;
30
31 1
    public function __construct($name = null) {
32
33 1
        $this->logger = new Logger(empty($name) ? self::DEFAULT_LOGGER_NAME : $name);
34
35 1
    }
36
37 1
    public function setBasePath($path) {
38
39 1
        $this->base_path = $path;
40
41 1
    }
42
43
    public function getBasePath() {
44
45
        return $this->base_path;
46
47
    }
48
49 2
    public function getLogger() {
50
51 2
        return $this->logger;
52
53
    }
54
55 1
    public function init($enable = true, $providers = array()) {
56
57 1
        if ( !$enable ) {
58
59
            $this->logger->pushHandler( ProviderBuilder::NullHandler() );
60
61
            return $this;
62
63
        }
64
65 1
        if ( empty($providers) ) return $this;
66
67 1
        foreach ($providers as $provider => $parameters) {
68
69 1
            $handler = $this->createProvider($provider, $parameters);
70
71 1
            if ( $handler instanceof HandlerInterface ) $this->logger->pushHandler($handler);
72
73 1
        }
74
75 1
        return $this;
76
77
    }
78
79 1
    public static function createFromConfiguration(Configuration $configuration, $stanza = null) {
80
81 1
        $base_path = $configuration->get('base-path');
82
83 1
        $log = null;
84
85 1
        if ( $stanza !== null ) {
86
            $log = $configuration->get($stanza);
87
        }
88
89 1
        if ( $log === null ) {
90 1
            $log = $configuration->get('log');
91 1
        }
92
93 1
        $name = empty($log['name']) ? null : $log['name'];
94
95 1
        $enable = (empty($log['enable']) || $log['enable'] !== false) ? true : false;
96
97 1
        $providers = empty($log['providers']) ? [] : $log['providers'];
98
99 1
        $manager = new Manager($name);
100
101 1
        $manager->setBasePath($base_path);
102
103 1
        return $manager->init($enable, $providers);
104
105
    }
106
107
    public static function create($name = null, $enable = true, $providers = array()) {
108
109
        $manager = new Manager($name);
110
111
        return $manager->init($enable, $providers);
112
113
    }
114
115 1
    private function createProvider($provider, $parameters) {
116
117 1
        if ( empty($parameters['type']) ) return null;
118
119 1
        switch ( $parameters['type'] ) {
120
121 1
            case 'StreamHandler':
122 1
                $handler = ProviderBuilder::StreamHandler($provider, $this->base_path, $parameters);
123 1
                break;
124
125
            case 'SyslogHandler':
126
                $handler = ProviderBuilder::SyslogHandler($provider, $parameters);
127
                break;
128
129
            case 'ErrorLogHandler':
130
                $handler = ProviderBuilder::ErrorLogHandler($provider, $parameters);
131
                break;
132
133
            case 'NullHandler':
134
                $handler = ProviderBuilder::NullHandler($provider, $parameters);
135
                break;
136
137
            default:
138
                $handler = null;
139
                break;
140
141 1
        }
142
143 1
        return $handler;
144
145
    }
146
147
}
148