Completed
Push — master ( 4c949c...532c30 )
by Julien
11:23
created

LastMovies::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
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 22
rs 9.2
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
class LastMovies extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'movies:newsletter';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Send an email for suscribers to last movies this last month';
25
26
    /**
27
     * Create a new command instance.
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function handle()
40
    {
41
42
        // SQL query
43
        $results = Movies::where('date_release', '>=', new Carbon('-1 month'))
44
                        ->where('date_release', '<=', new Carbon('now'))
45
                        ->get();
46
47
        // je parcours mes films
48
        foreach ($results as $movie) {
49
            $users = $movie->actors();
50
            foreach ($users as $user) {
51
                //send an email
52
                Mail::send('Emails/newsletter', [], function ($m) {
53
54
                    $m->from('[email protected]', 'Florent Boyer');
55
                    $m->to('[email protected]', 'Boyer Julien')
56
                        ->subject('Welcome to newsletter');
57
                });
58
            }
59
        }
60
    }
61
}
62