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

AuthViewsResource   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 40
c 3
b 1
f 0
dl 0
loc 147
ccs 38
cts 40
cp 0.95
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 13 4
A __construct() 0 15 1
A authViewInstalled() 0 3 2
A exists() 0 15 3
A installed() 0 11 3
A install() 0 16 3
A authViewExists() 0 4 2
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 = 'The set of AdminLTE replacement auth views for the Laravel/UI package';
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 actually need to replace the
56
        // content of any existing authentication view that were originally
57
        // provided by 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. We need to
99
        // check that at least one of the target files exists and the
100
        // replacement content is present.
101
102 9
        foreach ($this->source as $file => $content) {
103 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
104
105 9
            if ($this->authViewExists($target, $content)) {
106 1
                return true;
107
            }
108
        }
109
110 8
        return false;
111
    }
112
113
    /**
114
     * Checks whether the resource is correctly installed, i.e. if the source
115
     * items matches with the items available at the target location.
116
     *
117
     * @return bool
118
     */
119 9
    public function installed()
120
    {
121 9
        foreach ($this->source as $file => $content) {
122 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
123
124 9
            if (! $this->authViewInstalled($target, $content)) {
125 4
                return false;
126
            }
127
        }
128
129 8
        return true;
130
    }
131
132
    /**
133
     * Checks whether an authentication view exists.
134
     *
135
     * @param  string  $path  Absolute path of the authentication view
136
     * @param  string  $content  The expected content of the view
137
     * @return bool
138
     */
139 9
    protected function authViewExists($path, $content)
140
    {
141 9
        return File::isFile($path)
142 9
            && strpos(File::get($path), $content) !== false;
143
    }
144
145
    /**
146
     * Checks whether an authentication view is correctly installed.
147
     *
148
     * @param  string  $path  Absolute path of the authentication view
149
     * @param  string  $content  The expected content of the view
150
     * @return bool
151
     */
152 9
    protected function authViewInstalled($path, $content)
153
    {
154 9
        return File::isFile($path) && File::get($path) === $content;
155
    }
156
}
157