Completed
Pull Request — 3.x (#200)
by
unknown
06:29 queued 03:16
created

LoggerConsumer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 4
c 3
b 2
f 0
lcom 1
cbo 3
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A process() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Consumer;
13
14
use Psr\Log\LoggerInterface;
15
use Sonata\NotificationBundle\Exception\InvalidParameterException;
16
use Symfony\Component\HttpKernel\Log\LoggerInterface as LegacyLoggerInterface;
17
18
class LoggerConsumer implements ConsumerInterface
19
{
20
    /**
21
     * @var LoggerInterface
22
     */
23
    protected $logger;
24
25
    /**
26
     * @var string[]
27
     */
28
    protected $types = array(
29
        'emerg' => 'emergency',
30
        'alert' => 'alert',
31
        'crit' => 'critical',
32
        'err' => 'error',
33
        'warn' => 'warning',
34
        'notice' => 'notice',
35
        'info' => 'info',
36
        'debug' => 'debug',
37
    );
38
39
    /**
40
     * @param LoggerInterface|LegacyLoggerInterface $logger
41
     */
42
    public function __construct($logger)
43
    {
44
        if ($logger instanceof LegacyLoggerInterface) {
45
            trigger_error(sprintf('Using an instance of "%s" is deprecated since version 2.3. Use Psr\Log\LoggerInterface instead.', get_class($logger)), E_USER_DEPRECATED);
46
        }
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function process(ConsumerEvent $event)
54
    {
55
        $message = $event->getMessage();
56
57
        if (!array_key_exists($message->getValue('level'), $this->types)) {
58
            throw new InvalidParameterException();
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\NotificationBundl...validParameterException has been deprecated with message: Use Sonata\CoreBundle\Exception\InvalidParameterException instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
59
        }
60
61
        $level = $this->types[$message->getValue('level')];
62
63
        $this->logger->{$level}($message->getValue('message'));
64
    }
65
}
66