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.

GroupsHistoryCommand::handleResponse()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 15
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the slack-cli package.
5
 *
6
 * (c) Cas Leentfaar <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CL\SlackCli\Command;
13
14
use CL\Slack\Model\SimpleMessage;
15
use CL\Slack\Payload\GroupsHistoryPayload;
16
use CL\Slack\Payload\GroupsHistoryPayloadResponse;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputOption;
19
20
/**
21
 * @author Cas Leentfaar <[email protected]>
22
 */
23
class GroupsHistoryCommand extends AbstractApiCommand
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected function configure()
29
    {
30
        parent::configure();
31
32
        $this->setName('groups:history');
33
        $this->setDescription('Returns a portion of messages/events from the specified private group (see `--help`)');
34
        $this->addArgument('group-id', InputArgument::REQUIRED, 'ID of the group to fetch history for');
35
        $this->addOption('latest', 'l', InputOption::VALUE_REQUIRED, 'Latest message timestamp to include in results');
36
        $this->addOption('oldest', 'o', InputOption::VALUE_REQUIRED, 'Oldest message timestamp to include in results');
37
        $this->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Number of messages to return, between 1 and 1000.');
38
        $this->setHelp(<<<EOT
39
The <info>groups:history</info> command returns a portion of messages/events from the specified private group.
40
To read the entire history for a group, call the method with no latest or oldest arguments, and then continue paging
41
using the instructions below.
42
43
The messages array up to 100 messages between `--latest` and `--oldest`. If there were more than 100 messages between
44
those two points, then `has_more` will be true.
45
46
If a message has the same timestamp as latest or oldest it will not be included in the list. This allows a client to
47
fetch all messages in a hole in channel history, by running <info>groups.history</info> with `--latest` set to the
48
oldest message they have after the hole, and `--oldest` to the latest message they have before the hole.
49
If the response includes `has_more` then the client can make another call, using the `ts` value of the final messages as
50
the latest param to get the next page of messages.
51
52
If there are more than 100 messages between the two timestamps then the messages returned are the ones closest to latest.
53
In most cases an application will want the most recent messages and will page backward from there. If oldest is provided
54
but not latest then the messages returned are those closest to oldest, allowing you to page forward through history if
55
desired.
56
57
If the latest or oldest arguments are provided then those timestamps will also be included in the output.
58
59
Messages of type "message" are user-entered text messages sent to the group, while other types are events that happened
60
within the group. All messages have both a type and a sortable ts, but the other fields depend on the type. For a list
61
of all possible events, see the channel messages documentation: <comment>https://api.slack.com/docs/messages</comment>
62
63
If a message has been starred by the calling user, the `is_starred` property will be present and true. This property is
64
only added for starred items, so is not present in the majority of messages.
65
66
The `is_limited` boolean property is only included for free teams that have reached the free message limit. If true,
67
there are messages before the current result set, but they are beyond the message limit.
68
69
For more information about the related API method, check out the official documentation:
70
<comment>https://api.slack.com/methods/groups.history</comment>
71
EOT
72
        );
73
    }
74
75
    /**
76
     * @return GroupsHistoryPayload
77
     */
78
    protected function createPayload()
79
    {
80
        $payload = new GroupsHistoryPayload();
81
        $payload->setGroupId($this->input->getArgument('group-id'));
82
        $payload->setLatest($this->input->getOption('latest'));
83
        $payload->setOldest($this->input->getOption('oldest'));
84
        $payload->setCount($this->input->getOption('count'));
85
86
        return $payload;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @param GroupsHistoryPayloadResponse $payloadResponse
93
     */
94
    protected function handleResponse($payloadResponse)
95
    {
96
        if ($payloadResponse->isOk()) {
97
            $messages = array_map(function (SimpleMessage $message) {
98
                return $this->serializeObjectToArray($message);
99
            }, $payloadResponse->getMessages());
100
101
            $firstMessage = reset($messages);
102
            $headers      = array_keys($firstMessage);
103
104
            $this->writeOk('Successfully retrieved history');
105
            $this->renderTable($messages, $headers);
106
            if ($payloadResponse->getLatest() !== null) {
107
                $this->output->writeln(sprintf('Latest: <comment>%s</comment>', $payloadResponse->getLatest()));
108
            }
109
            if ($payloadResponse->getHasMore() !== null) {
110
                $this->output->writeln(sprintf('Has more: <comment>%s</comment>', $payloadResponse->getHasMore() ? 'yes' : 'no'));
111
            }
112
        } else {
113
            $this->writeError(sprintf('Failed to retrieve history: %s', lcfirst($payloadResponse->getErrorExplanation())));
114
        }
115
    }
116
}
117