Completed
Push — master ( 65b126...03a743 )
by Antonio Carlos
08:20 queued 05:40
created

HealthStatus::getSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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
    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
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...
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
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...
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
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...
123
            ->line($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...
124
            ->from(
125
                config('health.notifications.from.address'),
126
                config('health.notifications.from.name')
127
            )
128
            ->subject($this->getSubject())
129
            ->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
    private function getActionMessage($item)
139
    {
140
        return isset($item->errorMessage)
141
            ? $item->errorMessage
142
            : config('health.notifications.action_message');
143
    }
144
145
    /**
146
     * Get the action title.
147
     *
148
     * @return mixed
149
     */
150
    protected function getActionTitle()
151
    {
152
        return config('health.notifications.action-title');
153
    }
154
155
    /**
156
     * Get the email subject.
157
     *
158
     * @return mixed
159
     */
160
    protected function getSubject()
161
    {
162
        return config('health.notifications.subject');
163
    }
164
165
    /**
166
     * Get failing message.
167
     *
168
     * @return string
169
     */
170
    protected function getMessage()
171
    {
172
        $domain = Request::server('SERVER_NAME');
173
174
        return sprintf(
175
            $this->getActionMessage($this),
176
            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...
Deprecated Code introduced by
The function studly_case() has been deprecated with message: Str::studly() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
177
            $domain ? " in {$domain}." : '.'
178
        );
179
    }
180
181
    /**
182
     * Get the action link.
183
     *
184
     * @return string
185
     */
186
    protected function getActionLink()
187
    {
188
        return route(config('health.routes.notification'));
189
    }
190
}
191