SnapshotService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spiral\Snapshotter\AggregationHandler\Services;
4
5
use Spiral\Core\Service;
6
use Spiral\Debug\SnapshotInterface;
7
use Spiral\Snapshotter\AggregationHandler\Database\IncidentRecord;
8
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord;
9
use Spiral\Snapshotter\AggregationHandler\Database\Sources\SnapshotSource;
10
use Spiral\Snapshotter\AggregationHandler\Database\Sources\IncidentSource;
11
12
class SnapshotService extends Service
13
{
14
    /** @var null|SnapshotSource */
15
    private $source = null;
16
17
    /**
18
     * AggregationService constructor.
19
     *
20
     * @param SnapshotSource $source
21
     */
22
    public function __construct(SnapshotSource $source)
23
    {
24
        $this->source = $source;
25
    }
26
27
    /**
28
     * @return SnapshotSource
29
     */
30
    public function getSource(): SnapshotSource
31
    {
32
        return $this->source;
33
    }
34
35
    /**
36
     * Select or create new Aggregation (you must save entity by yourself).
37
     *
38
     * @param string $hash
39
     * @return SnapshotRecord
40
     */
41
    public function getByHash(string $hash): SnapshotRecord
42
    {
43
        /** @var SnapshotRecord $snapshot */
44
        $snapshot = $this->source->findByHash($hash);
45
        if (empty($snapshot)) {
46
            $snapshot = $this->source->create();
47
            $snapshot->exception_hash = $hash;
48
        }
49
50
        return $snapshot;
51
    }
52
53
    /**
54
     * Count occurrence by intervals.
55
     *
56
     * @param SnapshotRecord $snapshot
57
     * @param IncidentSource $source
58
     * @return array
59
     */
60
    public function countOccurred(SnapshotRecord $snapshot, IncidentSource $source): array
61
    {
62
        $daily = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1D'));
63
        $weekly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P7D'));
64
        $monthly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1M'));
65
        $yearly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1Y'));
66
67
        return compact('daily', 'weekly', 'monthly', 'yearly');
68
    }
69
70
    /**
71
     * @param SnapshotRecord $aggregation
72
     * @param IncidentSource $source
73
     * @param \DateInterval  $interval
74
     * @return int
75
     */
76
    private function countIntervalOccurred(
77
        SnapshotRecord $aggregation,
78
        IncidentSource $source,
79
        \DateInterval $interval
80
    ): int
81
    {
82
        return $source->findBySnapshot($aggregation)->where(
83
            'time_created',
84
            '>=',
0 ignored issues
show
Unused Code introduced by
The call to RecordSelector::where() has too many arguments starting with '>='.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
            (new \DateTime('now'))->sub($interval)
86
        )->count();
87
    }
88
89
    /**
90
     * Creates unique hash to allow aggregating snapshots.
91
     *
92
     * @param SnapshotInterface $snapshot
93
     * @return string
94
     */
95
    public static function makeHash(SnapshotInterface $snapshot): string
96
    {
97
        return hash('sha256', $snapshot->getMessage());
98
    }
99
100
    /**
101
     * @param SnapshotRecord $snapshot
102
     */
103
    public function delete(SnapshotRecord $snapshot)
104
    {
105
        $snapshot->archiveLastIncident();
106
        $snapshot->forgetLastIncident();
107
108
        $incidents = $snapshot->getIncidentsHistory();
109
110
        /** @var IncidentRecord $incident */
111
        foreach ($incidents as $incident) {
112
            $incident->delete();
113
        }
114
115
        $snapshot->save();
116
    }
117
}