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

BasicRoutesResource   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

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

5 Methods

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