Debug::log()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
crap 3.1406
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