Completed
Push — master ( b654b1...24c64a )
by Hiraku
04:07 queued 01:25
created

CopyRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 86.84%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 2
cbo 3
dl 0
loc 94
ccs 33
cts 38
cp 0.8684
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __destruct() 0 12 4
A makeSuccess() 0 4 1
A getCurlOptions() 0 6 1
A setDestination() 0 18 3
A createDir() 0 11 3
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\IO;
10
use Composer\Config;
11
12
class CopyRequest extends BaseRequest
13
{
14
    /** @var string */
15
    private $destination;
16
17
    /** @var resource<stream<plainfile>> */
18
    private $fp;
19
20
    private $success = false;
21
22
    protected static $defaultCurlOptions = array(
23
        CURLOPT_HTTPGET => true,
24
        CURLOPT_FOLLOWLOCATION => true,
25
        CURLOPT_MAXREDIRS => 20,
26
        CURLOPT_ENCODING => '',
27
    );
28
29
    /**
30
     * @param string $url
31
     * @param string $destination
32
     * @param bool $useRedirector
33
     * @param IO\IOInterface $io
34
     * @param Config $config
35
     */
36 7
    public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config)
37
    {
38 7
        $this->setURL($url);
39 7
        $this->setDestination($destination);
40 6
        $this->setCA($config->get('capath'), $config->get('cafile'));
41 6
        $this->setupAuthentication($io, $useRedirector, $config->get('github-domains'), $config->get('gitlab-domains'));
42 6
    }
43
44 6
    public function __destruct()
45
    {
46 6
        if ($this->fp) {
47 6
            fclose($this->fp);
48
        }
49
50 6
        if (!$this->success) {
51 6
            if (file_exists($this->destination)) {
52 6
                unlink($this->destination);
53
            }
54
        }
55 6
    }
56
57 1
    public function makeSuccess()
58
    {
59 1
        $this->success = true;
60 1
    }
61
62
    /**
63
     * @return array
64
     */
65 2
    public function getCurlOptions()
66
    {
67 2
        $curlOpts = parent::getCurlOptions();
68 2
        $curlOpts[CURLOPT_FILE] = $this->fp;
69 2
        return $curlOpts;
70
    }
71
72
    /**
73
     * @param string
74
     */
75 7
    public function setDestination($destination)
76
    {
77 7
        $this->destination = $destination;
78 7
        if (is_dir($destination)) {
79 1
            throw new FetchException(
80 1
                'The file could not be written to ' . $destination . '. Directory exists.'
81
            );
82
        }
83
84 6
        $this->createDir($destination);
85
86 6
        $this->fp = fopen($destination, 'wb');
87 6
        if (!$this->fp) {
88
            throw new FetchException(
89
                'The file could not be written to ' . $destination
90
            );
91
        }
92 6
    }
93
94 6
    private function createDir($fileName)
95
    {
96 6
        $targetdir = dirname($fileName);
97 6
        if (!file_exists($targetdir)) {
98
            if (!mkdir($targetdir, 0766, true)) {
99
                throw new FetchException(
100
                    'The file could not be written to ' . $fileName
101
                );
102
            }
103
        }
104 6
    }
105
}
106