Passed
Pull Request — master (#679)
by Diego
06:03
created

AuthViewsResource::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console\PackageResources;
4
5
use JeroenNoten\LaravelAdminLte\Console\PackageResources\PackageResource;
6
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;
7
8
class AuthViewsResource extends PackageResource
9
{
10
    /**
11
     * Array with the replacement content of the authentication views.
12
     *
13
     * @var array
14
     */
15
    protected $authViewsContent = [
16
        'login.blade.php'             => '@extends(\'adminlte::auth.login\')',
17
        'register.blade.php'          => '@extends(\'adminlte::auth.register\')',
18
        'verify.blade.php'            => '@extends(\'adminlte::auth.verify\')',
19
        'passwords/confirm.blade.php' => '@extends(\'adminlte::auth.passwords.confirm\')',
20
        'passwords/email.blade.php'   => '@extends(\'adminlte::auth.passwords.email\')',
21
        'passwords/reset.blade.php'   => '@extends(\'adminlte::auth.passwords.reset\')',
22
    ];
23
24
    /**
25
     * Create a new resource instance.
26
     *
27
     * @return void
28
     */
29 15
    public function __construct()
30
    {
31
        // Fill the resource data.
32
33 15
        $this->description = 'The default package authentication views';
34 15
        $this->source = $this->authViewsContent;
35 15
        $this->target = CommandHelper::getViewPath('auth');
36 15
        $this->required = false;
37
38
        // Fill the set of installation messages.
39
40 15
        $this->messages = [
41
            'install'   => 'Install the AdminLTE authentication views?',
42
            'overwrite' => 'The authentication views already exists. Want to replace the views?',
43
            'success'   => 'Authentication views installed successfully.',
44
        ];
45 15
    }
46
47
    /**
48
     * Install/Export the resource.
49
     *
50
     * @return void
51
     */
52 8
    public function install()
53
    {
54
        // Install the authentication views. We going to replace the content
55
        // of any existing authentication view.
56
57 8
        foreach ($this->source as $file => $content) {
58 8
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
59 8
            CommandHelper::ensureDirectoryExists(dirname($target));
60 8
            file_put_contents($target, $content);
61
        }
62 8
    }
63
64
    /**
65
     * Uninstall/Remove the resource.
66
     *
67
     * @return void
68
     */
69 8
    public function uninstall()
70
    {
71
        // Remove the package authentication views.
72
73 8
        foreach ($this->source as $file => $content) {
74 8
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
75
76 8
            if (is_file($target)) {
77 8
                unlink($target);
78
            }
79
        }
80 8
    }
81
82
    /**
83
     * Check if the resource already exists on the target destination.
84
     *
85
     * @return bool
86
     */
87 9
    public function exists()
88
    {
89
        // Check if any of the authentication views already exists.
90
91 9
        foreach ($this->source as $file => $content) {
92 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
93
94 9
            if (is_file($target)) {
95 1
                return true;
96
            }
97
        }
98
99 8
        return false;
100
    }
101
102
    /**
103
     * Check if the resource is correctly installed.
104
     *
105
     * @return bool
106
     */
107 9
    public function installed()
108
    {
109 9
        foreach ($this->source as $file => $content) {
110 9
            $target = $this->target.DIRECTORY_SEPARATOR.$file;
111
112 9
            if (! $this->authViewInstalled($target, $content)) {
113 4
                return false;
114
            }
115
        }
116
117 8
        return true;
118
    }
119
120
    /**
121
     * Check if an authentication view is correctly installed.
122
     *
123
     * @param string $path Absolute path of the authentication view
124
     * @param string $content The expected content of the view
125
     * @return bool
126
     */
127 9
    protected function authViewInstalled($path, $content)
128
    {
129 9
        return is_file($path) && (file_get_contents($path) === $content);
130
    }
131
}
132