Downloader::shouldCleanFilesystem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace App\Domain;
4
5
use App\Filesystem\FilesystemCleaner;
6
use App\UI\Skippable;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
abstract class Downloader extends ContentsProcessor implements RootPathPartAware
10
{
11
    use Skippable;
12
13
    /** @var \App\Domain\PathPart */
14
    private $rootPathPart;
15
16
    /**
17
     * @return Downloads
18
     */
19
    abstract protected function createDownloadsCollection(): Downloads;
20
21
    /**
22
     * @param \App\Domain\Downloads $downloads
23
     *
24
     * @return \App\Domain\Downloads
25
     */
26
    abstract protected function filterAlreadyDownloaded(Downloads $downloads): Downloads;
27
28
    /**
29
     * @param \App\Domain\Content $content
30
     *
31
     * @return \App\Domain\Downloads
32
     */
33
    abstract protected function extractDownloads(Content $content): Downloads;
34
35
    /**
36
     * @param \App\Domain\Downloads $downloads
37
     * @param \App\Domain\Path $downloadPath
38
     *
39
     * @throws \RuntimeException
40
     */
41
    abstract protected function download(Downloads $downloads, Path $downloadPath): void;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function setRootPathPart(PathPart $rootPathPart): void
47
    {
48
        $this->rootPathPart = $rootPathPart;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     * @throws \RuntimeException
54
     * @throws \Symfony\Component\Filesystem\Exception\IOException
55
     */
56
    public function processContents(Contents $contents): void
57
    {
58
        if ($contents->isEmpty()) {
59
            return;
60
        }
61
62
        $downloaderPathPart = new PathPart($this->config['path_part'] ?? []);
63
        $downloadPath = new Path([$this->rootPathPart, $downloaderPathPart]);
64
65
        // Try to create the downloads directory... 'cause if it fails, nothing will work.
66
        (new Filesystem())->mkdir((string) $downloadPath);
67
68
        if ($this->shouldCleanFilesystem()) {
69
            (new FilesystemCleaner($this->ui))($downloadPath);
70
        }
71
72
        // Add the downloader path part and get a collection of downloads
73
        $downloads = $this->createDownloadsCollection();
74
        foreach ($contents as $content) {
75
            $content->getPath()->add($downloaderPathPart);
76
77
            foreach ($this->extractDownloads($content) as $download) {
78
                $downloads->add($download);
79
            }
80
        }
81
82
        $downloads = $this->filterAlreadyDownloaded($downloads);
83
84
        if ($this->shouldDownload($downloads, $downloadPath)) {
85
            $this->download($downloads, $downloadPath);
86
        }
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    protected function shouldCleanFilesystem(): bool
93
    {
94
        return $this->config['clean_filesystem'] ?? true;
95
    }
96
97
    /**
98
     * @param \App\Domain\Downloads $downloads
99
     * @param \App\Domain\Path $downloadPath
100
     *
101
     * @return bool
102
     */
103
    private function shouldDownload(Downloads $downloads, Path $downloadPath): bool
104
    {
105
        $this->ui->writeln(sprintf('Downloading files with <info>%s</info>... '.PHP_EOL, static::class));
106
107
        return $this->shouldProcess(
108
            $this->ui,
109
            $downloads,
110
            sprintf(
111
                '%sThe script is about to download <question> %s </question> files into <info>%s</info>. '.PHP_EOL,
112
                $this->ui->indent(),
113
                $downloads->count(),
114
                (string) $downloadPath
115
            ),
116
            'download'
117
        );
118
    }
119
}
120