Completed
Pull Request — master (#166)
by Hiraku
01:40
created

CopyRequest::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 9
nc 1
nop 5
crap 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 6
    public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config)
37
    {
38 6
        $this->setURL($url);
39 6
        $this->setDestination($destination);
40 5
        $this->setCA($config->get('capath'), $config->get('cafile'));
41 5
        $this->setupAuthentication(
42 5
            $io,
43 5
            $useRedirector,
44 5
            $config->get('github-domains') ?: array(),
45 5
            $config->get('gitlab-domains') ?: array()
46
        );
47 5
    }
48
49 5
    public function __destruct()
50
    {
51 5
        if ($this->fp) {
52 5
            fclose($this->fp);
53
        }
54
55 5
        if (!$this->success) {
56 5
            if (file_exists($this->destination)) {
57 5
                unlink($this->destination);
58
            }
59
        }
60 5
    }
61
62 1
    public function makeSuccess()
63
    {
64 1
        $this->success = true;
65 1
    }
66
67
    /**
68
     * @return array
69
     */
70 2
    public function getCurlOptions()
71
    {
72 2
        $curlOpts = parent::getCurlOptions();
73 2
        $curlOpts[CURLOPT_FILE] = $this->fp;
74 2
        return $curlOpts;
75
    }
76
77
    /**
78
     * @param string
79
     */
80 6
    public function setDestination($destination)
81
    {
82 6
        $this->destination = $destination;
83 6
        if (is_dir($destination)) {
84 1
            throw new FetchException(
85 1
                'The file could not be written to ' . $destination . '. Directory exists.'
86
            );
87
        }
88
89 5
        $this->createDir($destination);
90
91 5
        $this->fp = fopen($destination, 'wb');
92 5
        if (!$this->fp) {
93
            throw new FetchException(
94
                'The file could not be written to ' . $destination
95
            );
96
        }
97 5
    }
98
99 5
    private function createDir($fileName)
100
    {
101 5
        $targetdir = dirname($fileName);
102 5
        if (!file_exists($targetdir)) {
103
            if (!mkdir($targetdir, 0775, true)) {
104
                throw new FetchException(
105
                    'The file could not be written to ' . $fileName
106
                );
107
            }
108
        }
109 5
    }
110
}
111