Version   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A versionable() 0 4 1
A getResponsibleUserAttribute() 0 5 1
A getModel() 0 9 1
A revert() 0 11 2
1
<?php
2
namespace Mpociot\Versionable;
3
4
use Illuminate\Support\Facades\Config;
5
use Illuminate\Database\Eloquent\Model as Eloquent;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Class Version
10
 * @package Mpociot\Versionable
11
 */
12
class Version extends Eloquent
13
{
14
15
    /**
16
     * @var string
17
     */
18
    public $table = "versions";
19
20
    /**
21
     * @var string
22
     */
23
    protected $primaryKey = "version_id";
24
25
    /**
26
     * Sets up the relation
27
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
28
     */
29
    public function versionable()
30
    {
31
        return $this->morphTo();
32
    }
33
34
    /**
35
     * Return the user responsible for this version
36
     * @return mixed
37
     */
38
    public function getResponsibleUserAttribute()
39
    {
40
        $model = Config::get("auth.model");
41
        return $model::find($this->user_id);
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Mpociot\Versionable\Version>. 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...
42
    }
43
44
    /**
45
     * Return the versioned model
46
     * @return Model
47
     */
48
    public function getModel()
49
    {
50
        $model = new $this->versionable_type();
0 ignored issues
show
Documentation introduced by
The property versionable_type does not exist on object<Mpociot\Versionable\Version>. 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...
51
        $model->unguard();
52
        $model->fill(unserialize($this->model_data));
0 ignored issues
show
Documentation introduced by
The property model_data does not exist on object<Mpociot\Versionable\Version>. 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...
53
        $model->exists = true;
54
        $model->reguard();
55
        return $model;
56
    }
57
58
59
    /**
60
     * Revert to the stored model version make it the current version
61
     *
62
     * @return Model
63
     */
64
    public function revert()
65
    {
66
        $model = $this->getModel();
67
        unset( $model->{$model->getCreatedAtColumn()} );
68
        unset( $model->{$model->getUpdatedAtColumn()} );
69
        if (method_exists($model, 'getDeletedAtColumn')) {
70
            unset( $model->{$model->getDeletedAtColumn()} );
71
        }
72
        $model->save();
73
        return $model;
74
    }
75
76
}