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