AuthViewsResource   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 140
ccs 37
cts 37
cp 1
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstall() 0 9 3
A authViewInstalled() 0 3 2
A exists() 0 15 3
A installed() 0 11 3
A install() 0 10 2
A authViewExists() 0 4 2
A __construct() 0 15 1
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 30
    public function __construct()
31
    {
32
        // Fill the resource data.
33
34 30
        $this->description = 'The set of AdminLTE replacement auth views for the Laravel/UI package';
35 30
        $this->source = $this->authViewsContent;
36 30
        $this->target = CommandHelper::getViewPath('auth');
37 30
        $this->required = false;
38
39
        // Fill the set of installation messages.
40
41 30
        $this->messages = [
42 30
            'install' => 'Do you want to publish the replacement auth views for Laravel/UI?',
43 30
            'overwrite' => 'The auth views were already published. Want to replace?',
44 30
            'success' => 'Auth views published successfully',
45 30
        ];
46
    }
47
48
    /**
49
     * Installs or publishes the resource.
50
     *
51
     * @return void
52
     */
53 11
    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 11
        foreach ($this->source as $file => $content) {
60 11
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
61 11
            File::ensureDirectoryExists(File::dirname($target));
62 11
            File::put($target, $content);
63
        }
64
    }
65
66
    /**
67
     * Uninstalls the resource.
68
     *
69
     * @return void
70
     */
71 11
    public function uninstall()
72
    {
73
        // Remove the published authentication views.
74
75 11
        foreach ($this->source as $file => $content) {
76 11
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
77
78 11
            if (File::isFile($target)) {
79 11
                File::delete($target);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Checks whether the resource already exists in the target location.
86
     *
87
     * @return bool
88
     */
89 9
    public function exists()
90
    {
91
        // Check if any of the authentication views is published. We need to
92
        // check that at least one of the target files exists and the
93
        // replacement content is present.
94
95 9
        foreach ($this->source as $file => $content) {
96 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
97
98 9
            if ($this->authViewExists($target, $content)) {
99 1
                return true;
100
            }
101
        }
102
103 8
        return false;
104
    }
105
106
    /**
107
     * Checks whether the resource is correctly installed, i.e. if the source
108
     * items matches with the items available at the target location.
109
     *
110
     * @return bool
111
     */
112 12
    public function installed()
113
    {
114 12
        foreach ($this->source as $file => $content) {
115 12
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
116
117 12
            if (! $this->authViewInstalled($target, $content)) {
118 7
                return false;
119
            }
120
        }
121
122 9
        return true;
123
    }
124
125
    /**
126
     * Checks whether an authentication view exists.
127
     *
128
     * @param  string  $path  Absolute path of the authentication view
129
     * @param  string  $content  The expected content of the view
130
     * @return bool
131
     */
132 9
    protected function authViewExists($path, $content)
133
    {
134 9
        return File::isFile($path)
135 9
            && strpos(File::get($path), $content) !== false;
136
    }
137
138
    /**
139
     * Checks whether an authentication view is correctly installed.
140
     *
141
     * @param  string  $path  Absolute path of the authentication view
142
     * @param  string  $content  The expected content of the view
143
     * @return bool
144
     */
145 12
    protected function authViewInstalled($path, $content)
146
    {
147 12
        return File::isFile($path) && File::get($path) === $content;
148
    }
149
}
150