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

IndexAction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 104
rs 10
c 2
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handleCommand() 0 8 3
A handleCallback() 0 4 1
A createDefault() 0 7 1
A shouldHandleCommand() 0 3 2
A __invoke() 0 15 3
A handleNotification() 0 7 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
6
7
use CSlant\LaravelTelegramGitNotifier\Services\CallbackService;
8
use CSlant\LaravelTelegramGitNotifier\Services\CommandService;
9
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
10
use CSlant\TelegramGitNotifier\Bot;
11
use CSlant\TelegramGitNotifier\Exceptions\BotException;
12
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
13
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
14
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
15
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
16
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
17
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
18
use CSlant\TelegramGitNotifier\Notifier;
19
use GuzzleHttp\Client;
20
use GuzzleHttp\ClientInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Telegram\Telegram as TelegramSDK;
0 ignored issues
show
Bug introduced by
The type Telegram\Telegram was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
class IndexAction
25
{
26
    public function __construct(
27
        private ClientInterface $client,
28
        private Bot $bot,
29
        private Notifier $notifier,
30
        private Request $request
31
    ) {
32
    }
33
34
    /**
35
     * Create a new instance with default dependencies.
36
     *
37
     * @throws ConfigFileException
38
     */
39
    public static function createDefault(): self
40
    {
41
        return new self(
42
            new Client(),
43
            new Bot(new TelegramSDK(config('telegram-git-notifier.bot.token'))),
44
            new Notifier(),
45
            Request::createFromGlobals()
46
        );
47
    }
48
49
    /**
50
     * Handle telegram git notifier app.
51
     *
52
     * @throws InvalidViewTemplateException
53
     * @throws MessageIsEmptyException
54
     * @throws SendNotificationException
55
     * @throws BotException
56
     * @throws CallbackException
57
     * @throws EntryNotFoundException
58
     */
59
    public function __invoke(): void
60
    {
61
        if ($this->bot->isCallback()) {
62
            $this->handleCallback();
63
64
            return;
65
        }
66
67
        if ($this->shouldHandleCommand()) {
68
            $this->handleCommand();
69
70
            return;
71
        }
72
73
        $this->handleNotification();
74
    }
75
76
    /**
77
     * Handle callback actions.
78
     *
79
     * @throws CallbackException
80
     * @throws EntryNotFoundException
81
     * @throws InvalidViewTemplateException
82
     * @throws MessageIsEmptyException
83
     */
84
    private function handleCallback(): void
85
    {
86
        $callbackAction = new CallbackService($this->bot);
0 ignored issues
show
Bug introduced by
The call to CSlant\LaravelTelegramGi...kService::__construct() has too few arguments starting with viewNamespace. ( Ignorable by Annotation )

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

86
        $callbackAction = /** @scrutinizer ignore-call */ new CallbackService($this->bot);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
87
        $callbackAction->handle();
88
    }
89
90
    /**
91
     * Handle command messages.
92
     *
93
     * @throws EntryNotFoundException
94
     * @throws MessageIsEmptyException
95
     */
96
    private function handleCommand(): void
97
    {
98
        if (!$this->bot->isMessage() || !$this->bot->isOwner()) {
99
            return;
100
        }
101
102
        $commandAction = new CommandService($this->bot);
0 ignored issues
show
Bug introduced by
The call to CSlant\LaravelTelegramGi...dService::__construct() has too few arguments starting with viewNamespace. ( Ignorable by Annotation )

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

102
        $commandAction = /** @scrutinizer ignore-call */ new CommandService($this->bot);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
103
        $commandAction->handle();
104
    }
105
106
    /**
107
     * Handle notification sending.
108
     *
109
     * @throws InvalidViewTemplateException
110
     * @throws MessageIsEmptyException
111
     * @throws SendNotificationException
112
     */
113
    private function handleNotification(): void
114
    {
115
        $notificationService = new NotificationService(
0 ignored issues
show
Bug introduced by
The call to CSlant\LaravelTelegramGi...nService::__construct() has too few arguments starting with request. ( Ignorable by Annotation )

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

115
        $notificationService = /** @scrutinizer ignore-call */ new NotificationService(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
116
            $this->notifier,
117
            $this->bot->setting
118
        );
119
        $notificationService->handle();
120
    }
121
122
    /**
123
     * Check if the current message should be handled as a command.
124
     */
125
    private function shouldHandleCommand(): bool
126
    {
127
        return $this->bot->isMessage() && $this->bot->isOwner();
128
    }
129
}
130