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 |
View Code Duplication |
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
|
|
|
|
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.