EloquentRepository::uninstall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Repositories\Extension;
4
5
use Illuminate\Foundation\Validation\ValidatesRequests;
6
use Illuminate\Support\Facades\File;
7
use Yajra\CMS\Entities\Extension;
8
use Yajra\CMS\Exceptions\InvalidManifestException;
9
use Yajra\CMS\Repositories\RepositoryAbstract;
10
11
class EloquentRepository extends RepositoryAbstract implements Repository
12
{
13
    use ValidatesRequests;
14
15
    /**
16
     * Install an extension.
17
     *
18
     * @param string $type
19
     * @param array $attributes
20
     * @return \Yajra\CMS\Entities\Extension
21
     */
22
    public function install($type, array $attributes)
23
    {
24
        $extension       = new Extension;
25
        $extension->type = $type;
26
        $extension->name = $attributes['name'];
27
        if (isset($attributes['parameters'])) {
28
            $extension->parameters = $attributes['parameters'];
29
        }
30
        $extension->manifest = json_encode($attributes);
31
        $extension->save();
32
33
        return $extension;
34
    }
35
36
    /**
37
     * Uninstall extension.
38
     *
39
     * @param int $id
40
     * @throws \Exception
41
     */
42
    public function uninstall($id)
43
    {
44
        $extension = $this->getModel()->query()->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method query does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
        // TODO: remove extension files.
46
        $extension->delete();
47
    }
48
49
    /**
50
     * Get repository model.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
53
     */
54
    public function getModel()
55
    {
56
        return new Extension;
57
    }
58
59
    /**
60
     * Get all extensions.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Collection|static[]
63
     */
64
    public function all()
65
    {
66
        return $this->getModel()->all();
0 ignored issues
show
Bug introduced by
The method all does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
    }
68
69
    /**
70
     * Find or fail an extension.
71
     *
72
     * @param int $id
73
     * @return \Yajra\CMS\Entities\Extension
74
     */
75
    public function findOrFail($id)
76
    {
77
        return $this->getModel()->query()->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method query does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
    }
79
80
    /**
81
     * Get all registered widgets.
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85
    public function allWidgets()
86
    {
87
        return $this->getModel()->query()->where('type', 'widget')->get();
0 ignored issues
show
Bug introduced by
The method query does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
88
    }
89
90
    /**
91
     * Register an extension using manifest config file.
92
     *
93
     * @param string $path
94
     * @throws \Exception
95
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
96
     */
97
    public function registerManifest($path)
98
    {
99
        if (! File::exists($path)) {
100
            throw new \Exception('Extension manifest file does not exist! Path: ' . $path);
101
        }
102
103
        $manifest = File::get($path);
104
        $manifest = json_decode($manifest, true);
105
        $this->register($manifest);
106
    }
107
108
    /**
109
     * Register an extension.
110
     *
111
     * @param array $attributes
112
     * @return $this
113
     * @throws \Exception
114
     */
115
    public function register(array $attributes)
116
    {
117
        $validator = $this->getValidationFactory()->make($attributes, [
118
            'type'                 => 'required',
119
            'name'                 => 'required',
120
            'parameters.class'     => 'required_if:type,widget',
121
            'parameters.templates' => 'required_if:type,widget',
122
            'parameters.template'  => 'required_if:type,menu',
123
        ]);
124
125
        if ($validator->fails()) {
126
            throw new InvalidManifestException("Invalid manifest file detected!");
127
        }
128
129
        $extension       = new Extension;
130
        $extension->type = $attributes['type'];
131
        $extension->name = $attributes['name'];
132
        if (isset($attributes['protected'])) {
133
            $extension->protected = $attributes['protected'];
134
        }
135
        if (isset($attributes['parameters'])) {
136
            $extension->parameters = $attributes['parameters'];
137
        }
138
        $extension->manifest = json_encode($attributes);
139
        $extension->save();
140
141
        return $this;
142
    }
143
}
144