1 | <?php |
||
26 | class Plugin extends AbstractPlugin |
||
27 | { |
||
28 | /** |
||
29 | * @var WeatherProviderInterface |
||
30 | */ |
||
31 | protected $provider; |
||
32 | |||
33 | /** |
||
34 | * Accepts plugin configuration. |
||
35 | * |
||
36 | * Supported keys: |
||
37 | * provider |
||
38 | * config |
||
39 | * |
||
40 | * @param array $config |
||
41 | */ |
||
42 | 3 | public function __construct(array $config = array()) |
|
57 | |||
58 | /** |
||
59 | * Return an array of commands and associated methods |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | 1 | public function getSubscribedEvents() |
|
64 | { |
||
65 | return array( |
||
66 | 1 | 'command.weather' => 'handleCommand', |
|
67 | 1 | 'command.weather.help' => 'handleCommandHelp', |
|
68 | 1 | ); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Handler for the weather command |
||
73 | * |
||
74 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
75 | * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
||
76 | */ |
||
77 | 2 | public function handleCommand(Event $event, Queue $queue) |
|
78 | { |
||
79 | 2 | if ($this->provider->validateParams($event->getCustomParams())) { |
|
80 | 2 | $request = $this->getApiRequest($event, $queue); |
|
81 | 2 | $this->getEventEmitter()->emit('http.request', array($request)); |
|
82 | 2 | } else { |
|
83 | 2 | $this->handleCommandhelp($event, $queue); |
|
84 | } |
||
85 | 2 | } |
|
86 | |||
87 | /** |
||
88 | * Handler for the weather help command |
||
89 | * |
||
90 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
91 | * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
||
92 | */ |
||
93 | 2 | public function handleCommandHelp(Event $event, Queue $queue) |
|
97 | |||
98 | /** |
||
99 | * Set up the API request and set the callbacks |
||
100 | * |
||
101 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
102 | * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
||
103 | * |
||
104 | * @return \Phergie\Plugin\Http\Request |
||
105 | */ |
||
106 | 2 | protected function getApiRequest(Event $event, Queue $queue) |
|
107 | { |
||
108 | 2 | $self = $this; |
|
109 | 2 | return new HttpRequest(array( |
|
110 | 2 | 'url' => $this->provider->getApiRequestUrl($event), |
|
111 | 'resolveCallback' => function (Response $response) use ($self, $event, $queue) { |
||
112 | 2 | $self->sendIrcResponse($event, $queue, $this->provider->getSuccessLines($event, $response->getBody())); |
|
113 | 2 | }, |
|
114 | 2 | 'rejectCallback' => function (Response $error) use ($self, $event, $queue) { |
|
115 | 2 | $self->sendIrcResponse($event, $queue, $this->provider->getRejectLines($event, $error->getBody())); |
|
116 | 2 | } |
|
117 | 2 | )); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Send an array of response lines back to IRC |
||
122 | * |
||
123 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
124 | * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
||
125 | * @param array $ircResponse |
||
126 | */ |
||
127 | 2 | protected function sendIrcResponse(Event $event, Queue $queue, array $ircResponse) |
|
128 | { |
||
129 | 2 | foreach ($ircResponse as $ircResponseLine) { |
|
130 | 2 | $this->sendIrcResponseLine($event, $queue, $ircResponseLine); |
|
131 | 2 | } |
|
132 | 2 | } |
|
133 | |||
134 | /** |
||
135 | * Send a single response line back to IRC |
||
136 | * |
||
137 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
138 | * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue |
||
139 | * @param string $ircResponseLine |
||
140 | */ |
||
141 | 2 | protected function sendIrcResponseLine(Event $event, Queue $queue, $ircResponseLine) |
|
145 | |||
146 | /** |
||
147 | * Return an instance of a weather provider |
||
148 | * |
||
149 | * @return WeatherProviderInterface |
||
150 | */ |
||
151 | 2 | public function getProvider() |
|
155 | } |
||
156 |