Completed
Pull Request — master (#58)
by Phecho
03:31
created

Project::projectOwner()   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
# == Schema Information
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
13
#
14
# Table name: projects
15
#
16
#  id                     :integer          not null, primary key
17
#  name                   :string(255)
18
#  path                   :string(255)
19
#  description            :text
20
#  created_at             :timestamp
21
#  updated_at             :timestamp
22
#  creator_id             :integer
23
#  issues_enabled         :boolean          default(TRUE), not null
24
#  wall_enabled           :boolean          default(TRUE), not null
25
#  merge_requests_enabled :boolean          default(TRUE), not null
26
#  wiki_enabled           :boolean          default(TRUE), not null
27
#  owner_id               :integer
28
#  issues_tracker         :string(255)      default("gitlab"), not null
29
#  issues_tracker_id      :string(255)
30
#  snippets_enabled       :boolean          default(TRUE), not null
31
#  last_activity_at       :timestamp
32
#  import_url             :string(255)
33
#  visibility_level       :integer          default(0), not null
34
#  archived               :boolean          default(FALSE), not null
35
#  avatar                 :string(255)
36
#  import_status          :string(255)
37
#  repository_size        :float            default(0.0)
38
#  star_count             :integer          default(0), not null
39
#  import_type            :string(255)
40
#  import_source          :string(255)
41
#  commit_count           :integer          default(0)
42
#
43
44
namespace Gitamin\Models;
45
46
use AltThree\Validator\ValidatingTrait;
47
use Gitamin\Presenters\ProjectPresenter;
48
use Illuminate\Database\Eloquent\Builder;
49
use Illuminate\Database\Eloquent\Model;
50
use Illuminate\Database\Eloquent\SoftDeletes;
51
use McCool\LaravelAutoPresenter\HasPresenter;
52
53
class Project extends Model implements HasPresenter
54
{
55
    use SoftDeletes, ValidatingTrait;
56
57
    /**
58
     * List of attributes that have default values.
59
     *
60
     * @var mixed[]
61
     */
62
    protected $attributes = [
63
        'owner_id'    => 0,
64
        'description' => '',
65
        'path'        => '',
66
        'creator_id'  => 0,
67
    ];
68
69
    /**
70
     * The attributes that should be casted to native types.
71
     *
72
     * @var string[]
73
     */
74
    protected $casts = [
75
        'id'             => 'int',
76
        'owner_id'       => 'int',
77
        'description'    => 'string',
78
        'path'           => 'string',
79
        'issues_enabled' => 'boolean',
80
        'creator_id'     => 'int',
81
        'deleted_at'     => 'date',
82
    ];
83
84
    /**
85
     * The fillable properties.
86
     *
87
     * @var string[]
88
     */
89
    protected $fillable = [
90
        'name',
91
        'description',
92
        'visibility_level',
93
        'tags',
94
        'path',
95
        'issues_enabled',
96
        'creator_id',
97
        'owner_id',
98
    ];
99
100
    /**
101
     * The validation rules.
102
     *
103
     * @var string[]
104
     */
105
    public $rules = [
106
        'name'             => 'required|string',
107
        'visibility_level' => 'int|required',
108
        'path'             => 'required|string|max:15',
109
    ];
110
111
    /**
112
     * Projects can belong to a group.
113
     *
114
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
115
     */
116
    public function owner()
117
    {
118
        return $this->belongsTo(Group::class, 'owner_id', 'id');
119
    }
120
121
    /**
122
     * Lookup all of the issues reported on the project.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
125
     */
126
    public function issues()
127
    {
128
        return $this->hasMany(Issue::class, 'project_id', 'id');
129
    }
130
131
    /**
132
     * Projects can have many tags.
133
     *
134
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
135
     */
136
    public function tags()
137
    {
138
        return $this->belongsToMany(Tag::class);
139
    }
140
141
    /**
142
     * Find by path, or throw an exception.
143
     *
144
     * @param string   $owner_path
145
     * @param string   $project_path
146
     * @param string[] $columns
147
     *
148
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
149
     *
150
     * @return \Gitamin\Models\User
151
     */
152
    public static function findByPath($owner_path, $project_path, $columns = ['projects.*'])
153
    {
154
        $project = static::leftJoin('owners', function ($join) {
155
            $join->on('projects.owner_id', '=', 'owners.id');
156
        })->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner_path)->first($columns);
157
158
        if (!$project) {
159
            throw new ModelNotFoundException();
160
        }
161
162
        return $project;
163
    }
164
165
    /**
166
     * Finds all projects by visibility_level.
167
     *
168
     * @param \Illuminate\Database\Eloquent\Builder $query
169
     * @param int                                   $visibility_level
170
     *
171
     * @return \Illuminate\Database\Eloquent\Builder
172
     */
173
    public function scopeVisibilityLevel(Builder $query, $visibility_level)
174
    {
175
        return $query->where('visibility_level', $visibility_level);
176
    }
177
178
    /**
179
     * Finds all projects which don't have the given visibility_level.
180
     *
181
     * @param \Illuminate\Database\Eloquent\Builder $query
182
     * @param int                                   $visibility_level
183
     *
184
     * @return \Illuminate\Database\Eloquent\Builder
185
     */
186
    public function scopeNotVisibilityLevel(Builder $query, $visibility_level)
187
    {
188
        return $query->where('visibility_level', '<>', $visibility_level);
189
    }
190
191
    /**
192
     * Looks up the human readable version of the visibility_level.
193
     *
194
     * @return string
195
     */
196
    public function getHumanVisibilityLevelAttribute()
197
    {
198
        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...
199
    }
200
201
     /**
202
     * Returns project owner path.
203
     *
204
     * @return bool
205
     */
206
    public function getOwnerPathAttribute()
207
    {
208
209
        return $this->owner->path;
0 ignored issues
show
Documentation introduced by
The property owner 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...
210
    }
211
212
    /**
213
     * Returns all of the tags on this project.
214
     *
215
     * @return string
216
     */
217
    public function getTagsListAttribute()
218
    {
219
        $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...
220
            return $tag->name;
221
        });
222
223
        return implode(', ', $tags->toArray());
224
    }
225
226
    /**
227
     * Get the presenter class.
228
     *
229
     * @return string
230
     */
231
    public function getPresenterClass()
232
    {
233
        return ProjectPresenter::class;
234
    }
235
}
236