Completed
Push — master ( 28f481...c1dd49 )
by Freek
70:06 queued 55:46
created

Activity::getChangesAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Models;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Relations\MorphTo;
9
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
10
11
class Activity extends Model implements ActivityContract
12
{
13
    public $guarded = [];
14
15
    protected $casts = [
16
        'properties' => 'collection',
17
    ];
18
19
    public function __construct(array $attributes = [])
20
    {
21
        if (! isset($this->table)) {
22
            $this->setTable(config('activitylog.table_name'));
23
        }
24
25
        parent::__construct($attributes);
26
    }
27
28
    public function subject(): MorphTo
29
    {
30
        if (config('activitylog.subject_returns_soft_deleted_models')) {
31
            return $this->morphTo()->withTrashed();
32
        }
33
34
        return $this->morphTo();
35
    }
36
37
    public function causer(): MorphTo
38
    {
39
        return $this->morphTo();
40
    }
41
42
    public function getExtraProperty(string $propertyName)
43
    {
44
        return array_get($this->properties->toArray(), $propertyName);
0 ignored issues
show
Documentation introduced by
The property properties does not exist on object<Spatie\Activitylog\Models\Activity>. 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...
45
    }
46
47
    public function changes(): Collection
48
    {
49
        if (! $this->properties instanceof Collection) {
0 ignored issues
show
Documentation introduced by
The property properties does not exist on object<Spatie\Activitylog\Models\Activity>. 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...
50
            return new Collection();
51
        }
52
53
        return $this->properties->only(['attributes', 'old']);
0 ignored issues
show
Documentation introduced by
The property properties does not exist on object<Spatie\Activitylog\Models\Activity>. 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...
54
    }
55
56
    public function getChangesAttribute(): Collection
57
    {
58
        return $this->changes();
59
    }
60
61
    public function scopeInLog(Builder $query, ...$logNames): Builder
62
    {
63
        if (is_array($logNames[0])) {
64
            $logNames = $logNames[0];
65
        }
66
67
        return $query->whereIn('log_name', $logNames);
68
    }
69
70
    public function scopeCausedBy(Builder $query, Model $causer): Builder
71
    {
72
        return $query
73
            ->where('causer_type', $causer->getMorphClass())
74
            ->where('causer_id', $causer->getKey());
75
    }
76
77
    public function scopeForSubject(Builder $query, Model $subject): Builder
78
    {
79
        return $query
80
            ->where('subject_type', $subject->getMorphClass())
81
            ->where('subject_id', $subject->getKey());
82
    }
83
}
84