LastMovies::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 21
rs 9.3142
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Http\Models\Movies;
6
use Carbon\Carbon;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Mail;
9
10
/**
11
 * Task to send an email to user favoris
12
 * Class LastMovies.
13
 */
14
class LastMovies extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'movies:newsletter';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Send an email for suscribers to last movies this last month';
29
30
    /**
31
     * Create a new command instance.
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        // SQL query
46
        $results = Movies::where('date_release', '>=', new Carbon('-1 month'))
47
                        ->where('date_release', '<=', new Carbon('now'))
48
                        ->get();
49
50
        // je parcours mes films
51
        foreach ($results as $movie) {
52
            $users = $movie->actors();
53
            foreach ($users as $user) {
54
                //send an email
55
                Mail::send('Emails/newsletter', [], function ($m) {
56
57
                    $m->from('[email protected]', 'Florent Boyer');
58
                    $m->to('[email protected]', 'Boyer Julien')
59
                        ->subject('Welcome to newsletter');
60
                });
61
            }
62
        }
63
    }
64
}
65