Passed
Pull Request — master (#1285)
by Diego
06:46
created

AuthRoutesResource::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 17
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
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 AuthRoutesResource extends PackageResource
9
{
10
    /**
11
     * Create a new resource instance.
12
     *
13
     * @return void
14
     */
15 23
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 23
        $this->description = 'The set of routes for the Laravel/UI auth scaffolding';
20 23
        $this->source = CommandHelper::getStubPath('routes.stub');
21 23
        $this->target = base_path('routes/web.php');
22 23
        $this->required = false;
23
24
        // Fill the installation messages.
25
26 23
        $this->messages = [
27 23
            'install' => 'Do you want to publish the Laravel/UI auth routes?',
28 23
            'overwrite' => 'The auth routes were already published. Want to publish again?',
29 23
            'success' => 'Auth routes published successfully',
30 23
        ];
31
    }
32
33
    /**
34
     * Installs or publishes the resource.
35
     *
36
     * @return bool
37
     */
38 6
    public function install()
39
    {
40
        // If the routes already exists, we won't publish they again.
41
42 6
        if ($this->exists()) {
43 1
            return true;
44
        }
45
46
        // Get the set of routes to be published.
47
48 5
        $routes = File::get($this->source);
49
50
        // Add the routes to the web routes file.
51
52 5
        File::ensureDirectoryExists(File::dirname($this->target));
53
54 5
        return (bool) File::append($this->target, $routes);
55
    }
56
57
    /**
58
     * Uninstalls the resource.
59
     *
60
     * @return bool
61
     */
62 6
    public function uninstall()
63
    {
64
        // Get the set of routes to be removed.
65
66 6
        $routes = File::get($this->source);
67
68
        // If the target routes file exists, then remove the auth routes.
69
        // Otherwise, we consider the routes as uninstalled.
70
71 6
        if (File::isFile($this->target)) {
72 6
            $targetContent = File::get($this->target);
73 6
            $targetContent = str_replace($routes, '', $targetContent);
74
75 6
            return (bool) File::put($this->target, $targetContent);
76
        }
77
78
        return true;
79
    }
80
81
    /**
82
     * Checks whether the resource already exists in the target location.
83
     *
84
     * @return bool
85
     */
86 7
    public function exists()
87
    {
88 7
        $routes = File::get($this->source);
89
90
        // Check whether the target routes file exists and contains the
91
        // expected routes.
92
93 7
        return File::isFile($this->target)
94 7
            && (strpos(File::get($this->target), $routes) !== false);
95
    }
96
97
    /**
98
     * Checks whether the resource is correctly installed, i.e. if the source
99
     * items matches with the items available at the target location.
100
     *
101
     * @return bool
102
     */
103 7
    public function installed()
104
    {
105 7
        return $this->exists();
106
    }
107
}
108