GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Plugin::sendIrcResponseLine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Phergie plugin for returning weather information for a given location
4
 *
5
 * @link https://github.com/chrismou/phergie-irc-plugin-react-weather for the canonical source repository
6
 * @copyright Copyright (c) 2016 Chris Chrisostomou (https://mou.me)
7
 * @license http://phergie.org/license New BSD License
8
 * @package Chrismou\Phergie\Plugin\Weather
9
 */
10
11
namespace Chrismou\Phergie\Plugin\Weather;
12
13
use Phergie\Irc\Bot\React\AbstractPlugin;
14
use Phergie\Irc\Bot\React\EventQueueInterface as Queue;
15
use Phergie\Irc\Plugin\React\Command\CommandEvent as Event;
16
use GuzzleHttp\Message\Response;
17
use Phergie\Plugin\Http\Request as HttpRequest;
18
use Chrismou\Phergie\Plugin\Weather\Provider\WeatherProviderInterface;
19
20
/**
21
 * Plugin class.
22
 *
23
 * @category Chrismou
24
 * @package Chrismou\Phergie\Plugin\Weather
25
 */
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())
43
    {
44 3
        $providerConfig = array();
45 3
        $provider = 'Chrismou\Phergie\Plugin\Weather\Provider\OpenWeatherMap';
46
47 3
        if (isset($config['config'])) {
48 2
            $providerConfig = (is_array($config['config'])) ? $config['config'] : array($config['config']);
49 2
        }
50
51 3
        if (isset($config['provider'])) {
52 2
            $provider = $config['provider'];
53 2
        }
54
55 3
        $this->provider = new $provider($providerConfig);
56 3
    }
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)
94
    {
95 2
        $this->sendIrcResponse($event, $queue, $this->provider->getHelpLines());
96 2
    }
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)
142
    {
143 2
        $queue->ircPrivmsg($event->getSource(), $ircResponseLine);
144 2
    }
145
146
    /**
147
     * Return an instance of a weather provider
148
     *
149
     * @return WeatherProviderInterface
150
     */
151 2
    public function getProvider()
152
    {
153 2
        return $this->provider;
154
    }
155
}
156