Completed
Pull Request — master (#44)
by Hiraku
02:30
created

GitLabRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 4
dl 39
loc 39
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processRFSOption() 6 6 2
A getCurlOpts() 5 5 1
B promptAuth() 23 23 5

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
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo\Aspects;
8
9
use Composer\IO;
10
use Composer\Config as CConfig;
11
use Composer\Composer;
12
use Composer\Downloader;
13
14
/**
15
 * Simple Container for http-get request
16
 * GitLab edition
17
 */
18 View Code Duplication
class GitLabRequest extends HttpGetRequest
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    public function processRFSOption(array $options)
21
    {
22
        if (isset($options['gitlab-token'])) {
23
            $this->query['access_token'] = $options['gitlab-token'];
24
        }
25
    }
26
27
    public function getCurlOpts()
28
    {
29
        $curlOpts = parent::getCurlOpts();
30
        return $curlOpts;
31
    }
32
33
    public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
34
    {
35
        $httpCode = $res->info['http_code'];
36
        $message = "\nCould not fetch {$this->getURL()}, enter your $this->origin credentials ";
37
        if (401 === $httpCode) {
38
            $message .= 'to access private repos';
39
        } else {
40
            $message .= 'to go over the API rate limit';
41
        }
42
        $gitlab = new Util\GitLab($io, $config, null);
43
        if ($gitlab->authorizeOAuth($this->origin)) {
44
            return true;
45
        }
46
        if ($io->isInteractive() &&
47
            $gitlab->authorizeOAuthInteractively($this->origin, $message)) {
48
            return true;
49
        }
50
51
        throw new Downloader\TransportException(
52
            "Could not authenticate against $this->origin",
53
            401
54
        );
55
    }
56
}
57