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

File::readFileLinesIntoArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Guzzle\Exceptions\CurlException;
8
use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\HttpStatusCodeException;
9
use ZipArchive;
10
11
class File
12
{
13
    /**
14
     * @var ClientInterface
15
     */
16
    private $guzzleClient;
17
18
    /**
19
     * @var Filesystem
20
     */
21
    private $filesystem;
22
23 27
    /**
24
     * @var ZipArchive
25 27
     */
26 27
    private $zipArchive;
27 27
28
    public function __construct(ClientInterface $guzzleClient = null)
29
    {
30
        $this->guzzleClient = $guzzleClient;
31
        $this->filesystem = new Filesystem();
32
        $this->zipArchive = new ZipArchive();
33
    }
34
35
    /**
36
     * @param $source
37 2
     * @param null|string $destination
38
     *
39 2
     * @throws Exception
40 2
     *
41 1
     * @return bool|string
42
     */
43 1
    public function copyFile($source, $destination = null)
44 1
    {
45 1
        if (preg_match('/^http(s?):\/\//', $source)) {
46 1
            if ($destination === null) {
47 1
                throw new Exception('No file destination given.');
48 1
            }
49
            $destination = $this->download($source, $destination);
50
        } else {
51
            if ($destination !== null) {
52 1
                $this->filesystem->copy($source, $destination);
53 1
            } else {
54
                $destination = $source;
55
            }
56 1
        }
57
58
        if (!$this->filesystem->exists($destination)) {
59
            return false;
60
        }
61
62
        return $destination;
63
    }
64
65
    public function isHealthyZipFile($file)
66
    {
67
        $zipStatus = $this->zipArchive->open($file, ZipArchive::CHECKCONS);
68
        if ($zipStatus === ZipArchive::ER_OK || $zipStatus === true) {
69 2
            $this->zipArchive->close();
70
            $status = true;
71 2
        } else {
72
            $status = false;
73 2
        }
74
75
        return $status;
76
    }
77
78
    /**
79
     * @param string $url
80
     * @param string $destination
81
     *
82
     * @throws CurlException
83
     * @throws HttpStatusCodeException
84
     * @throws \Exception
85 2
     *
86
     * @return string
87 2
     */
88 2
    public function download($url, $destination)
89 2
    {
90 1
        $this->guzzleClient->request('GET', $url, ['sink' => $destination]);
91
92 1
        return $destination;
93 1
    }
94 1
95 1
    /**
96 1
     * @param string $file zipFile we want to open
97 1
     * @param null|string $extractTo
98 1
     * @param true|bool $delete
99 1
     *
100 1
     * @throws Exception
101 1
     *
102
     * @return array
103 1
     */
104
    public function unZip($file, $extractTo = null, $delete = true)
105
    {
106
        $zipDir = ($extractTo) ? $extractTo : dirname($file);
107
108
        if ($this->zipArchive->open($file) !== true) {
109
            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...
110
        }
111
        $files = [];
112
        for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) {
113
            $stat = $this->zipArchive->statIndex($i);
114
            $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...
115
        }
116
        $this->zipArchive->extractTo($zipDir);
117
        $this->zipArchive->close();
118
        if ($delete) {
119
            $this->filesystem->remove($file);
120
        }
121
122
        return $files;
123
    }
124
125
    /**
126
     * @param string $file
127
     * @return string[]
128
     */
129
    public function readFileLinesIntoArray($file)
130
    {
131
        return file($file);
132
    }
133
134
    /**
135
     * @param string[] $lines
136
     * @param string $file
137
     */
138
    public function writeLinesToFile($lines, $file)
139
    {
140
        $fp = fopen($file, 'w');
141
        fwrite($fp, implode('', $lines));
142
        fclose($fp);
143
    }
144
}
145