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

DebuggableTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 67
wmc 9
lcom 1
cbo 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enableDebug() 0 5 1
A setDebugger() 0 5 1
A delegateDebugger() 0 9 3
A isDebugging() 0 4 2
A debug() 0 10 2
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