1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Support\Services\BearyChat; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use ElfSundae\BearyChat\Message; |
7
|
|
|
use Illuminate\Queue\SerializesModels; |
8
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
9
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
10
|
|
|
|
11
|
|
|
class SendBearyChatJob implements ShouldQueue |
12
|
|
|
{ |
13
|
|
|
use InteractsWithQueue, Queueable, SerializesModels; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The BearyChat client. |
17
|
|
|
* |
18
|
|
|
* @var \ElfSundae\BearyChat\Client |
19
|
|
|
*/ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Message instance to be sent. |
24
|
|
|
* |
25
|
|
|
* @var \ElfSundae\BearyChat\Message |
26
|
|
|
*/ |
27
|
|
|
protected $message; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new job instance. |
31
|
|
|
* |
32
|
|
|
* @param mixed $message A Message instance, or parameters which can be handled |
33
|
|
|
* by the `send` method of a Message instance. |
34
|
|
|
*/ |
35
|
|
|
public function __construct($message = null) |
36
|
|
|
{ |
37
|
|
|
if ($message instanceof Message) { |
38
|
|
|
$this->message = $message; |
39
|
|
|
} elseif (is_string($message)) { |
40
|
|
|
$this->text($message); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if (func_num_args() > 1) { |
43
|
|
|
if (is_bool(func_get_arg(1))) { |
44
|
|
|
$this->markdown(func_get_arg(1)); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (func_num_args() > 2) { |
47
|
|
|
$this->notification(func_get_arg(2)); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
call_user_func_array([$this, 'add'], array_slice(func_get_args(), 1)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the job. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function handle() |
62
|
|
|
{ |
63
|
|
|
if ($this->client) { |
64
|
|
|
$this->client->sendMessage($this->message); |
65
|
|
|
} elseif ($this->message) { |
66
|
|
|
$this->message->send(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the client with client name. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function client($name) |
77
|
|
|
{ |
78
|
|
|
$this->client = bearychat($name); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Any unhandled methods will be sent to the Message instance. |
85
|
|
|
* |
86
|
|
|
* @param string $method |
87
|
|
|
* @param array $parameters |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function __call($method, $parameters) |
91
|
|
|
{ |
92
|
|
|
if (! $this->message) { |
93
|
|
|
$this->message = new Message($this->client ?: bearychat()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
call_user_func_array([$this->message, $method], $parameters); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: