Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.79% |
28 / 39 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Copier | |
71.79% |
28 / 39 |
|
66.67% |
2 / 3 |
13.71 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
prepareTarget | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
copy | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * Prepares the destination by deleting any files about to be copied. |
4 | * Copies the files. |
5 | * |
6 | * TODO: Exclude files list. |
7 | * |
8 | * @author CoenJacobs |
9 | * @author BrianHenryIE |
10 | * |
11 | * @license MIT |
12 | */ |
13 | |
14 | namespace BrianHenryIE\Strauss\Pipeline; |
15 | |
16 | use BrianHenryIE\Strauss\Config\CopierConfigInterface; |
17 | use BrianHenryIE\Strauss\Files\DiscoveredFiles; |
18 | use BrianHenryIE\Strauss\Files\File; |
19 | use BrianHenryIE\Strauss\Helpers\FileSystem; |
20 | use League\Flysystem\FilesystemException; |
21 | use Psr\Log\LoggerAwareTrait; |
22 | use Psr\Log\LoggerInterface; |
23 | |
24 | class Copier |
25 | { |
26 | use LoggerAwareTrait; |
27 | |
28 | protected DiscoveredFiles $files; |
29 | |
30 | protected FileSystem $filesystem; |
31 | |
32 | protected CopierConfigInterface $config; |
33 | |
34 | /** |
35 | * Copier constructor. |
36 | * |
37 | * @param DiscoveredFiles $files Contains a collections of Files with source and target paths. |
38 | * @param CopierConfigInterface $config |
39 | * @param FileSystem $filesystem A filesystem instance. |
40 | * @param LoggerInterface $logger A logger implementation. |
41 | */ |
42 | public function __construct( |
43 | DiscoveredFiles $files, |
44 | CopierConfigInterface $config, |
45 | FileSystem $filesystem, |
46 | LoggerInterface $logger |
47 | ) { |
48 | $this->files = $files; |
49 | $this->config = $config; |
50 | $this->logger = $logger; |
51 | $this->filesystem = $filesystem; |
52 | } |
53 | |
54 | /** |
55 | * If the target dir does not exist, create it. |
56 | * If it already exists, delete any files we're about to copy. |
57 | * |
58 | * @throws FilesystemException |
59 | */ |
60 | public function prepareTarget(): void |
61 | { |
62 | if (! $this->filesystem->directoryExists($this->config->getTargetDirectory())) { |
63 | $this->logger->info('Creating directory at ' . $this->config->getTargetDirectory()); |
64 | $this->filesystem->createDirectory($this->config->getTargetDirectory()); |
65 | } |
66 | |
67 | foreach ($this->files->getFiles() as $file) { |
68 | if (!$file->isDoCopy()) { |
69 | $this->logger->debug('Skipping ' . $file->getSourcePath()); |
70 | continue; |
71 | } |
72 | |
73 | $targetAbsoluteFilepath = $file->getAbsoluteTargetPath(); |
74 | |
75 | if ($this->filesystem->fileExists($targetAbsoluteFilepath)) { |
76 | $this->logger->info('Deleting existing destination file at ' . $targetAbsoluteFilepath); |
77 | $this->filesystem->delete($targetAbsoluteFilepath); |
78 | } |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * @throws FilesystemException |
84 | */ |
85 | public function copy(): void |
86 | { |
87 | $this->logger->notice('Copying files'); |
88 | |
89 | /** |
90 | * @var File $file |
91 | */ |
92 | foreach ($this->files->getFiles() as $file) { |
93 | if (!$file->isDoCopy()) { |
94 | $this->logger->debug('Skipping {sourcePath}', ['sourcePath' => $file->getSourcePath()]); |
95 | continue; |
96 | } |
97 | |
98 | $sourceAbsoluteFilepath = $file->getSourcePath(); |
99 | $targetAbsolutePath = $file->getAbsoluteTargetPath(); |
100 | |
101 | if ($this->filesystem->directoryExists($sourceAbsoluteFilepath)) { |
102 | $this->logger->info( |
103 | 'Creating directory at {targetPath}', |
104 | ['targetPath' => $targetAbsolutePath] |
105 | ); |
106 | $this->filesystem->createDirectory($targetAbsolutePath); |
107 | } elseif ($this->filesystem->fileExists($sourceAbsoluteFilepath)) { |
108 | $this->logger->info( |
109 | 'Copying file to {targetPath}', |
110 | ['targetPath' => $targetAbsolutePath] |
111 | ); |
112 | $this->filesystem->copy($sourceAbsoluteFilepath, $targetAbsolutePath); |
113 | } else { |
114 | $file->setDoPrefix(false); |
115 | $this->logger->warning( |
116 | 'Expected file not found: {sourcePath}', |
117 | ['sourcePath' => $sourceAbsoluteFilepath] |
118 | ); |
119 | } |
120 | } |
121 | } |
122 | } |