Completed
Push — master ( 1e94e4...f09733 )
by Hong
03:08
created

DebuggableTrait::delegateDebugger()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Debug;
16
17
use Psr\Log\LoggerInterface;
18
19
/**
20
 * DebuggableTrait
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     DebuggableInterface
25
 * @version 2.0.26
26
 * @since   2.0.26 added
27
 */
28
trait DebuggableTrait
29
{
30
    /**
31
     * @var    bool
32
     * @access protected
33
     */
34
    protected $debug_mode = false;
35
36
    /**
37
     * @var    LoggerInterface
38
     * @access protected
39
     */
40
    protected $debug_logger;
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function enableDebug(/*# bool */ $flag = true)
46
    {
47
        $this->debug_mode = (bool) $flag;
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function setDebugger(LoggerInterface $logger)
55
    {
56
        $this->debug_logger = $logger;
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function delegateDebugger($object)
64
    {
65
        if ($this->isDebugging() &&
66
            $object instanceof DebuggableInterface
67
        ) {
68
            $object->enableDebug(true)->setDebugger($this->debug_logger);
69
        }
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function isDebugging()/*# : bool */
77
    {
78
        return (bool) ($this->debug_mode && $this->debug_logger);
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function debug(/*# string */ $message)
85
    {
86
        if ($this->isDebugging()) {
87
            $this->debug_logger->debug(
88
                $message,
89
                ['className' => get_class($this)]
90
            );
91
        }
92
        return $this;
93
    }
94
}
95