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

SnapshotSource   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 49
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findByHash() 0 4 1
A findWithLast() 0 4 1
A findLast() 0 6 1
A findWithLastByPK() 0 10 3
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
}