Completed
Pull Request — master (#45)
by Phecho
04:16
created

Project::findByPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 3
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             :datetime
21
#  updated_at             :datetime
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       :datetime
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
     * Projects can belong to an owner.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
125
     */
126
    public function projectOwner()
127
    {
128
        return $this->belongsTo(Owner::class, 'owner_id', 'id');
129
    }
130
131
    /**
132
     * Lookup all of the issues reported on the project.
133
     *
134
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
135
     */
136
    public function issues()
137
    {
138
        return $this->hasMany(Issue::class, 'project_id', 'id');
139
    }
140
141
    /**
142
     * Projects can have many tags.
143
     *
144
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
145
     */
146
    public function tags()
147
    {
148
        return $this->belongsToMany(Tag::class);
149
    }
150
151
    /**
152
     * Find by path, or throw an exception.
153
     *
154
     * @param string   $owner_path
155
     * @param string   $project_path
156
     * @param string[] $columns
157
     *
158
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
159
     *
160
     * @return \Gitamin\Models\User
161
     */
162
    public static function findByPath($owner_path, $project_path, $columns = ['projects.*'])
163
    {
164
        $project = static::leftJoin('owners', function ($join) {
165
            $join->on('projects.owner_id', '=', 'owners.id');
166
        })->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner_path)->first($columns);
167
168
        if (!$project) {
169
            throw new ModelNotFoundException();
170
        }
171
172
        return $project;
173
    }
174
175
    /**
176
     * Finds all projects by visibility_level.
177
     *
178
     * @param \Illuminate\Database\Eloquent\Builder $query
179
     * @param int                                   $visibility_level
180
     *
181
     * @return \Illuminate\Database\Eloquent\Builder
182
     */
183
    public function scopeVisibilityLevel(Builder $query, $visibility_level)
184
    {
185
        return $query->where('visibility_level', $visibility_level);
186
    }
187
188
    /**
189
     * Finds all projects which don't have the given visibility_level.
190
     *
191
     * @param \Illuminate\Database\Eloquent\Builder $query
192
     * @param int                                   $visibility_level
193
     *
194
     * @return \Illuminate\Database\Eloquent\Builder
195
     */
196
    public function scopeNotVisibilityLevel(Builder $query, $visibility_level)
197
    {
198
        return $query->where('visibility_level', '<>', $visibility_level);
199
    }
200
201
    /**
202
     * Looks up the human readable version of the visibility_level.
203
     *
204
     * @return string
205
     */
206
    public function getHumanVisibilityLevelAttribute()
207
    {
208
        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...
209
    }
210
211
    /**
212
     * Returns all of the tags on this project.
213
     *
214
     * @return string
215
     */
216
    public function getTagsListAttribute()
217
    {
218
        $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...
219
            return $tag->name;
220
        });
221
222
        return implode(', ', $tags->toArray());
223
    }
224
225
    /**
226
     * Get the presenter class.
227
     *
228
     * @return string
229
     */
230
    public function getPresenterClass()
231
    {
232
        return ProjectPresenter::class;
233
    }
234
}
235