Werkspot /
BingAdsApiBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | 14 | public function __construct(ClientInterface $guzzleClient = null) |
|
| 24 | { |
||
| 25 | 14 | $this->guzzleClient = $guzzleClient; |
|
| 26 | 14 | $this->filesystem = new Filesystem(); |
|
| 27 | 14 | } |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @param $source |
||
| 31 | * @param null|string $destination |
||
| 32 | * |
||
| 33 | * @throws Exception |
||
| 34 | * |
||
| 35 | * @return bool|string |
||
| 36 | */ |
||
| 37 | 2 | public function getFile($source, $destination = null) |
|
| 38 | { |
||
| 39 | 2 | if (preg_match('/^((https?)\:\/\/)?([a-z0-9-.]*)\.([a-z]{2,255})(\:[0-9]{2,5})?(\/([a-z0-9+$_-]\.?)+)*\/?(\?[a-z+&$_.-][a-z0-9;:@&%=+\/$_.-]*)?(#[a-z_.-][a-z0-9+$_.-]*)?$/', $source)) { |
|
| 40 | 2 | if ($destination === null) { |
|
| 41 | 1 | throw new Exception('No file destination given.'); |
|
| 42 | } |
||
| 43 | 1 | $destination = $this->download($source, $destination); |
|
| 44 | 1 | } else { |
|
| 45 | 1 | if ($destination !== null) { |
|
| 46 | 1 | $this->filesystem->copy($source, $destination); |
|
| 47 | 1 | } else { |
|
| 48 | 1 | $destination = $source; |
|
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | 1 | if (!$this->filesystem->exists($destination)) { |
|
| 53 | 1 | return false; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | return $destination; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $url |
||
| 61 | * @param string $destination |
||
| 62 | * |
||
| 63 | * @throws CurlException |
||
| 64 | * @throws HttpStatusCodeException |
||
| 65 | * @throws \Exception |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 2 | public function download($url, $destination) |
|
| 70 | { |
||
| 71 | 2 | $this->guzzleClient->request('GET', $url, ['sink' => $destination]); |
|
| 72 | |||
| 73 | 2 | return $destination; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $file zipFile we want to open |
||
| 78 | * @param null|string $extractTo |
||
| 79 | * @param true|bool $delete |
||
| 80 | * |
||
| 81 | * @throws Exception |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 2 | public function unZip($file, $extractTo = null, $delete = true) |
|
| 86 | { |
||
| 87 | 2 | $zipDir = ($extractTo) ? $extractTo : dirname($file); |
|
| 88 | 2 | $zip = new ZipArchive(); |
|
| 89 | 2 | if ($zip->open($file) !== true) { |
|
| 90 | 1 | throw new Exception("Could not open file {$file}"); |
|
|
0 ignored issues
–
show
|
|||
| 91 | } |
||
| 92 | 1 | $files = []; |
|
| 93 | 1 | for ($i = 0; $i < $zip->numFiles; ++$i) { |
|
| 94 | 1 | $stat = $zip->statIndex($i); |
|
| 95 | 1 | $files[] = "{$zipDir}/{$stat['name']}"; |
|
|
0 ignored issues
–
show
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...
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...
|
|||
| 96 | 1 | } |
|
| 97 | 1 | $zip->extractTo($zipDir); |
|
| 98 | 1 | $zip->close(); |
|
| 99 | 1 | if ($delete) { |
|
| 100 | 1 | $this->filesystem->remove($file); |
|
| 101 | 1 | } |
|
| 102 | |||
| 103 | 1 | return $files; |
|
| 104 | } |
||
| 105 | } |
||
| 106 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.