1 | <?php |
||
2 | |||
3 | namespace mathewparet\LaravelInvites\Commands; |
||
4 | |||
5 | use mathewparet\LaravelInvites\Facades\LaravelInvites; |
||
6 | use mathewparet\LaravelInvites\Exceptions\LaravelInvitesException; |
||
7 | |||
8 | use Illuminate\Console\Command; |
||
9 | |||
10 | class GenerateInvitations extends Command |
||
11 | { |
||
12 | /** |
||
13 | * The name and signature of the console command. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $signature = 'invites:generate {email?} |
||
18 | {--a|allow=1 : Number of times the code can be used} |
||
19 | {--c|count=1 : The number of codes to be generated} |
||
20 | {--d|days= : The number of days until expiry (preceeds hours option)} |
||
21 | {--r|hours= : Number of hours until expiry}'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'Generates invitation codes'; |
||
29 | |||
30 | /** |
||
31 | * Create a new command instance. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function __construct() |
||
36 | { |
||
37 | parent::__construct(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Execute the console command. |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function handle() |
||
46 | { |
||
47 | $email = $this->argument('email') ?: null; |
||
48 | $allow = $this->option('allow'); |
||
49 | $count = $this->option('count'); |
||
50 | $hours = (int) $this->option('hours'); |
||
51 | $days = (int) $this->option('days'); |
||
52 | |||
53 | try |
||
54 | { |
||
55 | $invite = LaravelInvites::for($email)->allow($allow); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | |||
57 | if ($days) { |
||
58 | $invite->setExpiry(now()->addDays($days)); |
||
59 | } else if ($hours) { |
||
60 | $invite->setExpiry(now()->addHours($hours)); |
||
61 | } |
||
62 | |||
63 | $invite->generate($count); |
||
64 | |||
65 | $this->info($count." invitations generated."); |
||
66 | } catch (\Exception $e) |
||
67 | { |
||
68 | $this->error($e->getMessage()); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 |