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 ( 511799...093d46 )
by Ema
02:48
created

FilesApi   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 34.86 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 54.29%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 38
loc 109
ccs 19
cts 35
cp 0.5429
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B sendFile() 0 30 4
A getFileInfo() 10 10 2
A getFile() 14 14 4
A getFileLink() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Api;
6
7
use Http\Message\MultipartStream\MultipartStreamBuilder;
8
use Pnz\MattermostClient\Exception\InvalidArgumentException;
9
use Pnz\MattermostClient\Model\File\FileInfo;
10
use Pnz\MattermostClient\Model\File\FileUploadInfo;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamInterface;
13
14
final class FilesApi extends HttpApi
15
{
16
    /**
17
     * Uploads a file that can later be attached to a post.
18
     *
19
     * @see: https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files%2Fpost
20
     *
21
     * @param string      $fileContents The file contents to send
22
     * @param string      $filename     The filename to be used
23
     * @param string      $channelId    The ID of the channel that this file will be uploaded to
24
     * @param string|null $clientId     A unique identifier for the file that will be returned in the response
25
     *
26
     * @return FileUploadInfo|ResponseInterface
27
     */
28
    public function sendFile(string $fileContents, string $filename, string $channelId, string $clientId = null)
29
    {
30
        if (empty($fileContents) || empty($filename)) {
31
            throw new InvalidArgumentException('File contents and filename can not be empty');
32
        }
33
34
        if (empty($channelId)) {
35
            throw new InvalidArgumentException('ChannelID can not be empty');
36
        }
37
38
        $headers = [];
39
        $multipartStreamBuilder = new MultipartStreamBuilder();
40
41
        // Add channelID
42
        $multipartStreamBuilder->addResource('channel_id', $channelId);
43
        // Add file contents
44
        $multipartStreamBuilder->addResource('files', substr($fileContents, 0, 100), [
45
            'filename' => $filename,
46
        ]);
47
        // Add client id
48
        $multipartStreamBuilder->addResource('client_ids', $clientId);
49
50
        $multipartStream = $multipartStreamBuilder->build();
51
        $headers['Content-Type'] = 'multipart/form-data; boundary='.$multipartStreamBuilder->getBoundary();
52
        $multipartStreamBuilder->reset();
53
54
        $response = $this->httpPostRaw(sprintf('/files', $channelId), $multipartStream, $headers);
55
56
        return $this->handleResponse($response, FileUploadInfo::class);
57
    }
58
59
    /**
60
     * Get metadata for a file.
61
     *
62
     * @param string $fileId The ID of the file info to get
63
     *
64
     * @see https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files~1%7Bfile_id%7D~1info%2Fget
65
     *
66
     * @return FileInfo|ResponseInterface
67
     */
68 9 View Code Duplication
    public function getFileInfo(string $fileId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 9
        if (empty($fileId)) {
71 1
            throw new InvalidArgumentException('FileID can not be empty');
72
        }
73
74 8
        $response = $this->httpGet(sprintf('/files/%s/info', $fileId));
75
76 8
        return $this->handleResponse($response, FileInfo::class);
77
    }
78
79
    /**
80
     * Gets a file that has been uploaded previously.
81
     *
82
     * @param string $fileId The ID of the file to get
83
     *
84
     * @return StreamInterface
85
     */
86 9 View Code Duplication
    public function getFile(string $fileId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88 9
        if (empty($fileId)) {
89 1
            throw new InvalidArgumentException('FileID can not be empty');
90
        }
91
92 8
        $response = $this->httpGet(sprintf('/files/%s', $fileId));
93
94 8
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
95 7
            $this->handleErrors($response);
96
        }
97
98 1
        return $response->getBody();
99
    }
100
101
    /**
102
     * Gets a public link for a file that can be accessed without logging into Mattermost.
103
     *
104
     * @param string $fileId The ID of the file to get a link for
105
     *
106
     * @return string
107
     */
108 9 View Code Duplication
    public function getFileLink(string $fileId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110 9
        if (empty($fileId)) {
111 1
            throw new InvalidArgumentException('FileID can not be empty');
112
        }
113
114 8
        $response = $this->httpGet(sprintf('/files/%s/link', $fileId));
115
116 8
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
117 7
            $this->handleErrors($response);
118
        }
119
120 1
        return $response->getBody()->getContents();
121
    }
122
}
123