Passed
Pull Request — master (#1285)
by Diego
04:11
created

BasicRoutesResource   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
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 17 2
A exists() 0 9 2
A uninstall() 0 15 2
A __construct() 0 15 1
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 BasicRoutesResource 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 = 'A 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 publish.
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 File::put($this->target, $routes, FILE_APPEND);
0 ignored issues
show
Bug introduced by
JeroenNoten\LaravelAdmin...geResources\FILE_APPEND of type integer is incompatible with the type boolean expected by parameter $lock of Illuminate\Support\Facades\File::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        return File::put($this->target, $routes, /** @scrutinizer ignore-type */ FILE_APPEND);
Loading history...
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
70 6
        if (File::isFile($this->target)) {
71 6
            $targetContent = File::get($this->target);
72 6
            $targetContent = str_replace($routes, '', $targetContent);
73 6
            return File::put($this->target, $targetContent);
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * Checks whether the resource already exists in the target location.
81
     *
82
     * @return bool
83
     */
84 7
    public function exists()
85
    {
86 7
        $routes = File::get($this->source);
87
88
        // Check whether the target routes file exists and contains the
89
        // expected routes.
90
91 7
        return File::isFile($this->target)
92 7
            && (strpos(File::get($this->target), $routes) !== false);
93
    }
94
95
    /**
96
     * Checks whether the resource is correctly installed, i.e. if the source
97
     * items matches with the items available at the target location.
98
     *
99
     * @return bool
100
     */
101 7
    public function installed()
102
    {
103 7
        return $this->exists();
104
    }
105
}
106