Moment::target()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
# == Schema Information
13
#
14
# Table name: moments
15
#
16
#  id          :integer          not null, primary key
17
#  target_type :string(255)
18
#  target_id   :integer
19
#  title       :string(255)
20
#  data        :text
21
#  project_id  :integer
22
#  created_at  :timestamp
23
#  updated_at  :timestamp
24
#  action      :integer
25
#  author_id   :integer
26
#
27
28
namespace Gitamin\Models;
29
30
use Gitamin\Presenters\MomentPresenter;
31
use Illuminate\Database\Eloquent\Builder;
32
use Illuminate\Database\Eloquent\Model;
33
use McCool\LaravelAutoPresenter\HasPresenter;
34
35
class Moment extends Model implements HasPresenter
36
{
37
    const CREATED = 1;
38
    const UPDATED = 2;
39
    const CLOSED = 3;
40
    const REOPENED = 4;
41
    const PUSHED = 5;
42
    const COMMENTED = 6;
43
    const MERGED = 7;
44
    const JOINED = 8; # User joined project
45
    const LEFT = 9; # User left project
46
    const DESTROYED = 10;
47
48
    /**
49
     * The attributes that should be casted to native types.
50
     *
51
     * @var string[]
52
     */
53
    protected $casts = [
54
        'id' => 'int',
55
        'deleted_at' => 'date',
56
    ];
57
58
    /**
59
     * The fillable properties.
60
     *
61
     * @var string[]
62
     */
63
    protected $fillable = [
64
        'target_type',
65
        'target_id',
66
        'action',
67
        'author_id',
68
        'project_id',
69
        'title',
70
        'data',
71
        'created_at',
72
        'updated_at',
73
    ];
74
75
    /**
76
     * The validation rules.
77
     *
78
     * @var string[]
79
     */
80
    public $rules = [
81
        'target_type' => 'string',
82
        'target_id' => 'int',
83
        'action' => 'int',
84
        'author_id' => 'int',
85
        'project_id' => 'int',
86
        'title' => 'string',
87
        'data' => 'string',
88
    ];
89
90
    /**
91
     * Finds all rencent moments.
92
     *
93
     * @param \Illuminate\Database\Eloquent\Builder $query
94
     *
95
     * @return \Illuminate\Database\Eloquent\Builder
96
     */
97
    public function scopeRecent(Builder $query)
98
    {
99
        return $query->orderBy('id', 'desc');
100
    }
101
102
    /**
103
     * Finds all code push moments.
104
     *
105
     * @param \Illuminate\Database\Eloquent\Builder $query
106
     *
107
     * @return \Illuminate\Database\Eloquent\Builder
108
     */
109
    public function scopeCodePush(Builder $query)
110
    {
111
        return $query->where('action', '=', self::PUSHED);
112
    }
113
114
    /**
115
     * A moment belongs to a project.
116
     *
117
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
118
     */
119
    public function project()
120
    {
121
        return $this->belongsTo(Project::class, 'project_id', 'id');
122
    }
123
124
    /**
125
     * Moments can belong to an author.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
128
     */
129
    public function author()
130
    {
131
        return $this->belongsTo(User::class, 'author_id', 'id');
132
    }
133
134
    /**
135
     * Moments can belong to a target.
136
     *
137
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
138
     */
139
    public function target()
140
    {
141
        return $this->belongsTo('Gitamin\\Models\\'.$this->target_type, 'target_id', 'id');
0 ignored issues
show
Documentation introduced by
The property target_type does not exist on object<Gitamin\Models\Moment>. 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...
142
    }
143
144
    /**
145
     * Get the presenter class.
146
     *
147
     * @return string
148
     */
149
    public function getPresenterClass()
150
    {
151
        return MomentPresenter::class;
152
    }
153
}
154