Completed
Push — master ( 460b97...b63ecb )
by Ryan
05:27
created

ExtensionModel::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Addon\Extension;
2
3
use Anomaly\Streams\Platform\Addon\Extension\Contract\ExtensionInterface;
4
use Anomaly\Streams\Platform\Model\EloquentCollection;
5
use Anomaly\Streams\Platform\Model\EloquentModel;
6
use Anomaly\Streams\Platform\Model\EloquentObserver;
7
8
/**
9
 * Class ExtensionModel
10
 *
11
 * @link    http://pyrocms.com/
12
 * @author  PyroCMS, Inc. <[email protected]>
13
 * @author  Ryan Thompson <[email protected]>
14
 */
15 View Code Duplication
class ExtensionModel extends EloquentModel implements ExtensionInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
18
    /**
19
     * Define the table name.
20
     *
21
     * @var string
22
     */
23
    protected $table = 'addons_extensions';
24
25
    /**
26
     * Disable timestamps for extensions.
27
     *
28
     * @var bool
29
     */
30
    public $timestamps = false;
31
32
    /**
33
     * Boot the model
34
     */
35
    protected static function boot()
36
    {
37
        self::observe(app(EloquentObserver::class));
38
39
        parent::boot();
40
    }
41
42
    /**
43
     * Find a extension by it's namespace or return a new
44
     * extension with the given namespace.
45
     *
46
     * @param  $namespace
47
     * @return ExtensionModel
48
     */
49
    public function findByNamespaceOrNew($namespace)
50
    {
51
        $extension = $this->findByNamespace($namespace);
52
53
        if ($extension instanceof ExtensionModel) {
54
            return $extension;
55
        }
56
57
        $extension = $this->newInstance();
58
59
        $extension->namespace = $namespace;
0 ignored issues
show
Documentation introduced by
The property namespace does not exist on object<Anomaly\Streams\P...tension\ExtensionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
61
        $extension->save();
62
63
        return $extension;
64
    }
65
66
    /**
67
     * Find a extension by it's namespace.
68
     *
69
     * @param  $namespace
70
     * @return mixed
71
     */
72
    public function findByNamespace($namespace)
73
    {
74
        return $this->where('namespace', $namespace)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...tension\ExtensionModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
    }
76
77
    /**
78
     * Get all enabled extension namespaces.
79
     *
80
     * @return EloquentCollection
81
     */
82
    public function getEnabledNamespaces()
83
    {
84
        return $this->where('enabled', true)->get()->pluck('namespace');
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...tension\ExtensionModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
    }
86
87
    /**
88
     * Get all installed extension namespaces.
89
     *
90
     * @return EloquentCollection
91
     */
92
    public function getInstalledNamespaces()
93
    {
94
        return $this->where('installed', true)->get()->pluck('namespace');
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...tension\ExtensionModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
95
    }
96
97
    /**
98
     * Return a new collection.
99
     *
100
     * @param  array $items
101
     * @return EloquentCollection
102
     */
103
    public function newCollection(array $items = [])
104
    {
105
        return new EloquentCollection($items);
106
    }
107
}
108