Completed
Push — master ( 6fbb52...2e9214 )
by Carlos C
06:52
created

PhpDownloader::download()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 3
Ratio 16.67 %

Importance

Changes 0
Metric Value
dl 3
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
1
<?php
2
namespace XmlSchemaValidator\Downloader;
3
4
class PhpDownloader implements DownloaderInterface
5
{
6
    public function download($url, $timeout)
7
    {
8 View Code Duplication
        if (false === $tempname = tempnam(null, null)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
9
            throw new \RuntimeException('Cannot create a temporary file');
10
        }
11
        $ctx = stream_context_create([
12
            'http' => [
13
                'timeout' => $timeout,
14
                'ignore_errors' => false,
15
            ],
16
        ]);
17
        // if Error Control '@' is omitted then when the download fail an error occurs and cannot return the exception
18
        if (! @copy($url, $tempname, $ctx)) {
19
            unlink($tempname);
20
            throw new \RuntimeException("Download fail for url $url");
21
        }
22
        return $tempname;
23
    }
24
}
25