Passed
Branch master (e1ddde)
by Michael
03:56 queued 02:21
created

LoggingConfigurator::setConfigDir()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * {CLASS SUMMARY}
4
 *
5
 * Date: 10/6/18
6
 * Time: 2:43 PM
7
 * @author Michael Munger <[email protected]>
8
 */
9
10
namespace HPHIO\Util;
11
12
13
use Monolog\Handler\StreamHandler;
14
use Monolog\Logger;
15
16
class LoggingConfigurator
17
{
18
    public $applicationRoot = null;
19
    public $configDir       = null;
20
    public $configs         = null;
21
    private $container      = null;
22
23 3
    public function __construct($container)
24
    {
25 3
        $this->container = $container;
26 3
    }
27
28 3
    public function setAppRoot($directory) {
29 3
        $this->applicationRoot = $directory;
30 3
    }
31
32 3
    public function setConfigDir($configDirectory) {
33 3
        $this->configDir = $this->applicationRoot . "/" . $configDirectory;
34
35 3
        if(file_exists($this->configDir) === false ) mkdir($this->configDir);
36
37 3
        return (file_exists($this->configDir) && is_writable($this->configDir));
38
    }
39
40 2
    public function loadLoggingConf() {
41 2
        $this->configs = json_decode(file_get_contents($this->configDir . '/logging.json'));
42 2
        return (json_last_error() === JSON_ERROR_NONE);
43
    }
44
45 2
    public function getServiceLogConfig($logName) {
46
47 2
        foreach($this->configs->services as $logObject) {
48
49 2
            $config = $this->container->get(LoggingConfig::class);
50 2
            $config->importJSON($logObject);
51
52 2
            if($logObject->name === $logName) return $config;
53
        }
54
55 1
        return false;
56
    }
57
58
    /**
59
     * @param LoggingConfig $config
60
     * @throws \Exception
61
     */
62
63 1
    public function getLogger(LoggingConfig $config) {
64
65 1
        $log = new Logger($config->name);
66 1
        $log->pushHandler(new StreamHandler($config->logPath(), Logger::INFO));
67 1
        return $log;
68
    }
69
}