Completed
Push — master ( 5c01aa...2b4547 )
by Hiraku
9s
created

CurlRemoteFilesystem   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 65
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getLastHeaders() 0 8 3
A __debugInfo() 0 4 1
B getContents() 0 26 6
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\Config;
10
use Composer\IO;
11
use Composer\Util;
12
13
class CurlRemoteFilesystem extends Util\RemoteFilesystem
14
{
15
    protected $io;
16
    protected $config;
17
    protected $options;
18
    protected $disableTls;
19
20
    private $req;
21
22
    /**
23
     * @inheritDoc
24
     */
25 1
    public function __construct(IO\IOInterface $io, Config $config = null, array $options = array(), $disableTls = false)
26
    {
27 1
        $this->io = $io;
28 1
        $this->config = $config;
29 1
        $this->options = $options;
30 1
        $this->disableTls = $disableTls;
31 1
        parent::__construct($io, $config, $options, $disableTls);
32 1
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 1
    public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
38
    {
39 1
        $res = null;
40 1
        if (isset($options['http']['header'])) {
41
            $headers = $options['http']['header'];
42 1
        } elseif (isset($options['https']['header'])) {
43
            $headers = $options['https']['header'];
44
        } else {
45 1
            $headers = array();
46
        }
47
        try {
48 1
            $this->req = new FetchRequest($fileUrl, $this->io, $this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
49 1
            foreach ($headers as $header) {
50
                list($key, $val) = explode(':', $header, 2);
51
                $this->req->addHeader($key, $val);
52
            }
53 1
            $res = $this->req->fetch();
54
        } catch (\Exception $e) {
55
            $this->io->writeError((string)$e);
56
        }
57 1
        if (false === $res) {
58 1
            return parent::getContents($originUrl, $fileUrl, $progress, $options);
59
        } else {
60
            return $res;
61
        }
62
    }
63
64 1
    public function getLastHeaders()
65
    {
66 1
        if ($this->req && ($headers = $this->req->getLastHeaders())) {
67
            return $headers;
68
        } else {
69 1
            return parent::getLastHeaders();
70
        }
71
    }
72
73 1
    public function __debugInfo()
74
    {
75 1
        return array();
76
    }
77
}
78