Passed
Push — master ( 3fd782...cac45a )
by Diego
04:58
created

LayoutViewsResource   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 161
ccs 50
cts 50
cp 1
rs 10
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstallResource() 0 15 3
A install() 0 7 2
A publishResource() 0 8 2
A resourceInstalled() 0 7 2
A uninstall() 0 7 2
A __construct() 0 26 1
A exists() 0 11 3
A installed() 0 11 3
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 24
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 24
        $this->description = 'The set of views that defines the AdminLTE layout';
20 24
        $this->target = CommandHelper::getViewPath('vendor/adminlte');
21 24
        $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 24
        $this->source = [
28 24
            CommandHelper::getPackagePath('resources/views/auth'),
29 24
            CommandHelper::getPackagePath('resources/views/master.blade.php'),
30 24
            CommandHelper::getPackagePath('resources/views/page.blade.php'),
31 24
            CommandHelper::getPackagePath('resources/views/partials'),
32 24
            CommandHelper::getPackagePath('resources/views/plugins.blade.php'),
33 24
        ];
34
35
        // Fill the set of installation messages.
36
37 24
        $this->messages = [
38 24
            'install' => 'Do you want to publish the AdminLTE layout views?',
39 24
            'overwrite' => 'The layout views were already published. Want to replace?',
40 24
            'success' => 'AdminLTE layout views published successfully',
41 24
        ];
42
    }
43
44
    /**
45
     * Installs or publishes the resource.
46
     *
47
     * @return void
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 4
            $this->publishResource($src, $tgt);
56
        }
57
    }
58
59
    /**
60
     * Uninstalls the resource.
61
     *
62
     * @return void
63
     */
64 5
    public function uninstall()
65
    {
66
        // Uninstall the package layout views.
67
68 5
        foreach ($this->source as $src) {
69 5
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
70 5
            $this->uninstallResource($tgt);
71
        }
72
    }
73
74
    /**
75
     * Checks whether the resource already exists in the target location.
76
     *
77
     * @return bool
78
     */
79 8
    public function exists()
80
    {
81 8
        foreach ($this->source as $src) {
82 8
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
83
84 8
            if (File::exists($tgt)) {
85 2
                return true;
86
            }
87
        }
88
89 6
        return false;
90
    }
91
92
    /**
93
     * Checks whether the resource is correctly installed, i.e. if the source
94
     * items matches with the items available at the target location.
95
     *
96
     * @return bool
97
     */
98 6
    public function installed()
99
    {
100 6
        foreach ($this->source as $src) {
101 6
            $tgt = $this->target.DIRECTORY_SEPARATOR.File::basename($src);
102
103 6
            if (! $this->resourceInstalled($src, $tgt)) {
104 5
                return false;
105
            }
106
        }
107
108 4
        return true;
109
    }
110
111
    /**
112
     * Publishes the specified source (usually a file or folder) at the
113
     * specified target location.
114
     *
115
     * @param  string  $source  The source path
116
     * @param  string  $target  The target path
117
     * @return void
118
     */
119 4
    protected function publishResource($source, $target)
120
    {
121
        // Check whether the resource is a file or a directory.
122
123 4
        if (File::isDirectory($source)) {
124 4
            CommandHelper::copyDirectory($source, $target, true, true);
125
        } else {
126 4
            File::copy($source, $target);
127
        }
128
    }
129
130
    /**
131
     * Uninstalls the resource at the specified target location.
132
     *
133
     * @param  string  $target  The target path
134
     * @return void
135
     */
136 5
    protected function uninstallResource($target)
137
    {
138
        // When the target does not exists, we consider the resource as
139
        // unistalled.
140
141 5
        if (! File::exists($target)) {
142 5
            return;
143
        }
144
145
        // Uninstall the resource at the specified target location.
146
147 5
        if (File::isDirectory($target)) {
148 4
            File::deleteDirectory($target);
149
        } else {
150 5
            File::delete($target);
151
        }
152
    }
153
154
    /**
155
     * Checks whether a resource is correctly installed at the specified target
156
     * location.
157
     *
158
     * @param  string  $source  The source path
159
     * @param  string  $target  The target path
160
     * @return bool
161
     */
162 6
    protected function resourceInstalled($source, $target)
163
    {
164
        // Check whether the resource is a file or a directory.
165
166 6
        return File::isDirectory($source)
167 6
            ? (bool) CommandHelper::compareDirectories($source, $target, true)
168 6
            : CommandHelper::compareFiles($source, $target);
169
    }
170
}
171