Completed
Push — master ( c26f0f...c7af1e )
by Konstantinos
18:52
created

Debug::logCacheFetch()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 2
crap 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 38
    public static function startStopwatch($event)
22
    {
23 38
        if (!\Service::getContainer()) {
24
            return;
25
        }
26
27 38
        $stopwatch = \Service::getContainer()->get('debug.stopwatch', null);
28
29 38
        if ($stopwatch) {
30 38
            $stopwatch->start($event);
31 38
        }
32 38
    }
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 38
    public static function finishStopwatch($event)
40
    {
41 38
        if (!\Service::getContainer()) {
42
            return 0;
43
        }
44
45 38
        $stopwatch = \Service::getContainer()->get('debug.stopwatch', null);
46
47 38
        if (!$stopwatch) {
48
            return 0;
49
        }
50
51 38
        $event = $stopwatch->stop($event);
52 38
        $periods = $event->getPeriods();
53 38
        $duration = end($periods)->getDuration();
54
55 38
        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 38
    public static function log($message, array $context = array(), $channel = 'app')
66
    {
67 38
        if (!\Service::getContainer()) {
68
            return;
69
        }
70
71 38
        $logger = \Service::getContainer()->get("monolog.logger.$channel", null);
72
73 38
        if (!$logger) {
74
            return;
75
        }
76
77 38
        $logger->debug($message, $context);
78 38
    }
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 38
    public static function logCacheFetch($type, $id)
86
    {
87 38
        if (\Service::isDebug() && \Service::getContainer()) {
88 38
            $collector = \Service::getContainer()->get('data_collector.bzion_database_collector', null);
89 38
            if ($collector) {
90 38
                $collector->logCacheFetch($type, $id);
91 38
            }
92 38
        }
93 38
    }
94
}
95