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 NotificationChannels\BearyChat; |
||
4 | |||
5 | use Illuminate\Support\Str; |
||
6 | use ElfSundae\BearyChat\Client; |
||
7 | use ElfSundae\BearyChat\Message; |
||
8 | use ElfSundae\BearyChat\MessageDefaults; |
||
9 | use Illuminate\Notifications\Notification; |
||
10 | use ElfSundae\BearyChat\Laravel\ClientManager; |
||
11 | use NotificationChannels\BearyChat\Exceptions\CouldNotSendNotification; |
||
12 | |||
13 | class BearyChatChannel |
||
14 | { |
||
15 | /** |
||
16 | * The BearyChat client manager. |
||
17 | * |
||
18 | * @var \ElfSundae\BearyChat\Laravel\ClientManager |
||
19 | */ |
||
20 | protected $clientManager; |
||
21 | |||
22 | /** |
||
23 | * Create a new BearyChatChannel instance. |
||
24 | * |
||
25 | * @param \ElfSundae\BearyChat\Laravel\ClientManager $clientManager |
||
26 | */ |
||
27 | public function __construct(ClientManager $clientManager) |
||
28 | { |
||
29 | $this->clientManager = $clientManager; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Send the given notification. |
||
34 | * |
||
35 | * @param mixed $notifiable |
||
36 | * @param \Illuminate\Notifications\Notification $notification |
||
37 | * |
||
38 | * @throws \NotificationChannels\BearyChat\Exceptions\CouldNotSendNotification |
||
39 | */ |
||
40 | public function send($notifiable, Notification $notification) |
||
41 | { |
||
42 | $message = $this->getMessage($notifiable, $notification); |
||
43 | |||
44 | $client = $message->getClient(); |
||
45 | |||
46 | if ($route = $notifiable->routeNotificationFor('BearyChat')) { |
||
47 | |||
48 | // Route can be an user, a channel, a webhook endpoint, |
||
49 | // or a client name which correspond to one of the clients |
||
50 | // listed in the BearyChat configuration file. |
||
51 | |||
52 | if (Str::startsWith($route, ['@', '#'])) { |
||
53 | $message->to($route); |
||
54 | } elseif (Str::startsWith($route, ['http://', 'https://'])) { |
||
55 | $client = $client ? $client->webhook($route) : new Client($route); |
||
56 | } else { |
||
57 | $clientName = $route; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // If the message is not created from a client, or the notifiable object |
||
62 | // provides a different client to send this message, we should obtain |
||
63 | // the client via the ClientManager and apply any message defaults from |
||
64 | // this client to the message instance. |
||
65 | |||
66 | if (is_null($client) || isset($clientName)) { |
||
67 | $client = $this->clientManager->client(isset($clientName) ? $clientName : null); |
||
68 | |||
69 | $message = $this->applyMessageDefaultsFromClient($client, $message); |
||
70 | } |
||
71 | |||
72 | if (! $client->sendMessage($message)) { |
||
73 | throw CouldNotSendNotification::sendingFailed($client->getWebhook(), $message); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the message from the given notification. |
||
79 | * |
||
80 | * @param mixed $notifiable |
||
81 | * @param \Illuminate\Notifications\Notification $notification |
||
82 | * @return \ElfSundae\BearyChat\Message |
||
83 | * |
||
84 | * @throws \NotificationChannels\BearyChat\Exceptions\CouldNotSendNotification if the |
||
85 | * passed message is not an instance of \ElfSundae\BearyChat\Message |
||
86 | */ |
||
87 | protected function getMessage($notifiable, Notification $notification) |
||
88 | { |
||
89 | $message = $notification->toBearyChat($notifiable); |
||
1 ignored issue
–
show
|
|||
90 | |||
91 | if (! (is_object($message) && $message instanceof Message)) { |
||
92 | throw CouldNotSendNotification::invalidMessage($message); |
||
93 | } |
||
94 | |||
95 | return $message; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Apply message defaults from a client to a message instance. |
||
100 | * |
||
101 | * @param \ElfSundae\BearyChat\Client $client |
||
102 | * @param \ElfSundae\BearyChat\Message $message |
||
103 | * @return \ElfSundae\BearyChat\Message |
||
104 | */ |
||
105 | protected function applyMessageDefaultsFromClient(Client $client, Message $message) |
||
106 | { |
||
107 | static $globalDefaultsKeys = null; |
||
108 | |||
109 | if (is_null($globalDefaultsKeys)) { |
||
110 | $globalDefaultsKeys = [ |
||
111 | MessageDefaults::CHANNEL, |
||
112 | MessageDefaults::USER, |
||
113 | MessageDefaults::MARKDOWN, |
||
114 | MessageDefaults::NOTIFICATION, |
||
115 | ]; |
||
116 | } |
||
117 | |||
118 | foreach ($globalDefaultsKeys as $key) { |
||
119 | $method = Str::studly($key); |
||
120 | |||
121 | if (method_exists($message, $getMethod = 'get'.$method)) { |
||
122 | if (is_null($message->{$getMethod}())) { |
||
123 | $message->{$method}($client->getMessageDefaults($key)); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if (($attachmentColor = $client->getMessageDefaults(MessageDefaults::ATTACHMENT_COLOR))) { |
||
129 | $attachmentDefaults = [ |
||
130 | 'color' => $attachmentColor, |
||
131 | ]; |
||
132 | |||
133 | // First we apply attachment defaults from the client to this message instance, |
||
134 | // then reset message's attachments via its `setAttachments` method which can |
||
135 | // handle its attachment defaults. |
||
136 | |||
137 | $message->setAttachmentDefaults($attachmentDefaults + $message->getAttachmentDefaults()); |
||
138 | $message->setAttachments($message->getAttachments()); |
||
139 | } |
||
140 | |||
141 | return $message; |
||
142 | } |
||
143 | } |
||
144 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.