Completed
Pull Request — 1.1 (#8)
by
unknown
02:55
created

AbstractConsumer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Mouf\AmqpClient;
4
5
use Doctrine\ORM\ORMException;
6
use Mouf\Utils\Log\ErrorLogLogger;
7
use Psr\Log\LoggerInterface;
8
9
abstract class AbstractConsumer implements ConsumerInterface
10
{
11
    use ConsumerTrait;
12
13
    /**
14
     * @var LoggerInterface
15
     */
16
    private $logger;
17
18
    /**
19
     * AbstractConsumer constructor.
20
     * @param LoggerInterface|null $logger
21
     */
22
    public function __construct(LoggerInterface $logger = null)
23
    {
24
        $this->logger = $logger ?: new ErrorLogLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?: new \Mouf\Utils\Log\ErrorLogLogger() can also be of type object<Mouf\Utils\Log\ErrorLogLogger>. 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...
25
    }
26
27
    public function callback($msg)
28
    {
29
        try {
30
            $this->onMessageReceived($msg);
31
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
32
        } catch (RetryableExceptionInterface $e) {
33
            $this->logger->error("Exception caught while consuming message.", [
34
                'exception' => $e
35
            ]);
36
            $msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag'], true, true);
37
            if ($e instanceof FatalExceptionInterface) {
38
                throw $e;
39
            }
40
        } catch (\Exception $e) {
41
            $this->logger->error("Exception caught while consuming message.", [
42
                'exception' => $e
43
            ]);
44
            $msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag'], true, false);
45
            if ($e instanceof FatalExceptionInterface) {
46
                throw $e;
47
            }
48
        }
49
    }
50
51
    abstract public function onMessageReceived($msg);
52
}
53