Passed
Push — main ( aee526...df35b1 )
by Tan
13:15
created

WebhookService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 15
dl 0
loc 67
rs 10
c 6
b 2
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeWebhook() 0 8 3
A __construct() 0 4 1
A deleteWebHook() 0 3 1
A createDefault() 0 7 1
A setWebhook() 0 3 1
A getUpdates() 0 3 1
A getWebHookInfo() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CSlant\LaravelTelegramGitNotifier\Services;
6
7
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
8
use CSlant\TelegramGitNotifier\Interfaces\WebhookInterface;
9
use CSlant\TelegramGitNotifier\Webhook;
10
use Illuminate\Support\Facades\Config;
11
12
class WebhookService
13
{
14
    private const CONFIG_PREFIX = 'telegram-git-notifier';
15
16
    public function __construct(
17
        private WebhookInterface $webhook
18
    ) {
19
        $this->initializeWebhook();
20
    }
21
22
    public static function createDefault(): self
23
    {
24
        $webhook = new Webhook();
25
        $webhook->setToken(Config::get(self::CONFIG_PREFIX . '.bot.token'));
26
        $webhook->setUrl(Config::get(self::CONFIG_PREFIX . '.app.url'));
27
28
        return new self($webhook);
29
    }
30
31
    /**
32
     * Set webhook for telegram bot.
33
     *
34
     * @throws WebhookException
35
     */
36
    public function setWebhook(): string
37
    {
38
        return $this->webhook->setWebhook();
39
    }
40
41
    /**
42
     * Delete webhook for telegram bot.
43
     *
44
     * @throws WebhookException
45
     */
46
    public function deleteWebHook(): string
47
    {
48
        return $this->webhook->deleteWebHook();
49
    }
50
51
    /**
52
     * Get webhook update.
53
     *
54
     * @throws WebhookException
55
     */
56
    public function getUpdates(): string
57
    {
58
        return $this->webhook->getUpdates();
59
    }
60
61
    /**
62
     * Get webhook info.
63
     *
64
     * @throws WebhookException
65
     */
66
    public function getWebHookInfo(): string
67
    {
68
        return $this->webhook->getWebHookInfo();
69
    }
70
71
    private function initializeWebhook(): void
72
    {
73
        if ($this->webhook->getToken() === null) {
0 ignored issues
show
Bug introduced by
The method getToken() does not exist on CSlant\TelegramGitNotifi...rfaces\WebhookInterface. ( Ignorable by Annotation )

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

73
        if ($this->webhook->/** @scrutinizer ignore-call */ getToken() === null) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            $this->webhook->setToken(Config::get(self::CONFIG_PREFIX . '.bot.token'));
75
        }
76
77
        if ($this->webhook->getUrl() === null) {
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on CSlant\TelegramGitNotifi...rfaces\WebhookInterface. ( Ignorable by Annotation )

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

77
        if ($this->webhook->/** @scrutinizer ignore-call */ getUrl() === null) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            $this->webhook->setUrl(Config::get(self::CONFIG_PREFIX . '.app.url'));
79
        }
80
    }
81
}
82