Completed
Push — master ( bb131b...ea520a )
by Bukashk0zzz
03:03
created

JWTRequest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 11.57 %

Importance

Changes 0
Metric Value
dl 14
loc 121
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sendFile() 0 12 1
A createClient() 0 10 1
A delete() 0 3 1
A put() 6 6 1
A __construct() 0 4 1
A buildURL() 0 8 3
A post() 6 6 1
A get() 0 3 1

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 declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Service;
4
5
use AtlassianConnectBundle\Entity\TenantInterface;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Handler\CurlHandler;
8
use GuzzleHttp\HandlerStack;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
/**
12
 * Class JWTRequest
13
 */
14
class JWTRequest
15
{
16
    /**
17
     * @var TenantInterface
18
     */
19
    private $tenant;
20
21
    /**
22
     * @var Client
23
     */
24
    private $client;
25
26
    /**
27
     * JWTRequest constructor.
28
     *
29
     * @param TenantInterface $tenant
30
     */
31
    public function __construct(TenantInterface $tenant)
32
    {
33
        $this->tenant = $tenant;
34
        $this->client = $this->createClient();
35
    }
36
37
    /**
38
     * @param UploadedFile $file
39
     * @param string       $restUrl
40
     *
41
     * @return string
42
     */
43
    public function sendFile(UploadedFile $file, string $restUrl): string
44
    {
45
        $options['headers']['X-Atlassian-Token'] = 'nocheck';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
46
        $savedFile = $file->move('/tmp/', $file->getClientOriginalName());
47
48
        $options['body'] = [
49
            'file' => \fopen($savedFile->getRealPath(), 'rb'),
50
        ];
51
52
        \unlink($savedFile->getRealPath());
53
54
        return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents();
55
    }
56
57
    /**
58
     * @param string  $restUrl
59
     * @param mixed[] $json
60
     *
61
     * @return string
62
     */
63 View Code Duplication
    public function put(string $restUrl, array $json): string
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...
64
    {
65
        $options['headers']['Content-Type'] = 'application/json';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
66
        $options['json'] = $json;
67
68
        return $this->client->put($this->buildURL($restUrl), $options)->getBody()->getContents();
69
    }
70
71
    /**
72
     * @param string  $restUrl
73
     * @param mixed[] $json
74
     *
75
     * @return string
76
     */
77 View Code Duplication
    public function post(string $restUrl, array $json): string
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...
78
    {
79
        $options['headers']['Content-Type'] = 'application/json';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
80
        $options['json'] = $json;
81
82
        return $this->client->post($this->buildURL($restUrl), $options)->getBody()->getContents();
83
    }
84
85
    /**
86
     * @param string $restUrl
87
     *
88
     * @return string
89
     */
90
    public function get(string $restUrl): string
91
    {
92
        return $this->client->get($this->buildURL($restUrl))->getBody()->getContents();
93
    }
94
95
    /**
96
     * @param string $restUrl
97
     *
98
     * @return string
99
     */
100
    public function delete(string $restUrl): string
101
    {
102
        return $this->client->delete($this->buildURL($restUrl))->getBody()->getContents();
103
    }
104
105
    /**
106
     * @param string $restUrl
107
     *
108
     * @return string
109
     */
110
    private function buildURL(string $restUrl): string
111
    {
112
        // Jira return absolute self links, so its more easy to work with get with absolute urls in such cases
113
        if ((\mb_strpos($restUrl, 'http://') !== 0) && (\mb_strpos($restUrl, 'https://') !== 0)) {
114
            return $this->tenant->getBaseUrl().$restUrl;
115
        }
116
117
        return $restUrl;
118
    }
119
120
    /**
121
     * Create a HTTP client
122
     *
123
     * @return Client
124
     */
125
    private function createClient(): Client
126
    {
127
        $stack = new HandlerStack();
128
        $stack->setHandler(new CurlHandler());
129
        $stack->push(JWTMiddleware::authTokenMiddleware(
130
            $this->tenant->getAddonKey(),
131
            $this->tenant->getSharedSecret()
132
        ));
133
134
        return new Client(['handler' => $stack]);
135
    }
136
}
137