Passed
Push — master ( 80df18...80df18 )
by Dāvis
04:16
created

SludioLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 3 2
A error() 0 3 1
A __construct() 0 7 4
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Logger;
4
5
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
6
use Psr\Log\LoggerInterface;
7
8
class SludioLogger
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) {
22
            throw new InvalidArgumentException(sprintf('SludioLogger 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
    /**
29
     * Logs a command
30
     *
31
     * @param string      $command Sludio command
32
     * @param null|string $error   Error message or null
33
     * @param string      $type    Log type
34
     */
35
    public function log($command, $error = null, $type = 'info')
36
    {
37
        $this->logger->{(string)$type}($command.': '.strtoupper((string)$type).($error !== null ? ': '.(string)$error : ''));
38
    }
39
40
    public function error($command, $error)
41
    {
42
        $this->log($command, $error, 'error');
43
    }
44
}
45