Passed
Push — master ( 806c04...bf1804 )
by Hirofumi
05:36
created

RequeueAbandonedJobMessageService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 48
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 18 4
1
<?php
2
3
namespace Shippinno\Job\Application\Messaging;
4
5
use Interop\Queue\Exception as QueueException;
6
use Interop\Queue\PsrContext;
7
use Shippinno\Job\Domain\Model\AbandonedJobMessageFailedToRequeueException;
8
use Shippinno\Job\Domain\Model\AbandonedJobMessageStore;
9
10
class RequeueAbandonedJobMessageService
11
{
12
    /**
13
     * @var PsrContext
14
     */
15
    private $context;
16
17
    /**
18
     * @var AbandonedJobMessageStore
19
     */
20
    private $abandonedJobMessageStore;
21
22
    /**
23
     * @param PsrContext $context
24
     * @param AbandonedJobMessageStore $abandonedJobMessageStore
25
     */
26 3
    public function __construct(
27
        PsrContext $context,
28
        AbandonedJobMessageStore $abandonedJobMessageStore
29
    ) {
30 3
        $this->context = $context;
31 3
        $this->abandonedJobMessageStore = $abandonedJobMessageStore;
32 3
    }
33
34
    /**
35
     * @param int $abandonedJobMessageId
36
     * @throws AbandonedJobMessageFailedToRequeueException
37
     * @throws \Shippinno\Job\Domain\Model\AbandonedJobMessageNotFoundException
38
     */
39 3
    public function execute(int $abandonedJobMessageId): void
40
    {
41 3
        $abandonedJobMessage = $this->abandonedJobMessageStore->abandonedJobMessageOfId($abandonedJobMessageId);
42 2
        $queue = $this->context->createQueue($abandonedJobMessage->queue());
43 2
        $message = $this->context->createMessage($abandonedJobMessage->message());
44 2
        if (method_exists($message, 'setMessageDeduplicationId')) {
45
            $message->setMessageDeduplicationId(uniqid());
46
        }
47 2
        if (method_exists($message, 'setMessageGroupId')) {
48
            $message->setMessageGroupId(uniqid());
49
        }
50
        try {
51 2
            $this->context->createProducer()->send($queue, $message);
52 1
            $this->abandonedJobMessageStore->remove($abandonedJobMessage);
0 ignored issues
show
Bug introduced by
It seems like $abandonedJobMessage defined by $this->abandonedJobMessa...$abandonedJobMessageId) on line 41 can be null; however, Shippinno\Job\Domain\Mod...bMessageStore::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
53 1
        } catch (QueueException $e) {
54 1
            throw new AbandonedJobMessageFailedToRequeueException($abandonedJobMessage->id(), $e);
55
        }
56 1
    }
57
}
58