|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Loan; |
|
6
|
|
|
use App\Notifications\FirstReminder; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class SendReminders extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'bibrex:reminders'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Send reminders.'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new command instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Execute the console command. |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
public function handle() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->info(sprintf( |
|
44
|
|
|
'-[ %s : Send reminders start ]------------------------------------', |
|
45
|
|
|
strftime('%Y-%m-%d %H:%M:%S') |
|
46
|
|
|
)); |
|
47
|
|
|
|
|
48
|
|
|
$n = 0; |
|
49
|
|
|
foreach (Loan::with( |
|
50
|
|
|
'item', |
|
51
|
|
|
'item.thing', |
|
52
|
|
|
'library', |
|
53
|
|
|
'user', |
|
54
|
|
|
'notifications' |
|
55
|
|
|
)->get() as $loan) { |
|
56
|
|
|
$settings = $loan->getLibrarySettings(); |
|
57
|
|
|
|
|
58
|
|
|
if (!$settings->reminders) { |
|
59
|
|
|
$this->comment("[{$loan->id}] Not sending reminders for {$loan->item->thing->name()}" . |
|
60
|
|
|
" from {$loan->library->name}."); |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($loan->due_at->getTimestamp() > Carbon::now()->getTimestamp()) { |
|
65
|
|
|
$this->comment("[{$loan->id}] Loan is not due yet."); |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (count($loan->notifications) > 0) { |
|
70
|
|
|
$this->comment("[{$loan->id}] Reminder already sent"); |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (empty($loan->user->email)) { |
|
75
|
|
|
$this->error("[{$loan->id}] Cannot send reminder. No email set for user {$loan->user->id}"); |
|
76
|
|
|
\Log::error(sprintf( |
|
77
|
|
|
'Kan ikke sende påminnelse til <a href="%s">%s</a> – mangler epostadresse.', |
|
78
|
|
|
action('UsersController@getShow', $loan->user->id), |
|
79
|
|
|
"bruker #{$loan->user->id}" |
|
80
|
|
|
)); |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Send reminder |
|
85
|
|
|
$this->info("[{$loan->id}] Sending reminder to {$loan->user->email}"); |
|
86
|
|
|
$loan->user->notify(new FirstReminder($loan)); |
|
87
|
|
|
$n++; |
|
88
|
|
|
} |
|
89
|
|
|
if ($n > 0) { |
|
90
|
|
|
\Log::info("$n påminnelse(r) ble sendt ut."); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->info(sprintf( |
|
94
|
|
|
'-[ %s : Send reminders complete ]------------------------------------', |
|
95
|
|
|
strftime('%Y-%m-%d %H:%M:%S') |
|
96
|
|
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: