Passed
Push — master ( 0e87cd...c4ac3e )
by Carlos C
12:45 queued 01:03
created

Upgrader::destinationPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Origins;
6
7
use Psr\Log\LoggerInterface;
8
9
// use function PhpCfdi\SatCatalogosPopulate\Utils\file_extension;
10
11
class Upgrader
12
{
13
    final public const DEFAULT_ORIGINS_FILENAME = 'origins.xml';
14
15 4
    public function __construct(
16
        private readonly ResourcesGatewayInterface $gateway,
17
        private readonly string $destinationPath,
18
        private readonly LoggerInterface $logger
19
    ) {
20
    }
21
22
    public function destinationPath(): string
23
    {
24
        return $this->destinationPath;
25
    }
26
27 1
    public function gateway(): ResourcesGatewayInterface
28
    {
29 1
        return $this->gateway;
30
    }
31
32 4
    protected function buildPath(string $filename): string
33
    {
34 4
        return $this->destinationPath . '/' . basename($filename);
35
    }
36
37 1
    protected function createReader(): OriginsIO
38
    {
39 1
        return new OriginsIO();
40
    }
41
42 1
    protected function createReviewers(): Reviewers
43
    {
44 1
        return Reviewers::createWithDefaultReviewers($this->gateway());
45
    }
46
47 1
    public function upgrade(string $filename = ''): Origins
48
    {
49 1
        if ('' === $filename) {
50 1
            $filename = $this->buildPath(self::DEFAULT_ORIGINS_FILENAME);
51
        }
52
53 1
        $reader = $this->createReader();
54 1
        $origins = $reader->readFile($filename);
55 1
        return $this->upgradeOrigins($origins);
56
    }
57
58 1
    public function upgradeOrigins(Origins $origins): Origins
59
    {
60 1
        $reviewers = $this->createReviewers();
61 1
        $reviews = $reviewers->review($origins);
62
63 1
        return $this->upgradeReviews($reviews);
64
    }
65
66 1
    public function upgradeReviews(Reviews $reviews): Origins
67
    {
68 1
        $origins = [];
69 1
        foreach ($reviews as $review) {
70 1
            $origins[] = $this->upgradeReview($review);
71
        }
72
73 1
        return new Origins($origins);
74
    }
75
76 4
    public function upgradeReview(Review $review): OriginInterface
77
    {
78 4
        $origin = $review->origin();
79 4
        $destination = $this->buildPath($origin->destinationFilename());
80 4
        if (! $review->status()->isNotUpdated()) {
81 2
            return $origin;
82
        }
83
84
        // $this->createBackup($destination);
85 2
        $this->logger->info(sprintf(
86
            'Actualizando %s desde %s en %s',
87 2
            $origin->name(),
88 2
            $origin->downloadUrl(),
89
            $destination
90
        ));
91 2
        $urlResponse = $this->gateway->get($origin->downloadUrl(), $destination);
92 2
        return $origin->withLastModified($urlResponse->lastModified());
93
    }
94
95
    /*
96
    protected function createBackup(string $currentFile)
97
    {
98
        if (! file_exists($currentFile)) {
99
            return;
100
        }
101
        $extension = file_extension($currentFile);
102
        $currentDate = (new \DateTimeImmutable())->setTimestamp(filectime($currentFile));
103
        $backupFile = sprintf(
104
            '%s-%s.%s',
105
            substr($currentFile, 0, - 1 - strlen($extension)), // current name without extension
106
            $currentDate->format('Ymd-HisO'),                  // date
107
            $extension                                         // extension
108
        );
109
        if (! file_exists($backupFile)) {
110
            $this->logger->info(sprintf('Respaldando %s en %s', $currentFile, $backupFile));
111
            copy($currentFile, $backupFile);
112
        }
113
    }
114
    */
115
}
116