Completed
Pull Request — master (#3)
by Valentin
11:14
created

SnapshotSource::findLast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Snapshotter\AggregationHandler\Database\Sources;
4
5
use Spiral\Database\Builders\SelectQuery;
6
use Spiral\ORM\Entities\RecordSelector;
7
use Spiral\ORM\Entities\RecordSource;
8
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord;
9
10
class SnapshotSource extends RecordSource
11
{
12
    const RECORD = SnapshotRecord::class;
13
14
    /**
15
     * @param string $hash
16
     * @return null|SnapshotRecord
17
     */
18
    public function findByHash(string $hash)
19
    {
20
        return $this->findOne(['exception_hash' => $hash]);
21
    }
22
23
    /**
24
     * @return RecordSelector
25
     */
26
    public function findWithLast(): RecordSelector
27
    {
28
        return $this->find()->with('last_incident', ['alias' => 'last_incident']);
29
    }
30
31
    /**
32
     * @return null|SnapshotRecord
33
     */
34
    public function findLast()
35
    {
36
        return $this->findWithLast()
37
            ->orderBy('last_incident.time_created', SelectQuery::SORT_DESC)
38
            ->findOne();
39
    }
40
41
    /**
42
     * Snapshot with last incident (should not be archived in the history).
43
     *
44
     * @param string|int $id
45
     * @param array      $load
46
     * @return null|SnapshotRecord
47
     */
48
    public function findWithLastByPK($id, array $load = [])
49
    {
50
        /** @var SnapshotRecord|null $snapshot */
51
        $snapshot = $this->findByPK($id, $load);
52
        if (empty($snapshot) || empty($snapshot->getLastIncident())) {
53
            return null;
54
        }
55
56
        return $snapshot;
57
    }
58
}