Passed
Pull Request — master (#1285)
by Diego
03:34
created

AuthViewsResource::uninstall()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
ccs 5
cts 6
cp 0.8333
crap 4.074
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 AuthViewsResource extends PackageResource
9
{
10
    /**
11
     * Array with the replacement content for the authentication views of the
12
     * legacy Laravel/UI package.
13
     *
14
     * @var array
15
     */
16
    protected $authViewsContent = [
17
        'login.blade.php' => '@extends(\'adminlte::auth.login\')',
18
        'register.blade.php' => '@extends(\'adminlte::auth.register\')',
19
        'verify.blade.php' => '@extends(\'adminlte::auth.verify\')',
20
        'passwords/confirm.blade.php' => '@extends(\'adminlte::auth.passwords.confirm\')',
21
        'passwords/email.blade.php' => '@extends(\'adminlte::auth.passwords.email\')',
22
        'passwords/reset.blade.php' => '@extends(\'adminlte::auth.passwords.reset\')',
23
    ];
24
25
    /**
26
     * Create a new resource instance.
27
     *
28
     * @return void
29
     */
30 23
    public function __construct()
31
    {
32
        // Fill the resource data.
33
34 23
        $this->description = 'AdminLTE styled auth views to replace the Laravel/UI ones';
35 23
        $this->source = $this->authViewsContent;
36 23
        $this->target = CommandHelper::getViewPath('auth');
37 23
        $this->required = false;
38
39
        // Fill the set of installation messages.
40
41 23
        $this->messages = [
42 23
            'install' => 'Do you want to publish the replacement auth views for Laravel/UI?',
43 23
            'overwrite' => 'The auth views were already published. Want to replace?',
44 23
            'success' => 'Auth views published successfully',
45 23
        ];
46
    }
47
48
    /**
49
     * Installs or publishes the resource.
50
     *
51
     * @return bool
52
     */
53 8
    public function install()
54
    {
55
        // Publish the authentication views. We are going to replace the content
56
        // of any existing authentication view that were originally provided by
57
        // the legacy Laravel/UI package.
58
59 8
        foreach ($this->source as $file => $content) {
60 8
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
61 8
            File::ensureDirectoryExists(File::dirname($target));
62
63 8
            if (! File::put($target, $content)) {
64
                return false;
65
            }
66
        }
67
68 8
        return true;
69
    }
70
71
    /**
72
     * Uninstalls the resource.
73
     *
74
     * @return bool
75
     */
76 8
    public function uninstall()
77
    {
78
        // Remove the published authentication views.
79
80 8
        foreach ($this->source as $file => $content) {
81 8
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
82
83 8
            if (File::isFile($target) && ! File::delete($target)) {
84
                return false;
85
            }
86
        }
87
88 8
        return true;
89
    }
90
91
    /**
92
     * Checks whether the resource already exists in the target location.
93
     *
94
     * @return bool
95
     */
96 9
    public function exists()
97
    {
98
        // Check if any of the authentication views is published.
99
100 9
        foreach ($this->source as $file => $content) {
101 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
102
103 9
            if (File::isFile($target)) {
104 1
                return true;
105
            }
106
        }
107
108 8
        return false;
109
    }
110
111
    /**
112
     * Checks whether the resource is correctly installed, i.e. if the source
113
     * items matches with the items available at the target location.
114
     *
115
     * @return bool
116
     */
117 9
    public function installed()
118
    {
119 9
        foreach ($this->source as $file => $content) {
120 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
121
122 9
            if (! $this->authViewInstalled($target, $content)) {
123 4
                return false;
124
            }
125
        }
126
127 8
        return true;
128
    }
129
130
    /**
131
     * Checks whether an authentication view is correctly installed.
132
     *
133
     * @param  string  $path  Absolute path of the authentication view
134
     * @param  string  $content  The expected content of the view
135
     * @return bool
136
     */
137 9
    protected function authViewInstalled($path, $content)
138
    {
139 9
        return File::isFile($path) && (File::get($path) === $content);
140
    }
141
}
142