This repository was archived by the owner on Jan 5, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +99
-16
lines changed Expand file tree Collapse file tree 6 files changed +99
-16
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ class Kernel extends ConsoleKernel
14
14
*/
15
15
protected $ commands = [
16
16
// Commands\Inspire::class,
17
+ \App \Console \Commands \SpawnOccurrences::class
17
18
];
18
19
19
20
/**
@@ -24,7 +25,8 @@ class Kernel extends ConsoleKernel
24
25
*/
25
26
protected function schedule (Schedule $ schedule )
26
27
{
27
- // $schedule->command('inspire')
28
- // ->hourly();
28
+ $ schedule ->command ('occurrences:spawn ' )
29
+ // every year at january 1st
30
+ ->yearly ();
29
31
}
30
32
}
Original file line number Diff line number Diff line change 4
4
5
5
use App \Occurrence ;
6
6
use App \Events \ServiceWasCreated ;
7
+ use App \Occurrences \OccurrenceCreator ;
7
8
use Illuminate \Queue \InteractsWithQueue ;
8
9
use Illuminate \Contracts \Queue \ShouldQueue ;
9
10
10
11
class CreateServiceOccurrence
11
12
{
13
+ protected $ occurrenceCreator ;
14
+
12
15
/**
13
16
* Create the event listener.
14
17
*
15
18
* @return void
16
19
*/
17
- public function __construct ()
20
+ public function __construct (OccurrenceCreator $ occurrenceCreator )
18
21
{
19
- //
22
+ $ this -> occurrenceCreator = $ occurrenceCreator ;
20
23
}
21
24
22
25
/**
@@ -33,16 +36,10 @@ public function handle(ServiceWasCreated $event)
33
36
$ event ->service ->day
34
37
);
35
38
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 );
43
40
44
41
/**
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
46
43
* already exists ???
47
44
*/
48
45
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
namespace App ;
4
4
5
5
use App \Client ;
6
- use App \Occurance ;
6
+ use App \Occurrence ;
7
7
use Illuminate \Database \Eloquent \Model ;
8
8
9
9
class Service extends Model
@@ -25,8 +25,8 @@ public function client()
25
25
*
26
26
* @return [type]
27
27
*/
28
- public function occurances ()
28
+ public function occurrences ()
29
29
{
30
- return $ this ->hasMany (Occurance ::class);
30
+ return $ this ->hasMany (Occurrence ::class);
31
31
}
32
32
}
Original file line number Diff line number Diff line change @@ -31,4 +31,4 @@ Clients have:
31
31
- city
32
32
- postal code
33
33
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
You can’t perform that action at this time.
0 commit comments