Passed
Push — 2.x ( d6434d...dbf03f )
by Terry
01:59
created

MessengerTrait::triggerMessengers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 12
rs 10
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\Kernel;
14
15
use Shieldon\Messenger\Messenger\MessengerInterface;
16
use RuntimeException;
17
18
/*
19
 * Messenger Trait is loaded in Kernel instance only.
20
 */
21
trait MessengerTrait
22
{
23
    /**
24
     * The ways Shieldon send a message to when someone has been blocked.
25
     * The collection of \Shieldon\Messenger\Messenger\MessengerInterface
26
     *
27
     * @var array
28
     */
29
    protected $messenger = [];
30
31
    /**
32
     * The message that will be sent to the third-party API.
33
     *
34
     * @var string
35
     */
36
    protected $msgBody = '';
37
38
    /**
39
     * Set a messenger
40
     *
41
     * @param MessengerInterfa $instance
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\Kernel\MessengerInterfa 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...
42
     *
43
     * @return void
44
     */
45
    public function setMessenger(MessengerInterface $instance): void
46
    {
47
        $class = $this->getClassName($instance);
0 ignored issues
show
Bug introduced by
It seems like getClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        /** @scrutinizer ignore-call */ 
48
        $class = $this->getClassName($instance);
Loading history...
48
        $this->messengers[$class] = $instance;
0 ignored issues
show
Bug Best Practice introduced by
The property messengers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
    }
50
51
    /**
52
     * Set the message body.
53
     *
54
     * @param string $message The message text.
55
     *
56
     * @return void
57
     */
58
    protected function setMessageBody(string $message = ''): void
59
    {
60
        $this->msgBody = $message;
61
    }
62
63
    // @codeCoverageIgnoreStart
64
65
    /**
66
     * Undocumented function
67
     *
68
     * @return void
69
     */
70
    protected function triggerMessengers(): void
71
    {
72
        if (empty($this->msgBody)) {
73
            return;
74
        }
75
76
        try {
77
            foreach ($this->messenger as $messenger) {
78
                $messenger->setTimeout(2);
79
                $messenger->send($this->msgBody);
80
            }
81
        } catch (RuntimeException $e) {
82
            // Do not throw error, becasue the third-party services might be unavailable.
83
        }
84
    }
85
86
    // @codeCoverageIgnoreEnd
87
}
88