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.
Completed
Push — master ( 055b1c...b305ce )
by Freek
02:30
created

SlashCommandResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 105
rs 10
c 2
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createForRequest() 0 6 1
A __construct() 0 4 1
A setRequest() 0 6 1
A displayResponseOnlyToUserWhoTypedCommand() 0 6 1
A displayResponseToEveryoneOnChannel() 0 6 1
A send() 0 4 1
A setText() 0 6 1
A getResponseUrl() 0 4 1
A getHttpResponse() 0 4 1
A getPayload() 0 12 1
1
<?php
2
3
namespace Spatie\SlashCommand;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
9
class SlashCommandResponse
10
{
11
    /** @var SlashCommandRequest */
12
    protected $request;
13
14
    /** @var string */
15
    protected $text;
16
17
    /** @var string */
18
    protected $responseType = 'ephemeral';
19
20
    /** @var string */
21
    protected $attachments = '';
22
23
    /** @var \GuzzleHttp\Client */
24
    protected $client;
25
26
    public static function createForRequest(Request $request): SlashCommandResponse
27
    {
28
        return app(SlashCommandResponse::class)
29
            ->setRequest($request)
30
            ->displayResponseOnlyToUserWhoTypedCommand();
31
    }
32
33
    public function __construct(Client $client)
34
    {
35
        $this->client = $client;
36
    }
37
38
    /**
39
     * @param \Illuminate\Http\Request $request
40
     * @return $this
41
     */
42
    public function setRequest(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
43
    {
44
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<Illuminate\Http\Request> is incompatible with the declared type object<Spatie\SlashCommand\SlashCommandRequest> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param string $text
51
     * @return $this
52
     */
53
    public function setText(string $text)
54
    {
55
        $this->text = $text;
56
57
        return $this;
58
    }
59
60
    public function getResponseUrl()
61
    {
62
        return $this->request->get('response_url');
63
    }
64
65
    /**
66
     * @return $this
67
     */
68
    public function displayResponseOnlyToUserWhoTypedCommand()
69
    {
70
        $this->responseType = 'ephemeral';
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function displayResponseToEveryoneOnChannel()
79
    {
80
        $this->responseType = 'in_channel';
81
82
        return $this;
83
    }
84
85
    /**
86
     * Send the response to Slack
87
     */
88
    public function send()
89
    {
90
        $this->client->post($this->getResponseUrl(), ['json' => $this->getPayload()]);
91
    }
92
93
    /*
94
     * Get the http response
95
     */
96
    public function getHttpResponse(): Response
97
    {
98
        return new Response($this->getPayload());
99
    }
100
101
    protected function getPayload(): array
102
    {
103
        return [
104
            'text' => $this->text,
105
            'link_names' => true,
106
            'unfurl_links' => true,
107
            'unfurl_media' => true,
108
            'mrkdwn' => true,
109
            'response_type' => $this->responseType,
110
            'attachments' => $this->attachments,
111
        ];
112
    }
113
}
114