1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace agoalofalife\Reports; |
5
|
|
|
|
6
|
|
|
use agoalofalife\Reports\Contracts\NotificationReport; |
7
|
|
|
use agoalofalife\Reports\Contracts\ResourceCollectionReport; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Support\Facades\Log; |
10
|
|
|
use Illuminate\Support\Facades\Notification; |
11
|
|
|
use Illuminate\Support\Facades\Storage; |
12
|
|
|
use \agoalofalife\Reports\Models\Report as ReportBase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Report |
16
|
|
|
* @package agoalofalife\Reports |
17
|
|
|
*/ |
18
|
|
|
abstract class Report implements ResourceCollectionReport |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Status report |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $status = ReportBase::STATUS_NEW; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Flag update report and not read |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $isCompleted = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Disk for filesystem |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $disk = 'public'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get file name |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
abstract public function getFilename() : string; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get title report |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
abstract public function getTitle() : string; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get description report |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
abstract public function getDescription() : string; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
2 |
|
public function toArray() : array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
2 |
|
'name' => $this->getTitle(), |
63
|
2 |
|
'description' => $this->getDescription(), |
64
|
2 |
|
'class' => $this->getNameClass(), |
65
|
2 |
|
'path' => $this->getPathToFile(), |
66
|
2 |
|
'lastModified' => $this->getDateLastModified(), |
67
|
2 |
|
'isCompleted' => $this->isCompleted, |
68
|
2 |
|
'status' => $this->status, |
69
|
2 |
|
'notifications' => $this->getListNotifications() |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get path to file |
75
|
|
|
* @return null|string |
76
|
|
|
*/ |
77
|
2 |
|
public function getPathToFile() : ?string |
78
|
|
|
{ |
79
|
2 |
|
return Storage::disk($this->disk)->exists($this->getFileNormalize()) ? |
80
|
2 |
|
Storage::disk($this->disk)->url($this->getFileNormalize()) : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get date last modified file |
85
|
|
|
* @return null|string |
86
|
|
|
*/ |
87
|
2 |
|
public function getDateLastModified() : ?string |
88
|
|
|
{ |
89
|
2 |
|
return Storage::disk($this->disk)->exists($this->getFileNormalize()) ? |
90
|
2 |
|
date('Y-m-d H:i:s', Storage::disk($this->disk)->lastModified($this->getFileNormalize())) : null; |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* Get full class name |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public function getNameClass() : string |
97
|
|
|
{ |
98
|
2 |
|
return get_class($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Change is completed |
103
|
|
|
* @param bool $flag |
104
|
|
|
*/ |
105
|
1 |
|
public function changeCompleted(bool $flag) : void |
106
|
|
|
{ |
107
|
1 |
|
$this->isCompleted = $flag; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
1 |
|
public function getCompleted() : bool |
114
|
|
|
{ |
115
|
1 |
|
return $this->isCompleted; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* Change status state report |
119
|
|
|
* @param string $status |
120
|
|
|
*/ |
121
|
2 |
|
public function changeStatus(string $status) : void |
122
|
|
|
{ |
123
|
2 |
|
$this->status = $status; |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get Model Report |
128
|
|
|
* @return Model |
129
|
|
|
*/ |
130
|
3 |
|
public function getReportModel() : Model |
131
|
|
|
{ |
132
|
3 |
|
return ReportBase::where('class_name', get_class($this))->get()->first(); |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* Get builder two parts string : |
136
|
|
|
* Filename |
137
|
|
|
* Extension |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
3 |
|
public function getFileNormalize() : string |
141
|
|
|
{ |
142
|
3 |
|
return "{$this->getFilename()}.{$this->extension}"; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
2 |
|
public function getListNotifications() : array |
149
|
|
|
{ |
150
|
2 |
|
if ($this instanceof NotificationReport) { |
151
|
1 |
|
return $this->getNotification()->via([]); |
|
|
|
|
152
|
|
|
} |
153
|
1 |
|
return []; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Send notification |
158
|
|
|
*/ |
159
|
2 |
|
public function send() : void |
160
|
|
|
{ |
161
|
2 |
|
if ($this instanceof NotificationReport) { |
162
|
2 |
|
Notification::send($this->getNotifiable(), $this->getNotification()); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: