Completed
Pull Request — master (#63)
by Phecho
04:21
created

Moment::author()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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\IssuePresenter;
31
use Gitamin\Presenters\MomentPresenter;
32
use Illuminate\Database\Eloquent\Builder;
33
use Illuminate\Database\Eloquent\Model;
34
use McCool\LaravelAutoPresenter\HasPresenter;
35
36
class Moment extends Model implements HasPresenter
37
{
38
    const CREATED = 1;
39
    const UPDATED = 2;
40
    const CLOSED = 3;
41
    const REOPENED = 4;
42
    const PUSHED = 5;
43
    const COMMENTED = 6;
44
    const MERGED = 7;
45
    const JOINED = 8; # User joined project
46
    const LEFT = 9; # User left project
47
    const DESTROYED = 10;
48
49
    /**
50
     * The attributes that should be casted to native types.
51
     *
52
     * @var string[]
53
     */
54
    protected $casts = [
55
        'id'         => 'int',
56
        'deleted_at' => 'date',
57
    ];
58
59
    /**
60
     * The fillable properties.
61
     *
62
     * @var string[]
63
     */
64
    protected $fillable = [
65
        'target_type',
66
        'target_id',
67
        'action',
68
        'author_id',
69
        'project_id',
70
        'title',
71
        'data',
72
        'created_at',
73
        'updated_at',
74
    ];
75
76
    /**
77
     * The validation rules.
78
     *
79
     * @var string[]
80
     */
81
    public $rules = [
82
        'target_type' => 'string',
83
        'target_id'   => 'int',
84
        'action'      => 'int',
85
        'author_id'   => 'int',
86
        'project_id'  => 'int',
87
        'title'       => 'string',
88
        'data'        => 'string',
89
    ];
90
91
    /**
92
     * Finds all rencent moments.
93
     *
94
     * @param \Illuminate\Database\Eloquent\Builder $query
95
     *
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     */
98
    public function scopeRecent(Builder $query)
99
    {
100
        return $query->orderBy('id', 'desc');
101
    }
102
103
    /**
104
     * Finds all code push moments.
105
     *
106
     * @param \Illuminate\Database\Eloquent\Builder $query
107
     *
108
     * @return \Illuminate\Database\Eloquent\Builder
109
     */
110
    public function scopeCodePush(Builder $query)
111
    {
112
        return $query->where('action', '=', self::PUSHED);
113
    }
114
115
    /**
116
     * A moment belongs to a project.
117
     *
118
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
119
     */
120
    public function project()
121
    {
122
        return $this->belongsTo(Project::class, 'project_id', 'id');
123
    }
124
125
    /**
126
     * Moments can belong to an author.
127
     *
128
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
129
     */
130
    public function author()
131
    {
132
        return $this->belongsTo(User::class, 'author_id', 'id');
133
    }
134
135
    /**
136
     * Moments can belong to a target
137
     *
138
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
139
     */
140
    public function target()
141
    {
142
        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...
143
    }
144
145
    /**
146
     * Get the presenter class.
147
     *
148
     * @return string
149
     */
150
    public function getPresenterClass()
151
    {
152
        return MomentPresenter::class;
153
    }
154
}
155