Passed
Push — master ( fe617e...bc2c58 )
by Carlos C
14:43 queued 11s
created

Upgrader::upgradeOrigins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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