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