Completed
Push — master ( 195b94...7a36f1 )
by Mohamed
04:50
created

SendMessages   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 45
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Console\Commands;
13
14
use Illuminate\Console\Command;
15
use Illuminate\Contracts\Mail\Mailer;
16
use Tinyissue\Model;
17
18
/**
19
 * SendMessages is console command to process all of the stacked messages.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class SendMessages extends Command
24
{
25
    /**
26
     * The console command name.
27
     *
28
     * @var string
29
     */
30
    protected $name = 'tinyissue:messages';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Send stacked messages.';
38
39
    /**
40
     * Execute the job.
41
     *
42
     * @param Mailer $mailer
43
     *
44
     * @return void
45
     */
46
    public function handle(Mailer $mailer)
47
    {
48
        $queue = new Model\Message\Queue();
49
        // Get all latest messages in the queue
50
        $records = $queue->latestMessages()->get();
51
52
        // pull out the first item in the list
53
        while ($latest = $records->first()) {
54
            // Get all other records for the same issue & remove them from collection
55
            $others = $records->where('model_id', $latest->model_id)->where('model_type', $latest->model_type);
56
            $records->forget($others->keys()->toArray());
57
58
            // Ask the class to process these messages
59
            app($latest->model_type . '\\SendMessages')->setMailer($mailer)->process($latest, $others);
60
61
            // Remove message queue
62
            $others->each(function (Model\Message\Queue $queue) {
63
                $queue->delete();
64
            });
65
        }
66
    }
67
}
68