Passed
Pull Request — main (#33)
by
unknown
03:15
created

CopyAssetsToPublicFolder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 75
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fileIsSame() 0 3 1
A fileExists() 0 3 1
A copyDir() 0 3 1
A handle() 0 26 3
1
<?php
2
3
namespace Usernotnull\Toast\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
8
class CopyAssetsToPublicFolder extends Command
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $signature = 'tall-toasts:assets';
14
15
    /**
16
     * @var string
17
     */
18
    protected $description = 'Copy tall toasts assets to the public folder.';
19
20
    /**
21
     * Copies the files only if necessary.
22
     *
23
     * @return int
24
     */
25
    public function handle(): int
26
    {
27
        $vendorFolder = __DIR__ . '/../../dist/js';
28
29
        $vendorToastFile = $vendorFolder . '/tall-toasts.js';
30
31
32
        $publicFolder = public_path('/toast');
33
34
        $publicToastFile = $publicFolder . '/tall-toasts.js';
35
36
        if ($this->fileExists($publicToastFile)) {
37
            if ($this->fileIsSame($vendorToastFile, $publicToastFile)) {
38
                $this->info('Tall Toasts Javascript does not need to be copied.');
39
40
                return Command::SUCCESS;
41
            };
42
        } else {
43
            File::makeDirectory($publicFolder);
44
        }
45
46
        $this->copyDir($vendorFolder, $publicFolder);
47
48
        $this->info('Copied Tall Toasts assets to ' . $publicFolder);
49
50
        return Command::SUCCESS;
51
    }
52
53
    /**
54
     * Checks if a file exists.
55
     * @param string $fileName
56
     * @return bool
57
     */
58
    private function fileExists(string $fileName): bool
59
    {
60
        return File::exists($fileName);
61
    }
62
63
    /**
64
     * Checks if two file's contents are the same.
65
     * @param string $originalFile
66
     * @param string $publicFile
67
     * @return bool
68
     */
69
    private function fileIsSame(string $originalFile, string $publicFile): bool
70
    {
71
        return file_get_contents($originalFile) === file_get_contents($publicFile);
72
    }
73
74
    /**
75
     * Copies a file to a new location.
76
     * @param string $fileFrom
77
     * @param string $fileTo
78
     * @return bool
79
     */
80
    private function copyDir(string $fromFolder, string $toFolder): bool
81
    {
82
        return File::copyDirectory($fromFolder, $toFolder);
83
    }
84
}
85