WebHook::send()   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
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Gitter;
11
12
use Gitter\Resources\AbstractResource;
13
14
/**
15
 * Class WebHook
16
 * @package Gitter
17
 */
18
class WebHook extends AbstractResource
19
{
20
    const HOOK_LEVEL_INFO   = 'info';
21
    const HOOK_LEVEL_ERROR  = 'error';
22
23
    /**
24
     * @var string
25
     */
26
    private $hookId;
27
28
    /**
29
     * @var string
30
     */
31
    private $level = self::HOOK_LEVEL_INFO;
32
33
    /**
34
     * @var string|null
35
     */
36
    private $icon;
37
38
    /**
39
     * WebHook constructor.
40
     * @param Client $client
41
     * @param string $hookId
42
     * @throws \InvalidArgumentException
43
     */
44
    public function __construct(Client $client, string $hookId)
45
    {
46
        parent::__construct($client);
47
48
        $this->hookId = $hookId;
49
50
        if (!$this->hookId) {
51
            throw new \InvalidArgumentException('Invalid Hook Id');
52
        }
53
    }
54
55
    /**
56
     * @param string $level
57
     * @return WebHook
58
     */
59
    public function withLevel(string $level): WebHook
60
    {
61
        $this->level = $level;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $message
68
     * @return array
69
     * @throws \Throwable
70
     * @throws \GuzzleHttp\Exception\ClientException
71
     * @throws \Exception
72
     * @throws \RuntimeException
73
     * @throws \InvalidArgumentException
74
     */
75
    public function error(string $message): array
76
    {
77
        return $this->withLevel(static::HOOK_LEVEL_ERROR)->send($message);
78
    }
79
80
    /**
81
     * @param string $message
82
     * @return array
83
     * @throws \Throwable
84
     * @throws \GuzzleHttp\Exception\ClientException
85
     * @throws \Exception
86
     * @throws \RuntimeException
87
     * @throws \InvalidArgumentException
88
     */
89
    public function info(string $message): array
90
    {
91
        return $this->withLevel(static::HOOK_LEVEL_INFO)->send($message);
92
    }
93
94
    /**
95
     * @param string $type
96
     * @return $this|WebHook
97
     */
98
    public function withIcon(string $type): WebHook
99
    {
100
        $this->icon = $type;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $message
107
     * @return array
108
     * @throws \Throwable
109
     * @throws \GuzzleHttp\Exception\ClientException
110
     * @throws \Exception
111
     * @throws \RuntimeException
112
     * @throws \InvalidArgumentException
113
     */
114
    public function send(string $message): array
115
    {
116
        return $this->fetch($this->buildRoute($message));
117
    }
118
119
    /**
120
     * @param string $message
121
     * @return Route
122
     */
123
    private function buildRoute(string $message): Route
124
    {
125
        $icon = $this->level === static::HOOK_LEVEL_ERROR ? 'error' : $this->level;
126
127
        $route = Route::post($this->hookId)
128
            ->toWebhook()
129
            ->withBody('message', $message)
130
            ->withBody('errorLevel', $this->level);
131
132
        if ($this->icon !== null) {
133
            $route->withBody('icon', $icon);
134
        }
135
136
        return $route;
137
    }
138
}
139