Passed
Pull Request — master (#1285)
by Diego
04:50
created

LayoutViewsResource::uninstallResource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console\PackageResources;
4
5
use Illuminate\Support\Facades\File;
6
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;
7
8
class LayoutViewsResource extends PackageResource
9
{
10
    /**
11
     * Create a new resource instance.
12
     *
13
     * @return void
14
     */
15 23
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 23
        $this->description = 'The set of views that defines the AdminLTE layout';
20 23
        $this->target = CommandHelper::getViewPath('vendor/adminlte');
21 23
        $this->required = false;
22
23
        // Note we declare the source as an array of files and folders, we do
24
        // this way o avoid copying the components views located at the
25
        // 'resources/views/components' folder.
26
27 23
        $this->source = [
28 23
            CommandHelper::getPackagePath('resources/views/auth'),
29 23
            CommandHelper::getPackagePath('resources/views/master.blade.php'),
30 23
            CommandHelper::getPackagePath('resources/views/page.blade.php'),
31 23
            CommandHelper::getPackagePath('resources/views/partials'),
32 23
            CommandHelper::getPackagePath('resources/views/plugins.blade.php'),
33 23
        ];
34
35
        // Fill the set of installation messages.
36
37 23
        $this->messages = [
38 23
            'install' => 'Do you want to publish the AdminLTE layout views?',
39 23
            'overwrite' => 'The layout views were already published. Want to replace?',
40 23
            'success' => 'AdminLTE layout views published successfully',
41 23
        ];
42
    }
43
44
    /**
45
     * Installs or publishes the resource.
46
     *
47
     * @return bool
48
     */
49 4
    public function install()
50
    {
51
        // Publish the package layout views.
52
53 4
        foreach ($this->source as $src) {
54 4
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
55
56 4
            if (! $this->publishResource($src, $tgt)) {
57
                return false;
58
            }
59
        }
60
61 4
        return true;
62
    }
63
64
    /**
65
     * Uninstalls the resource.
66
     *
67
     * @return bool
68
     */
69 5
    public function uninstall()
70
    {
71
        // Uninstall the package layout views.
72
73 5
        foreach ($this->source as $src) {
74 5
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
75
76 5
            if (! $this->uninstallResource($tgt)) {
77
                return false;
78
            }
79
        }
80
81 5
        return true;
82
    }
83
84
    /**
85
     * Checks whether the resource already exists in the target location.
86
     *
87
     * @return bool
88
     */
89 8
    public function exists()
90
    {
91 8
        foreach ($this->source as $src) {
92 8
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
93
94 8
            if (File::exists($tgt)) {
95 2
                return true;
96
            }
97
        }
98
99 6
        return false;
100
    }
101
102
    /**
103
     * Checks whether the resource is correctly installed, i.e. if the source
104
     * items matches with the items available at the target location.
105
     *
106
     * @return bool
107
     */
108 6
    public function installed()
109
    {
110 6
        foreach ($this->source as $src) {
111 6
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
112
113 6
            if (! $this->resourceInstalled($src, $tgt)) {
114 5
                return false;
115
            }
116
        }
117
118 4
        return true;
119
    }
120
121
    /**
122
     * Publishes the specified source (usually a file or folder) at the
123
     * specified target location.
124
     *
125
     * @param  string  $source  The source path
126
     * @param  string  $target  The target path
127
     * @return bool
128
     */
129 4
    protected function publishResource($source, $target)
130
    {
131
        // Check whether the resource is a file or a directory.
132
133 4
        return File::isDirectory($source)
134 4
            ? (bool) CommandHelper::copyDirectory($source, $target, true, true)
135 4
            : File::copy($source, $target);
136
    }
137
138
    /**
139
     * Uninstalls the resource at the specified target location.
140
     *
141
     * @param  string  $target  The target path
142
     * @return bool
143
     */
144 5
    protected function uninstallResource($target)
145
    {
146
        // When the target does not exists, we consider the resource as
147
        // unistalled.
148
149 5
        if (! File::exists($target)) {
150 5
            return true;
151
        }
152
153
        // Uninstall the resource at the specified target location.
154
155 5
        return File::isDirectory($target)
156 4
            ? File::deleteDirectory($target)
157 5
            : File::delete($target);
158
    }
159
160
    /**
161
     * Checks whether a resource is correctly installed at the specified target
162
     * location.
163
     *
164
     * @param  string  $source  The source path
165
     * @param  string  $target  The target path
166
     * @return bool
167
     */
168 6
    protected function resourceInstalled($source, $target)
169
    {
170
        // Check whether the resource is a file or a directory.
171
172 6
        return File::isDirectory($source)
173 6
            ? (bool) CommandHelper::compareDirectories($source, $target, true)
174 6
            : CommandHelper::compareFiles($source, $target);
175
    }
176
}
177