1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crummy\Phlack; |
4
|
|
|
|
5
|
|
|
use Crummy\Phlack\Bridge\Guzzle\PhlackClient; |
6
|
|
|
use Crummy\Phlack\Bridge\Guzzle\Response\MessageResponse; |
7
|
|
|
use Crummy\Phlack\Common\Exception\UnexpectedTypeException; |
8
|
|
|
use Guzzle\Common\Collection; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
class Phlack extends Collection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Phlack Constructor. |
15
|
|
|
* |
16
|
|
|
* @param mixed $client |
17
|
|
|
* |
18
|
|
|
* @throws UnexpectedTypeException |
19
|
|
|
*/ |
20
|
15 |
|
public function __construct($client) |
21
|
|
|
{ |
22
|
15 |
|
if (is_string($client) || is_array($client)) { |
23
|
2 |
|
$client = new PhlackClient($client); |
|
|
|
|
24
|
15 |
|
} elseif (!$client instanceof PhlackClient) { |
25
|
1 |
|
throw new UnexpectedTypeException($client, ['string', 'array', 'Crummy\Phlack\Bridge\Guzzle\PhlackClient']); |
26
|
|
|
} |
27
|
|
|
|
28
|
14 |
|
parent::__construct([ |
29
|
14 |
|
'client' => $client, |
30
|
14 |
|
'builders' => [], |
31
|
|
|
'commands' => [ |
32
|
14 |
|
'send' => 'Send', |
33
|
14 |
|
], |
34
|
14 |
|
]); |
35
|
14 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Phlack Factory. |
39
|
|
|
* |
40
|
|
|
* @param array|string $config |
41
|
|
|
* |
42
|
|
|
* @return Phlack |
43
|
|
|
*/ |
44
|
2 |
|
public static function factory($config = []) |
45
|
|
|
{ |
46
|
2 |
|
return new self(new PhlackClient($config)); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Phlack |
51
|
|
|
*/ |
52
|
1 |
|
public static function fromConfig(array $config = [], array $defaults = [], array $required = []) |
53
|
|
|
{ |
54
|
1 |
|
return new self(new PhlackClient($config)); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string|array|JsonSerializable $message |
59
|
|
|
* |
60
|
|
|
* @throws UnexpectedTypeException |
61
|
|
|
* |
62
|
|
|
* @return MessageResponse |
63
|
|
|
*/ |
64
|
5 |
|
public function send($message) |
65
|
|
|
{ |
66
|
5 |
|
return $this['client']->getCommand($this['commands']['send'], $this->toParameters($message))->execute(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Bridge\Guzzle\PhlackClient |
71
|
|
|
*/ |
72
|
6 |
|
public function getClient() |
73
|
|
|
{ |
74
|
6 |
|
return $this['client']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Builder\MessageBuilder |
79
|
|
|
*/ |
80
|
2 |
|
public function getMessageBuilder() |
81
|
|
|
{ |
82
|
2 |
|
if (!isset($this['builders']['message'])) { |
83
|
2 |
|
$this->setPath('builders/message', new Builder\MessageBuilder()); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
2 |
|
return $this['builders']['message']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Builder\AttachmentBuilder |
91
|
|
|
*/ |
92
|
1 |
|
public function getAttachmentBuilder() |
93
|
|
|
{ |
94
|
1 |
|
if (!isset($this['builders']['attachment'])) { |
95
|
1 |
|
$this->setPath('builders/attachment', new Builder\AttachmentBuilder()); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
return $this['builders']['attachment']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $message |
103
|
|
|
* |
104
|
|
|
* @throws UnexpectedTypeException |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
5 |
|
private function toParameters($message) |
109
|
|
|
{ |
110
|
5 |
|
if (is_string($message)) { |
111
|
1 |
|
return $this->getMessageBuilder()->setText($message)->create()->jsonSerialize(); |
112
|
4 |
|
} elseif (is_array($message)) { |
113
|
1 |
|
return $message; |
114
|
3 |
|
} elseif ($message instanceof JsonSerializable) { |
115
|
2 |
|
return $message->jsonSerialize(); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
throw new UnexpectedTypeException($message, ['string', 'array', 'JsonSerializable']); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.