Passed
Pull Request — master (#22)
by Ronan
07:39 queued 02:00
created

EventFinder::event()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 4
crap 6
1
<?php
2
3
namespace App\Model\Finder;
4
5
use App\Model\Deployment;
6
use App\Model\Event;
7
use ClanCats\Hydrahon\Query\Expression;
8
use Ronanchilvers\Orm\Finder;
9
10
/**
11
 * Finder for deployment event models
12
 *
13
 * @author Ronan Chilvers <[email protected]>
14
 */
15
class EventFinder extends Finder
16
{
17
    const INFO  = 'info';
18
    const ERROR = 'error';
19
20
    /**
21
     * Create an event for a deployment
22
     *
23
     * @param string $type
24
     * @param \App\Model\Deployment $deployment
25
     * @param string $header
26
     * @param string $detail
27
     * @return bool|\App\Model\Event
28
     * @author Ronan Chilvers <[email protected]>
29
     */
30
    public function event(string $type, Deployment $deployment, string $header, string $detail = '')
31
    {
32
        $event = new Event;
33
        $event->deployment = $deployment;
34
        $event->type = $type;
35
        $event->header = $header;
36
        $event->detail = $detail;
37
38
        if (!$event->save()) {
39
            return false;
40
        }
41
42
        return $event;
43
    }
44
45
    /**
46
     * Get an event array for a deployment id
47
     *
48
     * @param int $deploymentId
49
     * @return array
50
     * @author Ronan Chilvers <[email protected]>
51
     */
52
    public function arrayForDeploymentId($deploymentId)
53
    {
54
        $events = $this
55
            ->select()
56
            ->where('event_deployment', $deploymentId)
57
            ->orderby('event_created')
58
            ->execute();
59
        if (empty($events)) {
60
            return [];
61
        }
62
63
        $arr = [];
64
        $header = false;
65
        $lastEvent = false;
66
        foreach ($events as $event) {
67
            if ($header !== $event->header) {
68
                if (isset($arr[$header]['times'])) {
69
                    $arr[$header]['times']['end'] = $lastEvent->created;
0 ignored issues
show
Bug introduced by
The property created does not exist on false.
Loading history...
70
                    $arr[$header]['times']['duration'] = $arr[$header]['times']['end']->diffInSeconds(
71
                        $arr[$header]['times']['start']
72
                    );
73
                }
74
                $header       = $event->header;
75
                $arr[$header]['times'] = [
76
                    'start' => $event->created,
77
                ];
78
            }
79
            if ('error' !== $arr[$header]['type']) {
80
                $arr[$header]['type'] = $event->type;
81
            }
82
            if (!isset($arr[$header]['events'])) {
83
                $arr[$header]['events'] = [];
84
            }
85
            $arr[$header]['events'][] = $event->detail . "\n";
86
            $lastEvent = $event;
87
        }
88
        $arr[$header]['times']['end'] = $lastEvent->created;
89
        $arr[$header]['times']['duration'] = $arr[$header]['times']['end']->diffInSeconds(
90
            $arr[$header]['times']['start']
91
        );
92
        // @TODO Remove var_dump
93
        // echo '<pre>' . print_r($arr, true); exit();
94
95
        return $arr;
96
    }
97
}
98