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

SnapshotService::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
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
use Spiral\Snapshotter\AggregationHandler\Database\Types\IncidentStatus;
12
13
class SnapshotService extends Service
14
{
15
    /** @var null|SnapshotSource */
16
    private $source = null;
17
18
    /**
19
     * AggregationService constructor.
20
     *
21
     * @param SnapshotSource $source
22
     */
23
    public function __construct(SnapshotSource $source)
24
    {
25
        $this->source = $source;
26
    }
27
28
    /**
29
     * @return SnapshotSource
30
     */
31
    public function getSource(): SnapshotSource
32
    {
33
        return $this->source;
34
    }
35
36
    /**
37
     * Select or create new Aggregation (you must save entity by yourself).
38
     *
39
     * @param string $hash
40
     * @return SnapshotRecord
41
     */
42
    public function getByHash(string $hash): SnapshotRecord
43
    {
44
        /** @var SnapshotRecord $snapshot */
45
        $snapshot = $this->source->findByHash($hash);
46
        if (empty($snapshot)) {
47
            $snapshot = $this->source->create();
48
            $snapshot->exception_hash = $hash;
49
        }
50
51
        return $snapshot;
52
    }
53
54
    /**
55
     * Count occurrence by intervals.
56
     *
57
     * @param SnapshotRecord $snapshot
58
     * @param IncidentSource $source
59
     * @return array
60
     */
61
    public function countOccurred(SnapshotRecord $snapshot, IncidentSource $source): array
62
    {
63
        $daily = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1D'));
64
        $weekly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P7D'));
65
        $monthly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1M'));
66
        $yearly = $this->countIntervalOccurred($snapshot, $source, new \DateInterval('P1Y'));
67
68
        return compact('daily', 'weekly', 'monthly', 'yearly');
69
    }
70
71
    /**
72
     * @param SnapshotRecord $aggregation
73
     * @param IncidentSource $source
74
     * @param \DateInterval  $interval
75
     * @return int
76
     */
77
    private function countIntervalOccurred(
78
        SnapshotRecord $aggregation,
79
        IncidentSource $source,
80
        \DateInterval $interval
81
    ): int
82
    {
83
        return $source->findBySnapshot($aggregation)->where(
84
            'time_created',
85
            '>=',
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...
86
            (new \DateTime('now'))->sub($interval)
87
        )->count();
88
    }
89
90
    /**
91
     * Creates unique hash to allow aggregating snapshots.
92
     *
93
     * @param SnapshotInterface $snapshot
94
     * @return string
95
     */
96
    public static function makeHash(SnapshotInterface $snapshot): string
97
    {
98
        return hash('sha256', $snapshot->getMessage());
99
    }
100
101
    /**
102
     * @param SnapshotRecord $snapshot
103
     */
104
    public function delete(SnapshotRecord $snapshot)
105
    {
106
        $snapshot->archiveLastIncident();
107
        $snapshot->forgetLastIncident();
108
109
        $incidents = $snapshot->getIncidentsHistory();
110
111
        /** @var IncidentRecord $incident */
112
        foreach ($incidents as $incident) {
113
            $incident->delete();
114
        }
115
116
        $snapshot->save();
117
    }
118
}