LoggerConsumer::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Consumer;
15
16
use Psr\Log\LoggerInterface;
17
use Sonata\NotificationBundle\Exception\InvalidParameterException;
18
use Symfony\Component\HttpKernel\Log\LoggerInterface as LegacyLoggerInterface;
19
20
class LoggerConsumer implements ConsumerInterface
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    protected $logger;
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $types = [
31
        'emerg' => 'emergency',
32
        'alert' => 'alert',
33
        'crit' => 'critical',
34
        'err' => 'error',
35
        'warn' => 'warning',
36
        'notice' => 'notice',
37
        'info' => 'info',
38
        'debug' => 'debug',
39
    ];
40
41
    /**
42
     * @param LoggerInterface|LegacyLoggerInterface $logger
43
     */
44
    public function __construct($logger)
45
    {
46
        if ($logger instanceof LegacyLoggerInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpKernel\Log\LoggerInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
                sprintf(
49
                    'Using an instance of "%s" is deprecated since version 2.3. Use Psr\Log\LoggerInterface instead.',
50
                    \get_class($logger)
51
                ),
52
                E_USER_DEPRECATED
53
            );
54
        }
55
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger can also be of type object<Symfony\Component...el\Log\LoggerInterface>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function process(ConsumerEvent $event): void
62
    {
63
        $message = $event->getMessage();
64
65
        if (!\array_key_exists($message->getValue('level'), $this->types)) {
66
            throw new InvalidParameterException();
67
        }
68
69
        $level = $this->types[$message->getValue('level')];
70
71
        $this->logger->{$level}($message->getValue('message'));
72
    }
73
}
74