Completed
Push — master ( e8e767...e951ab )
by Kirill
02:50
created

WebHook::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
namespace Gitter;
9
10
use Gitter\Resources\AbstractResource;
11
12
/**
13
 * Class WebHook
14
 * @package Gitter
15
 */
16
class WebHook extends AbstractResource
17
{
18
    const HOOK_LEVEL_INFO   = 'info';
19
    const HOOK_LEVEL_ERROR  = 'error';
20
21
    /**
22
     * @var string
23
     */
24
    private $hookId;
25
26
    /**
27
     * @var string
28
     */
29
    private $level = self::HOOK_LEVEL_INFO;
30
31
    /**
32
     * @var string
33
     */
34
    private $icon = null;
35
36
    /**
37
     * WebHook constructor.
38
     * @param Client $client
39
     * @param string $hookId
40
     */
41
    public function __construct(Client $client, string $hookId)
42
    {
43
        parent::__construct($client);
44
45
        $this->hookId = $hookId;
46
    }
47
48
    /**
49
     * @return $this|WebHook
50
     */
51
    public function error(): WebHook
52
    {
53
        $this->level = static::HOOK_LEVEL_ERROR;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return $this|WebHook
60
     */
61
    public function info(): WebHook
62
    {
63
        $this->level = static::HOOK_LEVEL_INFO;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $type
70
     * @return $this|WebHook
71
     */
72
    public function icon(string $type): WebHook
73
    {
74
        $this->icon = $type;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $message
81
     * @return mixed
82
     */
83
    public function send(string $message)
84
    {
85
        return $this->fetch($this->buildRoute($message));
86
    }
87
88
    /**
89
     * @param string $message
90
     * @return Route
91
     */
92
    private function buildRoute(string $message): Route
93
    {
94
        $icon = $this->level === static::HOOK_LEVEL_ERROR ? 'error' : $this->level;
95
96
        $route = Route::post($this->hookId)
97
            ->toWebhook()
98
            ->withBody('message', $message)
99
            ->withBody('errorLevel', $this->level);
100
101
        if ($this->icon !== null) {
102
            $route->withBody('icon', $icon);
103
        }
104
105
        return $route;
106
    }
107
}