Passed
Push — master ( 8ba614...a9bcb8 )
by Dāvis
02:54
created

SludioLogger::__construct()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 9.2
c 1
b 0
f 1
1
<?php
2
3
namespace Sludio\HelperBundle\Logger;
4
5
use Psr\Log\LoggerInterface;
6
use InvalidArgumentException;
7
8
class SludioLogger
9
{
10
    protected $logger;
11
12
    /**
13
     * Constructor.
14
     *
15
     * @param LoggerInterface $logger
16
     */
17
    public function __construct($logger = null)
18
    {
19
        if (!$logger instanceof LoggerInterface && null !== $logger) {
20
            throw new InvalidArgumentException(sprintf('SludioLogger needs PSR-3 LoggerInterface, "%s" was injected instead.', is_object($logger) ? get_class($logger) : gettype($logger)));
21
        }
22
23
        $this->logger = $logger;
24
    }
25
26
    /**
27
     * Logs a command
28
     *
29
     * @param string      $command Sludio command
30
     * @param bool|string $error   Error message or null
31
     * @param string      $type    Log type
32
     */
33
    public function log($command, $error = false, $type = 'info')
34
    {
35
        $this->logger->{(string)$type}($command.': '.strtoupper((string)$type).($error !== false ? ': '.$error : ''));
0 ignored issues
show
Bug introduced by
Are you sure $error of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->logger->{(string)$type}($command.': '.strtoupper((string)$type).($error !== false ? ': './** @scrutinizer ignore-type */ $error : ''));
Loading history...
36
    }
37
38
    public function error($command, $error)
39
    {
40
        $this->log($command, $error, 'error');
41
    }
42
}