NatsLogger::getNbCommands()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the NatsBundle package.
4
 *
5
 * (c) Issel Guberna <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Octante\NatsBundle\Logger;
12
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * NatsLogger
17
 */
18
class NatsLogger
19
{
20
    protected $logger;
21
    protected $nbCommands = 0;
22
    protected $commands = array();
23
    protected $start;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param LoggerInterface $logger A LoggerInterface instance
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32
    public function __construct($logger = null)
33
    {
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * Logs a command
39
     *
40
     * @param string      $command    Nats command
41
     * @param float       $duration   Duration in milliseconds
42
     * @param string      $connection Connection alias
43
     * @param bool|string $error      Error message or false if command was successful
44
     */
45
    public function logCommand($command, $duration, $connection, $error = false)
46
    {
47
        ++$this->nbCommands;
48
49
        if (null !== $this->logger) {
50
            $this->commands[] = array(
51
                'cmd'         => $command,
52
                'executionMS' => $duration,
53
                'connection'  => $connection,
54
                'error'       => $error
55
            );
56
57
            if ($error) {
58
                $this->logger->error('Executing "' . $command . '" failed (' . $error . ')');
59
            } else {
60
                $this->logger->info('Executing "' . $command . '"');
61
            }
62
        }
63
    }
64
65
    /**
66
     * Returns the number of logged commands.
67
     *
68
     * @return integer
69
     */
70
    public function getNbCommands()
71
    {
72
        return $this->nbCommands;
73
    }
74
75
    /**
76
     * Returns an array of the logged commands.
77
     *
78
     * @return array
79
     */
80
    public function getCommands()
81
    {
82
        return $this->commands;
83
    }
84
}