Passed
Push — master ( 588cba...7cce6c )
by Florian
03:54
created

BasicViewsResource   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 118
ccs 33
cts 34
cp 0.9706
rs 10
wmc 14

6 Methods

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