Debug   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 81
ccs 23
cts 31
cp 0.7419
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A startStopwatch() 0 12 3
A finishStopwatch() 0 18 3
A log() 0 14 3
A logCacheFetch() 0 9 4
1
<?php
2
/**
3
 * This file contains functionality related to debugging, logging and timing actions
4
 *
5
 * @package    BZiON
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
namespace BZIon\Debug;
10
11
/**
12
 * Useful debugging functions (will only waste time on a development environment)
13
 */
14
abstract class Debug
15
{
16
    /**
17
     * Start counting time for an event
18
     * @param  string $event The event's name
19
     * @return void
20
     */
21 76
    public static function startStopwatch($event)
22
    {
23 76
        if (!\Service::getContainer()) {
24
            return;
25
        }
26
27 76
        $stopwatch = \Service::getContainer()->get('debug.stopwatch', null);
28
29 76
        if ($stopwatch) {
30 76
            $stopwatch->start($event);
31
        }
32 76
    }
33
34
    /**
35
     * Stop counting time for an event and get its duration
36
     * @param  string $event The event's name
37
     * @return int    The time in milliseconds
38
     */
39 76
    public static function finishStopwatch($event)
40
    {
41 76
        if (!\Service::getContainer()) {
42
            return 0;
43
        }
44
45 76
        $stopwatch = \Service::getContainer()->get('debug.stopwatch', null);
46
47 76
        if (!$stopwatch) {
48
            return 0;
49
        }
50
51 76
        $event = $stopwatch->stop($event);
52 76
        $periods = $event->getPeriods();
53 76
        $duration = end($periods)->getDuration();
54
55 76
        return $duration;
56
    }
57
58
    /**
59
     * Log a debug message
60
     * @param  string $message The message to return
61
     * @param  array  $context Any additional information to show
62
     * @param  string $channel The channel to store the log into
63
     * @return void
64
     */
65 76
    public static function log($message, array $context = array(), $channel = 'app')
66
    {
67 76
        if (!\Service::getContainer()) {
68
            return;
69
        }
70
71 76
        $logger = \Service::getContainer()->get("monolog.logger.$channel", null);
72
73 76
        if (!$logger) {
74
            return;
75
        }
76
77 76
        $logger->debug($message, $context);
78 76
    }
79
80
    /**
81
     * Log a cache fetch in the data collector
82
     * @param string $type The type of the fetched model
83
     * @param int    $id   The ID of the fetched model
84
     */
85 76
    public static function logCacheFetch($type, $id)
86
    {
87 76
        if (\Service::isDebug() && \Service::getContainer()) {
88
            $collector = \Service::getContainer()->get('data_collector.bzion_database_collector', null);
89
            if ($collector) {
90
                $collector->logCacheFetch($type, $id);
91
            }
92
        }
93 76
    }
94
}
95