Passed
Push — master ( 3b0f08...588cba )
by Florian
12:00 queued 09:25
created

BasicRoutesResource   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 96
ccs 29
cts 29
cp 1
rs 10
wmc 8

5 Methods

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