IftttWebhookService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Arimolzer\IftttWebhook;
4
5
use Arimolzer\IftttWebhook\Exceptions\IftttWebhookUndefinedKey;
6
use Arimolzer\IftttWebhook\Exceptions\IftttWebhookException;
7
use GuzzleHttp\Client;
8
9
/**
10
 * Class IftttWebhook
11
 * @package Arimolzer\IftttWebhook
12
 */
13
class IftttWebhookService
14
{
15
    /** @var string */
16
    const IFTTT_WEBHOOK_URL = "https://maker.ifttt.com/trigger/%s/with/key/%s";
17
18
    /** @var string $key */
19
    protected $key;
20
21
    /** @var string $event */
22
    protected $event;
23
24
    /** @var string $param1 */
25
    protected $param1;
26
27
    /** @var string $param2 */
28
    protected $param2;
29
30
    /** @var string $param3 */
31
    protected $param3;
32
33
    /** @var Client */
34
    protected $client;
35
36
    /**
37
     * IftttWebhookService constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->client = new Client();
42
        $this->event = config('ifttt-webhook.credentials.default.event');
43
        $this->key = config('ifttt-webhook.credentials.default.key');
44
    }
45
46
    /**
47
     * @param string|null $param1
48
     * @param string|null $param2
49
     * @param string|null $param3
50
     * @param string|null $event
51
     * @param string|null $key
52
     * @return bool
53
     * @throws IftttWebhookException
54
     * @throws IftttWebhookUndefinedKey
55
     */
56
    public function call(
57
        string $param1 = null,
58
        string $param2 = null,
59
        string $param3 = null,
60
        string $event = null,
61
        string $key = null
62
    ) : bool {
63
        // Set the params to send to the IFTTT endpoint.
64
        $this->param1 = $param1;
65
        $this->param2 = $param2;
66
        $this->param3 = $param3;
67
68
        // Set the event and key to the provided values or default
69
        // to the config value set inside the class constructor.
70
        $this->event = $event ?? $this->event;
71
        $this->key = $key ?? $this->key;
72
73
        // Throw exceptions if no key or event values are configured or provided.
74
        if (!$this->event) {
75
            throw new IftttWebhookException();
76
        } elseif (!$this->key) {
77
            throw new IftttWebhookUndefinedKey();
78
        }
79
80
        try {
81
            // Trigger IFTTT workflow with
82
            $response = $this->client->request('POST', $this->getUrl($this->event, $this->key), [
83
                'json' => [
84
                    'value1' => $this->param1,
85
                    'value2' => $this->param2,
86
                    'value3' => $this->param3,
87
                ],
88
            ]);
89
        } catch (\GuzzleHttp\Exception\GuzzleException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\GuzzleException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
90
            throw new IftttWebhookException();
91
        }
92
93
        // If the response code is inside the HTTP status code success range return true, else false.
94
        return $response->getStatusCode() > 200 && $response->getStatusCode() < 300;
95
    }
96
97
    /**
98
     * Build the URL to post to.
99
     *
100
     * @param string $event
101
     * @param string $key
102
     * @return string
103
     */
104
    protected function getUrl(string $event, string $key) : string
105
    {
106
        return sprintf(self::IFTTT_WEBHOOK_URL, $event, $key);
107
    }
108
}
109