Passed
Branch etl-core (c0e492)
by Jean Paul
01:52
created

SlackCommunicator::send()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 25
rs 8.0555
cc 9
nc 5
nop 0
1
<?php
2
3
namespace Coco\SourceWatcher\Watcher\Communicator;
4
5
use Exception;
6
7
class SlackCommunicator implements Communicator
8
{
9
    protected string $webHookUrl;
10
    protected string $method;
11
    protected string $contentType;
12
    protected string $data;
13
14
    public function __construct ()
15
    {
16
        $this->method = "POST";
17
        $this->contentType = "Content-Type: application/json";
18
    }
19
20
    /**
21
     * @return string
22
     */
23
    public function getWebHookUrl () : string
24
    {
25
        return $this->webHookUrl;
26
    }
27
28
    /**
29
     * @param string $webHookUrl
30
     */
31
    public function setWebHookUrl ( string $webHookUrl ) : void
32
    {
33
        $this->webHookUrl = $webHookUrl;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getMethod () : string
40
    {
41
        return $this->method;
42
    }
43
44
    /**
45
     * @param string $method
46
     */
47
    public function setMethod ( string $method ) : void
48
    {
49
        $this->method = $method;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getContentType () : string
56
    {
57
        return $this->contentType;
58
    }
59
60
    /**
61
     * @param string $contentType
62
     */
63
    public function setContentType ( string $contentType ) : void
64
    {
65
        $this->contentType = $contentType;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getData () : string
72
    {
73
        return $this->data;
74
    }
75
76
    /**
77
     * @param string $data
78
     */
79
    public function setData ( string $data ) : void
80
    {
81
        $this->data = $data;
82
    }
83
84
    /**
85
     * @return bool|string
86
     * @throws Exception
87
     */
88
    public function send ()
89
    {
90
        if ( $this->webHookUrl == null || $this->webHookUrl == "" ) {
91
            throw new Exception( "A web hook url must be defined first." );
92
        }
93
94
        if ( $this->method == null || $this->method == "" ) {
95
            throw new Exception( "A method must be defined first." );
96
        }
97
98
        if ( $this->contentType == null || $this->contentType == "" ) {
99
            throw new Exception( "A content type be defined first." );
100
        }
101
102
        if ( $this->data == null || $this->data == "" ) {
103
            throw new Exception( "Data must be defined first." );
104
        }
105
106
        $ch = curl_init( $this->webHookUrl );
107
        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

107
        curl_setopt( /** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, $this->method );
Loading history...
108
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $this->contentType, 'Content-Length: ' . strlen( $this->data ) ) );
109
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->data );
110
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
111
112
        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

112
        return curl_exec( /** @scrutinizer ignore-type */ $ch );
Loading history...
113
    }
114
}
115