DynamicRoute   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 58
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A save() 0 15 4
1
<?php
2
3
4
namespace ElementsFramework\DynamicRouting\Model;
5
6
7
use ElementsFramework\DynamicRouting\Exception\BasicValidationFailedForRouteException;
8
use ElementsFramework\DynamicRouting\Exception\HandlerNotFoundException;
9
use ElementsFramework\DynamicRouting\Exception\HandlerValidationFailedForRouteException;
10
use ElementsFramework\DynamicRouting\Service\RouteHandlerResolver;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Facades\Validator;
13
14
class DynamicRoute extends Model
15
{
16
17
    /**
18
     * Mass assignment protection - fillable fields
19
     * @var array
20
     */
21
    protected $fillable = [
22
        'method',
23
        'name',
24
        'pattern',
25
        'handler',
26
        'configuration'
27
    ];
28
29
    /**
30
     * Validation rules.
31
     * @var array
32
     */
33
    public static $validation = [
34
        'method' => ['required'],
35
        'name' => ['required'],
36
        'pattern' => ['required'],
37
        'handler' => ['required']
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected $casts = [
44
        'configuration' => 'array',
45
    ];
46
47
    /**
48
     * Saves the route if valid.
49
     * @param array $options
50
     * @return bool
51
     * @throws BasicValidationFailedForRouteException
52
     * @throws HandlerNotFoundException
53
     * @throws HandlerValidationFailedForRouteException
54
     */
55
    public function save(array $options = [])
56
    {
57
        $validator = Validator::make($this->toArray(), self::$validation);
58
        if($validator->fails()) {
59
            throw BasicValidationFailedForRouteException::fromRoute($this);
60
        }
61
        if(RouteHandlerResolver::handlerExists($this->handler) === false) {
0 ignored issues
show
Documentation introduced by
The property handler does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
62
            throw HandlerNotFoundException::fromIdentifier($this->handler);
0 ignored issues
show
Documentation introduced by
The property handler does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
63
        }
64
        if(RouteHandlerResolver::getInstance($this->handler)->isValid($this) === false) {
0 ignored issues
show
Documentation introduced by
The property handler does not exist on object<ElementsFramework...ing\Model\DynamicRoute>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
65
            throw HandlerValidationFailedForRouteException::fromRoute($this);
66
        }
67
68
        return parent::save($options);
69
    }
70
71
}