Failed Conditions
Pull Request — master (#8)
by Laurens
02:20
created

File::copyFile()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 8.7624
cc 5
eloc 13
nc 7
nop 2
crap 5
1
<?php
2
namespace Werkspot\BingAdsApiBundle\Api\Helper;
3
4
use Exception;
5
use GuzzleHttp\ClientInterface;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException;
8
use Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException;
9
use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\CurlException;
10
use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\HttpStatusCodeException;
11
use ZipArchive;
12
13
class File
14
{
15
    /**
16
     * @var ClientInterface
17
     */
18
    private $guzzleClient;
19
20
    /**
21
     * @var Filesystem
22
     */
23 27
    private $filesystem;
24
25 27
    /**
26 27
     * @var ZipArchive
27 27
     */
28
    private $zipArchive;
29
30
    public function __construct(ClientInterface $guzzleClient = null)
31
    {
32
        $this->guzzleClient = $guzzleClient;
33
        $this->filesystem = new Filesystem();
34
        $this->zipArchive = new ZipArchive();
35
    }
36
37 2
    /**
38
     * @param $source
39 2
     * @param null|string $destination
40 2
     *
41 1
     * @throws NoFileDestinationException|FileNotCopiedException
42
     *
43 1
     * @return string
44 1
     */
45 1
    public function copyFile($source, $destination = null)
46 1
    {
47 1
        if (preg_match('/^http(s?):\/\//', $source)) {
48 1
            if ($destination === null) {
49
                throw new NoFileDestinationException();
50
            }
51
            $destination = $this->download($source, $destination);
52 1
        } else {
53 1
            if ($destination !== null) {
54
                $this->filesystem->copy($source, $destination);
55
            } else {
56 1
                $destination = $source;
57
            }
58
        }
59
60
        if (!$this->filesystem->exists($destination)) {
61
            throw new FileNotCopiedException();
62
        }
63
64
        return $destination;
65
    }
66
67
    /**
68
     * @param string $file
69 2
     *
70
     * @return bool
71 2
     */
72
    public function isHealthyZipFile($file)
73 2
    {
74
        $zipStatus = $this->zipArchive->open($file, ZipArchive::CHECKCONS);
75
        if ($zipStatus === ZipArchive::ER_OK || $zipStatus === true) {
76
            $this->zipArchive->close();
77
            $status = true;
78
        } else {
79
            $status = false;
80
        }
81
82
        return $status;
83
    }
84
85 2
    /**
86
     * @param string $url
87 2
     * @param string $destination
88 2
     *
89 2
     * @throws CurlException
90 1
     * @throws HttpStatusCodeException
91
     * @throws \Exception
92 1
     *
93 1
     * @return string
94 1
     */
95 1
    public function download($url, $destination)
96 1
    {
97 1
        $this->guzzleClient->request('GET', $url, ['sink' => $destination]);
98 1
99 1
        return $destination;
100 1
    }
101 1
102
    /**
103 1
     * @param string $file zipFile we want to open
104
     * @param null|string $extractTo
105
     * @param true|bool $delete
106
     *
107
     * @throws Exception
108
     *
109
     * @return array
110
     */
111
    public function unZip($file, $extractTo = null, $delete = true)
112
    {
113
        $zipDir = ($extractTo) ? $extractTo : dirname($file);
114
115
        if ($this->zipArchive->open($file) !== true) {
116
            throw new Exception("Could not open file {$file}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $file instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
117
        }
118
        $files = [];
119
        for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) {
120
            $stat = $this->zipArchive->statIndex($i);
121
            $files[] = "{$zipDir}/{$stat['name']}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $zipDir instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $stat instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
122
        }
123
        $this->zipArchive->extractTo($zipDir);
124
        $this->zipArchive->close();
125
        if ($delete) {
126
            $this->filesystem->remove($file);
127
        }
128
129
        return $files;
130
    }
131
132
    /**
133
     * @param string $file
134
     * @return string[]
135
     */
136
    public function readFileLinesIntoArray($file)
137
    {
138
        return file($file);
139
    }
140
141
    /**
142
     * @param string[] $lines
143
     * @param string $file
144
     */
145
    public function writeLinesToFile($lines, $file)
146
    {
147
        $fp = fopen($file, 'w');
148
        fwrite($fp, implode('', $lines));
149
        fclose($fp);
150
    }
151
}
152