Completed
Pull Request — master (#7)
by Ross
02:53
created

ClassPropertiesFormatter::logCommandSucceeded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
namespace League\Tactician\Logger\Formatter;
3
4
use League\Tactician\Logger\PropertyNormalizer\PropertyNormalizer;
5
use League\Tactician\Logger\PropertyNormalizer\SimplePropertyNormalizer;
6
use Exception;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
10
/**
11
 * Formatter that includes the Command's name and properties for more detail
12
 */
13
class ClassPropertiesFormatter implements Formatter
14
{
15
    /**
16
     * @var PropertyNormalizer
17
     */
18
    private $normalizer;
19
20
    /**
21
     * @var string
22
     */
23
    private $commandReceivedLevel;
24
25
    /**
26
     * @var string
27
     */
28
    private $commandSucceededLevel;
29
30
    /**
31
     * @var string
32
     */
33
    private $commandFailedLevel;
34
35
    /**
36
     * @param PropertyNormalizer $normalizer
37
     * @param string $commandReceivedLevel
38
     * @param string $commandSucceededLevel
39
     * @param string $commandFailedLevel
40
     */
41
    public function __construct(
42
        PropertyNormalizer $normalizer = null,
43
        $commandReceivedLevel = LogLevel::DEBUG,
44
        $commandSucceededLevel = LogLevel::DEBUG,
45
        $commandFailedLevel = LogLevel::ERROR
46
    ) {
47
48
        $this->normalizer = $normalizer ?: new SimplePropertyNormalizer();
49
        $this->commandReceivedLevel = $commandReceivedLevel;
50
        $this->commandSucceededLevel = $commandSucceededLevel;
51
        $this->commandFailedLevel = $commandFailedLevel;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 View Code Duplication
    public function logCommandReceived(LoggerInterface $logger, $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $logger->log(
60
            $this->commandReceivedLevel,
61
            'Command received: ' . get_class($command),
62
            ['command' => $this->normalizer->normalize($command)]
63
        );
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 View Code Duplication
    public function logCommandSucceeded(LoggerInterface $logger, $command, $returnValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $logger->log(
72
            $this->commandSucceededLevel,
73
            'Command succeeded: ' . get_class($command),
74
            [
75
                'command' => $this->normalizer->normalize($command)
76
            ]
77
        );
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function logCommandFailed(LoggerInterface $logger, $command, Exception $e)
84
    {
85
        $logger->log(
86
            $this->commandFailedLevel,
87
            'Command failed: ' . get_class($command),
88
            ['exception' => $e]
89
        );
90
    }
91
}
92