Test Failed
Push — master ( 13dcda...44dfad )
by Antonio Carlos
04:33
created

HealthStatus::getMessage()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Health\Notifications;
4
5
use Request;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Notifications\Notification;
8
use Illuminate\Notifications\Messages\MailMessage;
9
use Illuminate\Notifications\Messages\SlackMessage;
10
11
class HealthStatus extends Notification
12
{
13
    use Queueable;
14
15
    /**
16
     * @var
17
     */
18
    private $item;
19
    /**
20
     * @var
21
     */
22
    private $channel;
23
24
    /**
25
     * Create a new notification instance.
26
     *
27
     * @param $item
28
     */
29 1
    public function __construct($item, $channel)
30
    {
31 1
        $this->item = $item;
32
33 1
        $this->channel = $channel;
34 1
    }
35
36
    /**
37
     * Get sender instance.
38
     *
39
     * @param $name
40
     * @return \Illuminate\Foundation\Application|mixed
41
     */
42
    private function getSenderInstance($name)
43
    {
44
        $name = substr($name, 2);
45
46
        return instantiate(
47
            config(
48
                'health.notifications.channels.' . strtolower($name) . '.sender'
49
            )
50
        );
51
    }
52
53
    /**
54
     * Get the notification's delivery channels.
55
     *
56
     * @return array
57
     */
58 1
    public function via()
59
    {
60 1
        return [$this->channel];
61
    }
62
63
    /**
64
     * Magic getter.
65
     *
66
     * @param $name
67
     * @return null
68
     */
69 1
    public function __get($name)
70
    {
71 1
        if (isset($this->item->$name)) {
72 1
            return $this->item->$name;
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * @param $name
80
     * @param $parameters
81
     * @return mixed
82
     */
83
    public function __call($name, $parameters)
84
    {
85
        $parameters[] = $this->item;
86
87
        return call_user_func_array(
88
            [$this->getSenderInstance($name), 'send'],
89
            $parameters
90
        );
91
    }
92
93
    /**
94
     * Create Slack message.
95
     *
96
     * @param $notifiable
97
     * @return SlackMessage
98
     */
99
    public function toSlack($notifiable)
0 ignored issues
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        return (new SlackMessage())
102
            ->error()
103
            ->from(
104
                config('health.notifications.from.name'),
105
                config('health.notifications.from.icon_emoji')
106
            )
107
            ->content($this->getMessage())
108
            ->attachment(function ($attachment) {
109
                $attachment
110
                    ->title($this->getActionTitle(), $this->getActionLink())
111
                    ->content($this->result->errorMessage);
0 ignored issues
show
Documentation introduced by
The property result does not exist on object<PragmaRX\Health\N...fications\HealthStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
            });
113
    }
114
115
    /**
116
     * Create Mail message.
117
     *
118
     * @param $notifiable
119
     * @return MailMessage
120
     */
121 1
    public function toMail($notifiable)
122
    {
123 1
        return (new MailMessage())
124 1
            ->line($this->getMessage($notifiable))
0 ignored issues
show
Unused Code introduced by
The call to HealthStatus::getMessage() has too many arguments starting with $notifiable.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125 1
            ->from(
126 1
                config('health.notifications.from.address'),
127 1
                config('health.notifications.from.name')
128
            )
129 1
            ->action($this->getActionTitle(), $this->getActionLink());
130
    }
131
132
    /**
133
     * Get the action message.
134
     *
135
     * @param $item
136
     * @return \Illuminate\Config\Repository|mixed
137
     */
138 1
    private function getActionMessage($item)
139
    {
140 1
        return isset($item->errorMessage)
141
            ? $item->errorMessage
142 1
            : config('health.notifications.action_message');
143
    }
144
145
    /**
146
     * Get the action title.
147
     *
148
     * @return mixed
149
     */
150 1
    protected function getActionTitle()
151
    {
152 1
        return config('health.notifications.action-title');
153
    }
154
155
    /**
156
     * Get failing message.
157
     *
158
     * @return string
159
     */
160 1
    protected function getMessage()
161
    {
162 1
        $domain = Request::server('SERVER_NAME');
163
164 1
        return sprintf(
165 1
            $this->getActionMessage($this),
166 1
            studly_case($this->resource->name),
0 ignored issues
show
Documentation introduced by
The property resource does not exist on object<PragmaRX\Health\N...fications\HealthStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167 1
            $domain ? " in {$domain}." : '.'
168
        );
169
    }
170
171
    /**
172
     * Get the action link.
173
     *
174
     * @return string
175
     */
176 1
    protected function getActionLink()
177
    {
178 1
        return route(config('health.routes.notification'));
179
    }
180
}
181