Completed
Push — master ( 3f3f0c...0c6969 )
by Luke
03:20
created

Event::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1.125
1
<?php
2
/**
3
 * PagerDuty v2 Events API Library.
4
 *
5
 * @author    Luke Waite <[email protected]>
6
 * @copyright 2017 Luke Waite
7
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
8
 *
9
 * @link      https://github.com/lukewaite/pagerduty
10
 */
11
12
namespace LukeWaite\PagerDuty;
13
14
class Event
15
{
16
    protected $event;
17
18 2
    public static function create($event = [])
19
    {
20 2
        return new static($event);
21
    }
22
23 8
    public function __construct($event = [])
24
    {
25 8
        foreach (array_keys($event) as $key) {
26 4
            $this->event[$key] = $event[$key];
27 8
        }
28
29 8
        $this->setDefaults();
30 8
    }
31
32 8
    protected function setDefaults()
33
    {
34 8
        if (!isset($this->event['payload']['severity'])) {
35 8
            $this->event['payload']['severity'] = 'critical';
36 8
        }
37
38
39 8
        if (!isset($this->event['payload']['source'])) {
40 4
            $this->event['payload']['source'] = gethostname();
41 4
        }
42 8
    }
43
44 2
    public function setRoutingKey($value)
45
    {
46 2
        return $this->setMeta('routing_key', $value);
47
    }
48
49
    public function resolve()
50
    {
51
        // TODO: Implement resolve
52
    }
53
54
    public function trigger()
55
    {
56
        // TODO: Implement trigger
57
    }
58
59
    public function acknowledge()
60
    {
61
        // TODO: Implement acknowledge
62
    }
63
64 2
    public function setDedupKey($key)
65
    {
66 2
        return $this->setMeta('dedup_key', $key);
67
    }
68
69 2
    public function setSummary($value)
70
    {
71 2
        return $this->setPayload('summary', $value);
72
    }
73
74 2
    public function setSource($value)
75
    {
76 2
        return $this->setPayload('source', $value);
77
    }
78
79 2
    public function setSeverity($value)
80
    {
81 2
        return $this->setPayload('severity', $value);
82
    }
83
84 2
    public function setTimestamp($value)
85
    {
86 2
        return $this->setPayload('timestamp', $value);
87
    }
88
89 2
    public function setComponent($value)
90
    {
91 2
        return $this->setPayload('component', $value);
92
    }
93
94 2
    public function setGroup($value)
95
    {
96 2
        return $this->setPayload('group', $value);
97
    }
98
99 2
    public function setClass($value)
100
    {
101 2
        return $this->setPayload('class', $value);
102
    }
103
104 2
    public function addCustomDetail($key, $value)
105
    {
106 2
        $this->event["payload"]["custom_details"][$key] = $value;
107
108 2
        return $this;
109
    }
110
111
    public function addLink()
112
    {
113
        // TODO: Implement addLink
114
    }
115
116
    public function addImage()
117
    {
118
        // TODO: Implement addImage
119
    }
120
121 2
    protected function setPayload($key, $value)
122
    {
123 2
        $this->event["payload"][$key] = $value;
124
125 2
        return $this;
126
    }
127
128 2
    protected function setMeta($key, $value)
129
    {
130 2
        $this->event[$key] = $value;
131
132 2
        return $this;
133
    }
134
135 8
    public function toArray()
136
    {
137 8
        return $this->event;
138
    }
139
140
    public function toJson()
141
    {
142
        return json_encode($this->event);
143
    }
144
}
145