Completed
Pull Request — master (#2)
by
unknown
38:19
created

MongoJobContract   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 0
loc 113
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A delete() 0 8 2
A attempts() 0 4 1
A reserved() 0 4 1
A reservedAt() 0 4 1
A getJobId() 0 4 1
A getRawBody() 0 4 1
A resolve() 0 4 1
1
<?php
2
3
namespace SfCod\QueueBundle\Job;
4
5
use SfCod\QueueBundle\Base\JobInterface;
6
use SfCod\QueueBundle\Base\JobResolverInterface;
7
use SfCod\QueueBundle\Queue\QueueInterface;
8
use stdClass;
9
10
/**
11
 * MongoJob for laravel queue
12
 *
13
 * @author Alexey Orlov <[email protected]>
14
 * @author Virchenko Maksim <[email protected]>
15
 */
16
class MongoJobContract extends JobContract implements JobContractInterface
17
{
18
    /**
19
     * Job resolver
20
     *
21
     * @var JobResolverInterface
22
     */
23
    protected $resolver;
24
25
    /**
26
     * The database queue instance.
27
     *
28
     * @var QueueInterface
29
     */
30
    protected $database;
31
32
    /**
33
     * The database job payload.
34
     *
35
     * @var StdClass
36
     */
37
    protected $job;
38
39
    /**
40
     * Create a new job instance.
41
     *
42
     * @param JobResolverInterface $resolver
43
     * @param QueueInterface $database
44
     * @param StdClass|MongoDB\Model\BSONDocument $job
45
     * @param string $queue
46
     */
47
    public function __construct(JobResolverInterface $resolver, QueueInterface $database, $job, string $queue)
48
    {
49
        $this->resolver = $resolver;
50
        $this->database = $database;
51
        $this->job = $job;
0 ignored issues
show
Documentation Bug introduced by
It seems like $job can also be of type object<SfCod\QueueBundle...oDB\Model\BSONDocument>. However, the property $job is declared as type object<SfCod\QueueBundle\Job\StdClass>. 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...
52
        $this->queue = $queue;
53
    }
54
55
    /**
56
     * Delete the job from the queue.
57
     */
58
    public function delete()
59
    {
60
        parent::delete();
61
62
        if ($this->database->deleteReserved($this->queue, (string)$this->getJobId())) {
63
            $this->deleted = true;
64
        }
65
    }
66
67
    /**
68
     * Get the number of times the job has been attempted.
69
     *
70
     * @return int
71
     */
72
    public function attempts(): int
73
    {
74
        return (int)$this->job->attempts;
75
    }
76
77
    /**
78
     * Check if job reserved
79
     *
80
     * @return bool
81
     */
82
    public function reserved(): bool
83
    {
84
        return (bool)$this->job->reserved;
85
    }
86
87
    /**
88
     * Get reserved at time
89
     *
90
     * @return int
91
     */
92
    public function reservedAt(): int
93
    {
94
        return (int)$this->job->reserved_at;
95
    }
96
97
    /**
98
     * Get the job identifier.
99
     *
100
     * @return string
101
     */
102
    public function getJobId(): string
103
    {
104
        return (string)$this->job->_id;
105
    }
106
107
    /**
108
     * Get the raw body string for the job.
109
     *
110
     * @return string
111
     */
112
    public function getRawBody(): string
113
    {
114
        return $this->job->payload;
115
    }
116
117
    /**
118
     * Resolve job
119
     *
120
     * @param string $class
121
     *
122
     * @return JobInterface
123
     */
124
    protected function resolve(string $class): JobInterface
125
    {
126
        return $this->resolver->resolve($class);
127
    }
128
}
129