ImageDownloader   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A download() 0 13 2
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Downloader;
12
13
use Symfony\Component\Filesystem\Filesystem;
14
use Symfony\Component\HttpFoundation\File\File;
15
use Webmozart\Assert\Assert;
16
17
final class ImageDownloader implements ImageDownloaderInterface
18
{
19
    /** @var Filesystem */
20
    private $filesystem;
21
22
    public function __construct(Filesystem $filesystem)
23
    {
24
        $this->filesystem = $filesystem;
25
    }
26
27
    public function download(string $url): File
28
    {
29
        $path = rtrim(sys_get_temp_dir(), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR . md5(random_bytes(10));
30
        $pathInfo = pathinfo($url);
31
        $extension = $pathInfo['extension'] ?? null;
32
        if (null !== $extension) {
33
            $path .= '.' . $extension;
34
        }
35
        $contents = file_get_contents($url);
36
        Assert::string($contents, sprintf('Content of file in path %s is null', $path));
37
        $this->filesystem->dumpFile($path, $contents);
38
39
        return new File($path);
40
    }
41
}
42