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

BasicViewsResource::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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