1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Snapshotter\AggregationHandler\Database\Sources; |
4
|
|
|
|
5
|
|
|
use Spiral\Database\Injections\Parameter; |
6
|
|
|
use Spiral\Debug\SnapshotInterface; |
7
|
|
|
use Spiral\ORM\Entities\RecordSelector; |
8
|
|
|
use Spiral\ORM\Entities\RecordSource; |
9
|
|
|
use Spiral\Snapshotter\AggregationHandler\Database\IncidentRecord; |
10
|
|
|
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord; |
11
|
|
|
use Spiral\Snapshotter\AggregationHandler\Database\Types\IncidentStatus; |
12
|
|
|
use Spiral\Snapshotter\AggregationHandler\Services\SnapshotService; |
13
|
|
|
|
14
|
|
|
class IncidentSource extends RecordSource |
15
|
|
|
{ |
16
|
|
|
const RECORD = IncidentRecord::class; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param SnapshotRecord|null $snapshot |
20
|
|
|
* @return RecordSelector |
21
|
|
|
*/ |
22
|
|
|
public function findBySnapshotWithSource(SnapshotRecord $snapshot = null): RecordSelector |
23
|
|
|
{ |
24
|
|
|
$where = [ |
25
|
|
|
'status' => [ |
26
|
|
|
'IN' => new Parameter([IncidentStatus::LAST, IncidentStatus::STORED]) |
27
|
|
|
] |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
if (!empty($snapshot)) { |
31
|
|
|
return $this->findBySnapshot($snapshot)->where($where); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->find($where); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SnapshotRecord $snapshotRecord |
39
|
|
|
* @return RecordSelector |
40
|
|
|
*/ |
41
|
|
|
public function findBySnapshot(SnapshotRecord $snapshotRecord): RecordSelector |
42
|
|
|
{ |
43
|
|
|
$where = [ |
44
|
|
|
'exception_hash' => $snapshotRecord->exception_hash |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
return $this->find($where); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param SnapshotRecord $snapshotRecord |
52
|
|
|
* @return RecordSelector |
53
|
|
|
*/ |
54
|
|
|
public function findSnapshotHistory(SnapshotRecord $snapshotRecord): RecordSelector |
55
|
|
|
{ |
56
|
|
|
$where = [ |
57
|
|
|
'exception_hash' => $snapshotRecord->exception_hash, |
58
|
|
|
'status' => [ |
59
|
|
|
'IN' => new Parameter([ |
60
|
|
|
IncidentStatus::STORED, |
61
|
|
|
IncidentStatus::SUPPRESSED |
62
|
|
|
]) |
63
|
|
|
] |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
return $this->find($where); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param SnapshotRecord $snapshotRecord |
71
|
|
|
* @param string|int $id |
72
|
|
|
* @param array $load |
73
|
|
|
* @return null|IncidentRecord |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function findStoredBySnapshotByPK(SnapshotRecord $snapshotRecord, $id, array $load = []) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
/** @var IncidentRecord $incident */ |
78
|
|
|
$incident = $this->findByPK($id, $load); |
79
|
|
|
if ( |
80
|
|
|
empty($incident) || |
81
|
|
|
!$incident->status->isStored() || |
82
|
|
|
$incident->getExceptionHash() !== $snapshotRecord->exception_hash |
83
|
|
|
) { |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $incident; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SnapshotRecord $snapshotRecord |
92
|
|
|
* @param string|int $id |
93
|
|
|
* @param array $load |
94
|
|
|
* @return null|IncidentRecord |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function findBySnapshotByPK(SnapshotRecord $snapshotRecord, $id, array $load = []) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
/** @var IncidentRecord $incident */ |
99
|
|
|
$incident = $this->findByPK($id, $load); |
100
|
|
|
if ( |
101
|
|
|
empty($incident) || |
102
|
|
|
$incident->getExceptionHash() !== $snapshotRecord->exception_hash |
103
|
|
|
) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $incident; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param SnapshotInterface $snapshot |
112
|
|
|
* @return IncidentRecord |
113
|
|
|
*/ |
114
|
|
|
public function createFromSnapshot(SnapshotInterface $snapshot): IncidentRecord |
115
|
|
|
{ |
116
|
|
|
$exception = $snapshot->getException(); |
117
|
|
|
$fields = [ |
118
|
|
|
'exception_hash' => SnapshotService::makeHash($snapshot), |
119
|
|
|
'exception_teaser' => $snapshot->getMessage(), |
120
|
|
|
'exception_classname' => get_class($exception), |
121
|
|
|
'exception_message' => $exception->getMessage(), |
122
|
|
|
'exception_line' => $exception->getLine(), |
123
|
|
|
'exception_file' => $exception->getFile(), |
124
|
|
|
'exception_code' => $exception->getCode(), |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
/** @var IncidentRecord $incident */ |
128
|
|
|
$incident = $this->create($fields); |
129
|
|
|
$incident->setExceptionSource($snapshot->render()); |
130
|
|
|
|
131
|
|
|
return $incident; |
132
|
|
|
} |
133
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.