MongoFailedJobProvider::getCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace yiicod\jobqueue\failed;
4
5
use Carbon\Carbon;
6
use Illuminate\Queue\Failed\FailedJobProviderInterface;
7
8
/**
9
 * Mongo provider for failed jobs
10
 *
11
 * @author Virchenko Maksim <[email protected]>
12
 */
13
class MongoFailedJobProvider implements FailedJobProviderInterface
14
{
15
    /**
16
     * The database connection name.
17
     *
18
     * @var string
19
     */
20
    protected $database;
21
22
    /**
23
     * The database table.
24
     *
25
     * @var string
26
     */
27
    protected $table;
28
29
    /**
30
     * Create a new database failed job provider.
31
     *
32
     * @param  string $database
33
     * @param  string $table
34
     */
35
    public function __construct($database, $table)
36
    {
37
        $this->database = $database;
38
        $this->table = $table;
39
    }
40
41
    /**
42
     * Log a failed job into storage.
43
     *
44
     * @param  string $connection
45
     * @param  string $queue
46
     * @param  string $payload
47
     * @param \Exception $exception
48
     *
49
     * @return int|null|void
50
     */
51
    public function log($connection, $queue, $payload, $exception)
52
    {
53
        $this->getCollection()->insert([
54
            'connection' => $connection,
55
            'queue' => $queue,
56
            'payload' => $payload,
57
            'exception' => $exception->getMessage(),
58
            'failed_at' => Carbon::now(),
59
        ]);
60
    }
61
62
    /**
63
     * Get a list of all of the failed jobs.
64
     *
65
     * @return array
66
     */
67
    public function all()
68
    {
69
        $result = [];
70
        $data = $this->getCollection()->find([])->sort(['_id' => -1]);
71
        foreach ($data as $item) {
72
            $result[] = $item;
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * Get a single failed job.
80
     *
81
     * @param  mixed $id
82
     *
83
     * @return array
84
     */
85
    public function find($id)
86
    {
87
        return $this->getCollection()->find(['_id' => new \MongoDB\BSON\ObjectID($id)]);
88
    }
89
90
    /**
91
     * Delete a single failed job from storage.
92
     *
93
     * @param  mixed $id
94
     *
95
     * @return bool
96
     */
97
    public function forget($id)
98
    {
99
        return $this->getCollection()->remove(['_id' => new \MongoDB\BSON\ObjectID($id)]);
100
    }
101
102
    /**
103
     * Flush all of the failed jobs from storage.
104
     */
105
    public function flush()
106
    {
107
        $this->getCollection()->remove();
108
    }
109
110
    /**
111
     * Get a new query builder instance for the table.
112
     *
113
     * @return object mongo collection
114
     */
115
    protected function getCollection()
116
    {
117
        return $this->database->getDatabase()->getCollection($this->table);
0 ignored issues
show
Bug introduced by
The method getDatabase cannot be called on $this->database (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
118
    }
119
}
120