FileFactory::createFromUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 6
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
4
5
use Psr\Log\LoggerInterface;
6
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File;
7
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository;
8
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException;
9
use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
10
11
class FileFactory extends AbstractFactory
12
{
13
    /** @var FileRepository */
14
    private $fileRepository;
15
16
17
    public function __construct(LoggerInterface $logger, FileRepository $fileRepository)
18
    {
19
        parent::__construct($logger);
20
        $this->fileRepository = $fileRepository;
21
    }
22
23
    /**
24
     * @param string[] $urlStrings
25
     *
26
     * @return File[]
27
     */
28 View Code Duplication
    public function createFromUrlsArray(array $urlStrings): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
29
    {
30
        $files = [];
31
32
        foreach ($urlStrings as $url) {
33
            try {
34
                $file = $this->createFromUrl($url);
35
                $files[] = $file;
36
            } catch (\Exception $e) {
37
                $this->logger->error('Error while creating file from DTO', ['file' => $url, 'message' => $e->getMessage()]);
38
                continue;
39
            }
40
        }
41
42
        return $files;
43
    }
44
45
    /**
46
     * @param string $url
47
     *
48
     * @return File
49
     *
50
     * @throws InvalidResponseException
51
     */
52
    public function createFromUrl(string $url): File
53
    {
54
        $this->validateData($url);
55
56
        // Replacing HTTP with HTTPS
57
        $url = str_replace('http://', 'https://', $url);
58
59
        if (null === ($file = $this->fileRepository->findOneBy(['remoteUrl' => $url]))) {
60
            // Creating new file
61
            $file = new File($url);
62
            $this->fileRepository->add($file);
63
        }
64
65
        return $file;
66
    }
67
68
    /**
69
     * @param $data
70
     *
71
     * @throws InvalidResponseException
72
     */
73
    private function validateData($data): void
74
    {
75
        if (!is_string($data)) {
76
            // @todo Change exception
77
            throw new InvalidResponseException('File data must be a string');
78
        }
79
    }
80
}