Completed
Push — master ( 2ad194...7cf0c0 )
by Phecho
06:25 queued 03:11
created

Project::group()   A

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
namespace Gitamin\Models;
13
14
use AltThree\Validator\ValidatingTrait;
15
use Gitamin\Presenters\ProjectPresenter;
16
use Illuminate\Database\Eloquent\Builder;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Database\Eloquent\SoftDeletes;
19
use McCool\LaravelAutoPresenter\HasPresenter;
20
21
class Project extends Model implements HasPresenter
22
{
23
    use SoftDeletes, ValidatingTrait;
24
25
    /**
26
     * List of attributes that have default values.
27
     *
28
     * @var mixed[]
29
     */
30
    protected $attributes = [
31
        'namespace_id' => 0,
32
        'description'  => '',
33
        'path'         => '',
34
        'creator_id'   => 0,
35
    ];
36
37
    /**
38
     * The attributes that should be casted to native types.
39
     *
40
     * @var string[]
41
     */
42
    protected $casts = [
43
        'id'           => 'int',
44
        'namespace_id' => 'int',
45
        'description'  => 'string',
46
        'path'         => 'string',
47
        'creator_id'   => 'int',
48
        'deleted_at'   => 'date',
49
    ];
50
51
    /**
52
     * The fillable properties.
53
     *
54
     * @var string[]
55
     */
56
    protected $fillable = [
57
        'name',
58
        'description',
59
        'visibility_level',
60
        'tags',
61
        'path',
62
        'creator_id',
63
        'namespace_id',
64
    ];
65
66
    /**
67
     * The validation rules.
68
     *
69
     * @var string[]
70
     */
71
    public $rules = [
72
        'name'             => 'required|string',
73
        'visibility_level' => 'int|required',
74
        'path'             => 'required|string|max:15',
75
    ];
76
77
    /**
78
     * Projects can belong to a group.
79
     *
80
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
81
     */
82
    public function group()
83
    {
84
        return $this->belongsTo(Group::class, 'namespace_id', 'id');
85
    }
86
87
     /**
88
     * Projects can belong to a namespace.
89
     *
90
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
91
     */
92
    public function projectNamespace()
93
    {
94
        return $this->belongsTo(ProjectNamespace::class, 'namespace_id', 'id');
95
    }
96
97
    /**
98
     * Lookup all of the issues reported on the project.
99
     *
100
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
101
     */
102
    public function issues()
103
    {
104
        return $this->hasMany(Issue::class, 'project_id', 'id');
105
    }
106
107
    /**
108
     * Projects can have many tags.
109
     *
110
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
111
     */
112
    public function tags()
113
    {
114
        return $this->belongsToMany(Tag::class);
115
    }
116
117
    /**
118
     * Finds all projects by visibility_level.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $query
121
     * @param int                                   $visibility_level
122
     *
123
     * @return \Illuminate\Database\Eloquent\Builder
124
     */
125
    public function scopeVisibilityLevel(Builder $query, $visibility_level)
126
    {
127
        return $query->where('visibility_level', $visibility_level);
128
    }
129
130
    /**
131
     * Finds all projects which don't have the given visibility_level.
132
     *
133
     * @param \Illuminate\Database\Eloquent\Builder $query
134
     * @param int                                   $visibility_level
135
     *
136
     * @return \Illuminate\Database\Eloquent\Builder
137
     */
138
    public function scopeNotVisibilityLevel(Builder $query, $visibility_level)
139
    {
140
        return $query->where('visibility_level', '<>', $visibility_level);
141
    }
142
143
    /**
144
     * Looks up the human readable version of the visibility_level.
145
     *
146
     * @return string
147
     */
148
    public function getHumanVisibilityLevelAttribute()
149
    {
150
        return trans('gitamin.projects.status.'.$this->visibility_level);
0 ignored issues
show
Documentation introduced by
The property visibility_level does not exist on object<Gitamin\Models\Project>. 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...
151
    }
152
153
    /**
154
     * Returns the namespace on this project.
155
     *
156
     * @return string
157
     */
158
    public function getNamespaceAttribute()
159
    {
160
        return $this->projectNamespace->path;
0 ignored issues
show
Documentation introduced by
The property projectNamespace does not exist on object<Gitamin\Models\Project>. 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...
161
    }
162
163
    /**
164
     * Returns all of the tags on this project.
165
     *
166
     * @return string
167
     */
168
    public function getTagsListAttribute()
169
    {
170
        $tags = $this->tags->map(function ($tag) {
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Gitamin\Models\Project>. 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...
171
            return $tag->name;
172
        });
173
174
        return implode(', ', $tags->toArray());
175
    }
176
177
    /**
178
     * Get the presenter class.
179
     *
180
     * @return string
181
     */
182
    public function getPresenterClass()
183
    {
184
        return ProjectPresenter::class;
185
    }
186
}
187