Passed
Push — master ( 3fd782...cac45a )
by Diego
04:58
created

AuthRoutesResource::installed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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