Passed
Push — 2.x ( 57e8e7...97fc32 )
by Terry
02:21
created

MessengerTrait::setMessengers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 34
rs 9.6333
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Messenger;
14
15
/*
16
 * Messenger Trait is loaded in Firewall instance only.
17
 */
18
trait MessengerTrait
19
{
20
    /**
21
     * Fetch value from configuration.
22
     *
23
     * @param string $option
24
     * @param string $section
25
     *
26
     * @return mixed
27
     */
28
    abstract function getOption(string $option, string $section = '');
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
30
    /**
31
     * Set the messenger modules.
32
     *
33
     * @return void
34
     */
35
    protected function setMessengers(): void
36
    {
37
        $messengerList = [
38
            'telegram',
39
            'line_notify',
40
            'sendgrid',
41
            'native_php_mail',
42
            'smtp',
43
            'mailgun',
44
            'rocket_chat',
45
            'slack',
46
            'slack_webhook',
47
        ];
48
49
        foreach ($messengerList as $messenger) {
50
            $setting = $this->getOption($messenger, 'messengers');
51
52
            if (is_array($setting)) {
53
54
                // Initialize messenger instances from the factory/
55
                if (MessengerFactory::check($messenger, $setting)) {
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Messenger\MessengerFactory 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...
56
    
57
                    $this->kernel->add(
58
                        MessengerFactory::getInstance(
59
                            // The ID of the messenger module in the configuration.
60
                            $messenger, 
61
                            // The settings of the messenger module in the configuration.
62
                            $setting    
63
                        )
64
                    );
65
                }
66
            }
67
68
            unset($setting);
69
        }
70
    }
71
72
    /**
73
     * Set messenger events.
74
     *
75
     * @return void
76
     */
77
    protected function setMessengerEvents(): void
78
    {
79
        $setting = $this->getOption('failed_attempts_in_a_row', 'events');
80
81
        $notifyDataCircle = $setting['data_circle']['messenger'] ?: false;
82
        $notifySystemFirewall = $setting['system_firewall']['messenger'] ?: false;
83
84
        $this->kernel->setProperty('deny_attempt_notify', [
85
            'data_circle' => $notifyDataCircle,
86
            'system_firewall' => $notifySystemFirewall,
87
        ]);
88
    }
89
}
90