Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
created

src/Notifications/HealthStatus.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function __construct($item, $channel)
30
    {
31
        $this->item = $item;
32
33
        $this->channel = $channel;
34
    }
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
    public function via()
59
    {
60
        return [$this->channel];
61
    }
62
63
    /**
64
     * Magic getter.
65
     *
66
     * @param $name
67
     * @return null
68
     */
69
    public function __get($name)
70
    {
71
        if (isset($this->item->$name)) {
72
            return $this->item->$name;
73
        }
74
    }
75
76
    /**
77
     * @param $name
78
     * @param $parameters
79
     * @return mixed
80
     */
81
    public function __call($name, $parameters)
82
    {
83
        $parameters[] = $this->item;
84
85
        return call_user_func_array(
86
            [$this->getSenderInstance($name), 'send'],
87
            $parameters
88
        );
89
    }
90
91
    /**
92
     * Create Slack message.
93
     *
94
     * @param $notifiable
95
     * @return SlackMessage
96
     */
97
    public function toSlack($notifiable)
0 ignored issues
show
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...
98
    {
99
        return (new SlackMessage())
100
            ->error()
101
            ->from(
102
                config('health.notifications.from.name'),
103
                config('health.notifications.from.icon_emoji')
104
            )
105
            ->content($this->getMessage())
106
            ->attachment(function ($attachment) {
107
                $attachment
108
                    ->title($this->getActionTitle(), $this->getActionLink())
109
                    ->content($this->result->errorMessage);
0 ignored issues
show
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...
110
            });
111
    }
112
113
    /**
114
     * Create Mail message.
115
     *
116
     * @param $notifiable
117
     * @return MailMessage
118
     */
119
    public function toMail($notifiable)
120
    {
121
        return (new MailMessage())
122
            ->line($this->getMessage($notifiable))
0 ignored issues
show
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...
123
            ->from(
124
                config('health.notifications.from.address'),
125
                config('health.notifications.from.name')
126
            )
127
            ->action($this->getActionTitle(), $this->getActionLink());
128
    }
129
130
    /**
131
     * Get the action message.
132
     *
133
     * @param $item
134
     * @return \Illuminate\Config\Repository|mixed
135
     */
136
    private function getActionMessage($item)
137
    {
138
        return isset($item->errorMessage)
139
            ? $item->errorMessage
140
            : config('health.notifications.action_message');
141
    }
142
143
    /**
144
     * Get the action title.
145
     *
146
     * @return mixed
147
     */
148
    protected function getActionTitle()
149
    {
150
        return config('health.notifications.action-title');
151
    }
152
153
    /**
154
     * Get failing message.
155
     *
156
     * @return string
157
     */
158
    protected function getMessage()
159
    {
160
        $domain = Request::server('SERVER_NAME');
161
162
        return sprintf(
163
            $this->getActionMessage($this),
164
            studly_case($this->resource->name),
0 ignored issues
show
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...
165
            $domain ? " in {$domain}." : '.'
166
        );
167
    }
168
169
    /**
170
     * Get the action link.
171
     *
172
     * @return string
173
     */
174
    protected function getActionLink()
175
    {
176
        return route(config('health.routes.notification'));
177
    }
178
}
179