Passed
Push — master ( 7ddb84...864384 )
by Diego
04:12
created

BladeComponentsResource::installed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 3
rs 10
c 1
b 0
f 0
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 BladeComponentsResource extends PackageResource
9
{
10
    /**
11
     * Create a new package resource instance.
12
     *
13
     * @return void
14
     */
15 25
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 25
        $this->description = 'The set of blade components provided by this package';
20
21 25
        $this->source = [
22 25
            'classes' => CommandHelper::getPackagePath('src/View/Components'),
23 25
            'views' => CommandHelper::getPackagePath('resources/views/components'),
24 25
        ];
25
26 25
        $this->target = [
27 25
            'classes' => app_path('View/Components/Adminlte'),
28 25
            'views' => CommandHelper::getViewPath('vendor/adminlte/components'),
29 25
        ];
30
31 25
        $this->required = false;
32
33
        // Fill the set of installation messages.
34
35 25
        $this->messages = [
36 25
            'install' => 'Do you want to publish the blade component files?',
37 25
            'overwrite' => 'Blade components were already published. Want to replace?',
38 25
            'success' => 'Blade components files published successfully',
39 25
        ];
40
    }
41
42
    /**
43
     * Installs or publishes the resource.
44
     *
45
     * @return void
46
     */
47 4
    public function install()
48
    {
49
        // Copy the component classes to the publishing destination.
50
51 4
        CommandHelper::CopyDirectory(
52 4
            $this->source['classes'],
53 4
            $this->target['classes'],
54 4
            true,
55 4
            true
56 4
        );
57
58
        // Copy the component views to the publishing destination.
59
60 4
        CommandHelper::CopyDirectory(
61 4
            $this->source['views'],
62 4
            $this->target['views'],
63 4
            true,
64 4
            true
65 4
        );
66
67
        // Adapt published components classes to the Laravel framework, this
68
        // will mainly change the component classes namespace.
69
70 4
        $this->adaptPublishedComponentClasses();
71
    }
72
73
    /**
74
     * Uninstalls the resource.
75
     *
76
     * @return void
77
     */
78 4
    public function uninstall()
79
    {
80
        // Remove the component classes from the target folder. When
81
        // component classes does not exists, we consider they as uninstalled.
82
83 4
        if (File::isDirectory($this->target['classes'])) {
84 4
            File::deleteDirectory($this->target['classes']);
85
        }
86
87
        // Remove the component views from the target folder. When
88
        // component views does not exists, we consider they as uninstalled.
89
90 4
        if (File::isDirectory($this->target['views'])) {
91 4
            File::deleteDirectory($this->target['views']);
92
        }
93
    }
94
95
    /**
96
     * Checks whether the resource already exists in the target location.
97
     *
98
     * @return bool
99
     */
100 5
    public function exists()
101
    {
102 5
        return File::isDirectory($this->target['classes'])
103 5
            || File::isDirectory($this->target['views']);
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 5
    public function installed()
113
    {
114
        // Note we can't expect the published component classes to match
115
        // exactly with the default package ones, since the namespace of the
116
        // classes are changed during the installation process. So, we'll just
117
        // control that the number of published component classes matches with
118
        // the packages default ones.
119
120 5
        $srcClassesNum = count(File::allFiles($this->source['classes']));
121
122 5
        $tgtClassesNum = File::isDirectory($this->target['classes'])
123 4
            ? count(File::allFiles($this->target['classes']))
124 4
            : 0;
125
126 5
        return $srcClassesNum === $tgtClassesNum
127 5
            && CommandHelper::compareDirectories(
128 5
                $this->source['views'],
129 5
                $this->target['views'],
130 5
                true
131 5
            );
132
    }
133
134
    /**
135
     * Adapt the published blade component classes by changing their namespace.
136
     *
137
     * @return void
138
     */
139 4
    protected function adaptPublishedComponentClasses()
140
    {
141
        // Get an array of all published component classes files.
142
143 4
        $files = File::allFiles($this->target['classes']);
144
145
        // Make replacements on each of the collected files.
146
147 4
        foreach ($files as $file) {
148 4
            $content = File::get($file->getPathname());
149
150
            // Replace the namespace.
151
152 4
            $content = str_replace(
153 4
                'namespace JeroenNoten\LaravelAdminLte\View\Components',
154 4
                'namespace App\View\Components\Adminlte',
155 4
                $content
156 4
            );
157
158
            // Put the new content in the file.
159
160 4
            File::put($file->getPathname(), $content);
161
        }
162
    }
163
}
164