Passed
Push — master ( 1c6332...1ba0c5 )
by Jean Paul
06:52
created

SlackCommunicator::send()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 0
dl 0
loc 25
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace Coco\SourceWatcher\Watcher\Communicator;
4
5
use Coco\SourceWatcher\Core\SourceWatcherException;
6
use Coco\SourceWatcher\Utils\i18n;
7
8
/**
9
 * Class SlackCommunicator
10
 * @package Coco\SourceWatcher\Watcher\Communicator
11
 */
12
class SlackCommunicator implements Communicator
13
{
14
    /**
15
     * @var string
16
     */
17
    protected string $webHookUrl = "";
18
19
    /**
20
     * @var string
21
     */
22
    protected string $method = "";
23
24
    /**
25
     * @var string
26
     */
27
    protected string $contentType = "";
28
29
    /**
30
     * @var string
31
     */
32
    protected string $data = "";
33
34
    /**
35
     * SlackCommunicator constructor.
36
     */
37
    public function __construct ()
38
    {
39
        $this->method = "POST";
40
        $this->contentType = "Content-Type: application/json";
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getWebHookUrl () : string
47
    {
48
        return $this->webHookUrl;
49
    }
50
51
    /**
52
     * @param string $webHookUrl
53
     */
54
    public function setWebHookUrl ( string $webHookUrl ) : void
55
    {
56
        $this->webHookUrl = $webHookUrl;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getMethod () : string
63
    {
64
        return $this->method;
65
    }
66
67
    /**
68
     * @param string $method
69
     */
70
    public function setMethod ( string $method ) : void
71
    {
72
        $this->method = $method;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getContentType () : string
79
    {
80
        return $this->contentType;
81
    }
82
83
    /**
84
     * @param string $contentType
85
     */
86
    public function setContentType ( string $contentType ) : void
87
    {
88
        $this->contentType = $contentType;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getData () : string
95
    {
96
        return $this->data;
97
    }
98
99
    /**
100
     * @param string $data
101
     */
102
    public function setData ( string $data ) : void
103
    {
104
        $this->data = $data;
105
    }
106
107
    /**
108
     * @return bool|string
109
     * @throws SourceWatcherException
110
     */
111
    public function send ()
112
    {
113
        if ( empty( $this->webHookUrl ) ) {
114
            throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Web_Hook" ) );
115
        }
116
117
        if ( empty( $this->method ) ) {
118
            throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Method" ) );
119
        }
120
121
        if ( empty( $this->contentType ) ) {
122
            throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Content_Type" ) );
123
        }
124
125
        if ( empty( $this->data ) ) {
126
            throw new SourceWatcherException( i18n::getInstance()->getText( SlackCommunicator::class, "No_Data" ) );
127
        }
128
129
        $ch = curl_init( $this->webHookUrl );
130
        curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $this->method );
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        curl_setopt( /** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, $this->method );
Loading history...
131
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $this->contentType, "Content-Length: " . strlen( $this->data ) ) );
132
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->data );
133
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
134
135
        return curl_exec( $ch );
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        return curl_exec( /** @scrutinizer ignore-type */ $ch );
Loading history...
136
    }
137
}
138