FileFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 22.86 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 16
loc 70
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createFromUrlsArray() 16 16 3
A createFromUrl() 0 15 2
A validateData() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}