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

BasicRoutesResource::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
ccs 6
cts 6
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 BasicRoutesResource extends PackageResource
9
{
10
    /**
11
     * Create a new resource instance.
12
     *
13
     * @return void
14
     */
15 15
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 15
        $this->description = 'The package routes';
20 15
        $this->source = CommandHelper::getStubPath('routes.stub');
21 15
        $this->target = base_path('routes/web.php');
22 15
        $this->required = false;
23
24
        // Fill the installation messages.
25
26 15
        $this->messages = [
27
            'install'   => 'Install the basic package routes?',
28
            'overwrite' => 'Basic routes are already installed. Want to install they again?',
29
            'success'   => 'Basic routes installed successfully.',
30
        ];
31 15
    }
32
33
    /**
34
     * Install/Export the resource.
35
     *
36
     * @return void
37
     */
38 6
    public function install()
39
    {
40
        // If routes already exists, there is no need to install again.
41
42 6
        if ($this->exists()) {
43 1
            return;
44
        }
45
46
        // Get the routes to install.
47
48 5
        $routes = file_get_contents($this->source);
49
50
        // Add the routes.
51
52 5
        CommandHelper::ensureDirectoryExists(dirname($this->target));
53 5
        file_put_contents($this->target, $routes, FILE_APPEND);
54 5
    }
55
56
    /**
57
     * Uninstall/Remove the resource.
58
     *
59
     * @return void
60
     */
61 6
    public function uninstall()
62
    {
63 6
        $routes = file_get_contents($this->source);
64
65
        // If the target routes file exists, remove the package routes.
66
67 6
        if (is_file($this->target)) {
68 6
            $targetContent = file_get_contents($this->target);
69 6
            $targetContent = str_replace($routes, '', $targetContent);
70 6
            file_put_contents($this->target, $targetContent);
71
        }
72 6
    }
73
74
    /**
75
     * Check if the resource already exists on the target destination.
76
     *
77
     * @return bool
78
     */
79 7
    public function exists()
80
    {
81 7
        $routes = file_get_contents($this->source);
82
83
        // First, check if the target routes file exists.
84
85 7
        if (! is_file($this->target)) {
86 2
            return false;
87
        }
88
89
        // Now, check if the target file already contains the routes.
90
91 7
        $targetContent = file_get_contents($this->target);
92
93 7
        return (strpos($targetContent, $routes) !== false);
94
    }
95
96
    /**
97
     * Check if the resource is correctly installed.
98
     *
99
     * @return bool
100
     */
101 7
    public function installed()
102
    {
103 7
        return $this->exists();
104
    }
105
}
106