Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 7f0776a

Browse files
author
Mario Basic
committed
Added command for spawning occurrences on a yearly basic.
1 parent 03ad166 commit 7f0776a

File tree

6 files changed

+99
-16
lines changed

6 files changed

+99
-16
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Service;
6+
use Carbon\Carbon;
7+
use Illuminate\Console\Command;
8+
use App\Occurrences\OccurrenceCreator;
9+
10+
class SpawnOccurrences extends Command
11+
{
12+
protected $occurrenceCreator;
13+
protected $services;
14+
protected $carbon;
15+
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'occurrences:spawn';
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Generate occurrences for the upcoming year based on active services.';
29+
30+
/**
31+
* Create a new command instance.
32+
*
33+
* @return void
34+
*/
35+
public function __construct(OccurrenceCreator $occurrenceCreator, Service $services, Carbon $carbon)
36+
{
37+
parent::__construct();
38+
39+
$this->occurrenceCreator = $occurrenceCreator;
40+
$this->services = $services;
41+
$this->carbon = $carbon;
42+
}
43+
44+
/**
45+
* Execute the console command.
46+
*
47+
* @return mixed
48+
*/
49+
public function handle()
50+
{
51+
foreach($this->services->all() as $service) {
52+
$date = $this->carbon->createFromDate(
53+
date('Y'),
54+
$service->month,
55+
$service->day
56+
);
57+
$this->occurrenceCreator->create($date, $service);
58+
}
59+
60+
$this->info($this->services->count() . ' occurrences have been spawned');
61+
}
62+
}

app/Console/Kernel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
1414
*/
1515
protected $commands = [
1616
// Commands\Inspire::class,
17+
\App\Console\Commands\SpawnOccurrences::class
1718
];
1819

1920
/**
@@ -24,7 +25,8 @@ class Kernel extends ConsoleKernel
2425
*/
2526
protected function schedule(Schedule $schedule)
2627
{
27-
// $schedule->command('inspire')
28-
// ->hourly();
28+
$schedule->command('occurrences:spawn')
29+
// every year at january 1st
30+
->yearly();
2931
}
3032
}

app/Listeners/CreateServiceOccurrence.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
use App\Occurrence;
66
use App\Events\ServiceWasCreated;
7+
use App\Occurrences\OccurrenceCreator;
78
use Illuminate\Queue\InteractsWithQueue;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910

1011
class CreateServiceOccurrence
1112
{
13+
protected $occurrenceCreator;
14+
1215
/**
1316
* Create the event listener.
1417
*
1518
* @return void
1619
*/
17-
public function __construct()
20+
public function __construct(OccurrenceCreator $occurrenceCreator)
1821
{
19-
//
22+
$this->occurrenceCreator = $occurrenceCreator;
2023
}
2124

2225
/**
@@ -33,16 +36,10 @@ public function handle(ServiceWasCreated $event)
3336
$event->service->day
3437
);
3538

36-
$occurence = new Occurrence;
37-
$occurence->occurs_at = $date->timestamp;
38-
$occurence->offer_sent = false;
39-
$occurence->payment_received = false;
40-
$occurence->receipt_sent = false;
41-
$occurence->service()->associate($event->service);
42-
$occurence->save();
39+
$this->occurrenceCreator->create($date, $event->service);
4340

4441
/**
45-
* TODO: Maybe do a check here if the occurence for this service
42+
* TODO: Maybe do a check here if the occurrence for this service
4643
* already exists ???
4744
*/
4845
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Occurrences;
4+
5+
use App\Service;
6+
use Carbon\Carbon;
7+
use App\Occurrence;
8+
9+
class OccurrenceCreator
10+
{
11+
public function create(Carbon $date, Service $service)
12+
{
13+
$occurrence = new Occurrence;
14+
$occurrence->occurs_at = $date->timestamp;
15+
$occurrence->offer_sent = false;
16+
$occurrence->payment_received = false;
17+
$occurrence->receipt_sent = false;
18+
$occurrence->service()->associate($service);
19+
$occurrence->save();
20+
}
21+
22+
}

app/Service.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App;
44

55
use App\Client;
6-
use App\Occurance;
6+
use App\Occurrence;
77
use Illuminate\Database\Eloquent\Model;
88

99
class Service extends Model
@@ -25,8 +25,8 @@ public function client()
2525
*
2626
* @return [type]
2727
*/
28-
public function occurances()
28+
public function occurrences()
2929
{
30-
return $this->hasMany(Occurance::class);
30+
return $this->hasMany(Occurrence::class);
3131
}
3232
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Clients have:
3131
- city
3232
- postal code
3333

34-
> At the start of every year, create new service occurances.
34+
> At the start of every year, create new service occurrences using task scheduler

0 commit comments

Comments
 (0)