Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Copier
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 prepareTarget
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 copy
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
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
14namespace BrianHenryIE\Strauss;
15
16use BrianHenryIE\Strauss\Composer\ComposerPackage;
17use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
18use League\Flysystem\Filesystem;
19use League\Flysystem\Local\LocalFilesystemAdapter;
20
21class Copier
22{
23    /**
24     * The only path variable with a leading slash.
25     * All directories in project end with a slash.
26     *
27     * @var string
28     */
29    protected string $workingDir;
30
31    protected string $absoluteTargetDir;
32
33    protected DiscoveredFiles $files;
34
35    /** @var Filesystem */
36    protected Filesystem $filesystem;
37
38    /**
39     * Copier constructor.
40     *
41     * @param DiscoveredFiles $files
42     * @param string $workingDir
43     * @param StraussConfig $config
44     */
45    public function __construct(DiscoveredFiles $files, string $workingDir, StraussConfig $config)
46    {
47        $this->files = $files;
48
49        $this->absoluteTargetDir = $workingDir . $config->getTargetDirectory();
50
51        $this->filesystem = new Filesystem(new LocalFilesystemAdapter('/'));
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     * @return void
59     */
60    public function prepareTarget(): void
61    {
62        if (! is_dir($this->absoluteTargetDir)) {
63            $this->filesystem->createDirectory($this->absoluteTargetDir);
64            $this->filesystem->setVisibility($this->absoluteTargetDir, 'public');
65        } else {
66            foreach ($this->files->getFiles() as $file) {
67                $targetAbsoluteFilepath = $this->absoluteTargetDir . $file->getTargetRelativePath();
68
69                if ($this->filesystem->fileExists($targetAbsoluteFilepath)) {
70                    $this->filesystem->delete($targetAbsoluteFilepath);
71                }
72            }
73        }
74    }
75
76    public function copy(): void
77    {
78        /**
79         * @var File $file
80         */
81        foreach ($this->files->getFiles() as $file) {
82            $sourceAbsoluteFilepath = $file->getSourcePath();
83
84            $targetAbsolutePath = $this->absoluteTargetDir . $file->getTargetRelativePath();
85
86            $this->filesystem->copy($sourceAbsoluteFilepath, $targetAbsolutePath);
87        }
88    }
89}