Passed
Push — master ( 9b369b...9fe481 )
by Dāvis
04:59
created

Logger::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7
8
class Logger
9
{
10
    protected $logger;
11
12
    /**
13
     * Constructor.
14
     *
15
     * @param LoggerInterface $logger
16
     *
17
     * @throws InvalidArgumentException
18
     */
19
    public function __construct($logger = null)
20
    {
21
        if (null !== $logger && !$logger instanceof LoggerInterface) {
0 ignored issues
show
introduced by
The condition null !== $logger && ! $l...Psr\Log\LoggerInterface can never be true.
Loading history...
22
            throw new InvalidArgumentException(sprintf('Logger needs PSR-3 LoggerInterface, "%s" was injected instead.', \is_object($logger) ? \get_class($logger) : \gettype($logger)));
23
        }
24
25
        $this->logger = $logger;
26
    }
27
28
    public function error($command, $error)
29
    {
30
        $this->log($command, $error, 'error');
31
    }
32
33
    /**
34
     * Logs a command
35
     *
36
     * @param string      $command Ccommand
37
     * @param null|string $error   Error message or null
38
     * @param string      $type    Log type
39
     */
40
    public function log($command, $error = null, $type = 'info')
41
    {
42
        $this->logger->{(string)$type}($command.': '.strtoupper((string)$type).($error !== null ? ': '.(string)$error : ''));
43
    }
44
}
45