Completed
Push — master ( 307d8a...2c7bf4 )
by Phecho
05:31
created

Owner::project()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
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: owners
15
#
16
#  id          :integer          not null, primary key
17
#  name        :string(255)      not null
18
#  path        :string(255)      not null
19
#  user_id     :integer
20
#  created_at  :timestamp
21
#  updated_at  :timestamp
22
#  type        :string(255)
23
#  description :string(255)      default(""), not null
24
#  avatar      :string(255)
25
#  public      :boolean          default(FALSE)
26
#
27
28
namespace Gitamin\Models;
29
30
use AltThree\Validator\ValidatingTrait;
31
use Illuminate\Database\Eloquent\Model;
32
use Illuminate\Support\Facades\Auth;
33
34
class Owner extends Model
35
{
36
    use ValidatingTrait;
37
38
    /**
39
     * The attributes that should be casted to native types.
40
     *
41
     * @var string[]
42
     */
43
    protected $casts = [
44
        'id' => 'int',
45
        'name' => 'string',
46
        'path' => 'string',
47
        'user_id' => 'int',
48
        'type' => 'string',
49
    ];
50
51
    /**
52
     * The fillable properties.
53
     *
54
     * @var string[]
55
     */
56
    protected $fillable = [
57
        'name',
58
        'path',
59
        'user_id',
60
        'description',
61
        'type',
62
    ];
63
64
    /**
65
     * The validation rules.
66
     *
67
     * @var string[]
68
     */
69
    public $rules = [
70
        'name' => 'required|string',
71
        'path' => 'required|string|max:15',
72
        'user_id' => 'int',
73
        'type' => 'string',
74
        'description' => 'string',
75
    ];
76
77
    /**
78
     * A owner can have many projects.
79
     *
80
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
81
     */
82
    public function projects()
83
    {
84
        return $this->hasMany(Project::class, 'owner_id', 'id');
85
    }
86
87
    /**
88
     * Find project under this owner by path, or throw an exception.
89
     *
90
     * @param string   $path
91
     * @param string[] $columns
92
     *
93
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
94
     *
95
     * @return \Gitamin\Models\User
96
     */
97
    public function project($path, $columns = ['*'])
98
    {
99
        $project = Project::where('owner_id', '=', $this->id)->where('path', '=', $path)->first($columns);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Gitamin\Models\Owner>. 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...
100
101
        if (! $project) {
102
            throw new ModelNotFoundException();
103
        }
104
105
        return $project;
106
    }
107
108
    /**
109
     * Find by path, or throw an exception.
110
     *
111
     * @param string   $path
112
     * @param string[] $columns
113
     *
114
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
115
     *
116
     * @return \Gitamin\Models\User
117
     */
118
    public static function findByPath($path, $columns = ['*'])
119
    {
120
        $owner = static::where('path', $path)->first($columns);
121
122
        if (! $owner) {
123
            throw new ModelNotFoundException();
124
        }
125
126
        return $owner;
127
    }
128
129
    /**
130
     * Finds all my owners.
131
     *
132
     * @param \Illuminate\Database\Eloquent\Builder $query
133
     *
134
     * @return \Illuminate\Database\Eloquent\Builder
135
     */
136
    public function scopeMine($query)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        //return $query->where('user_id', Auth::user()->id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
139
    }
140
}
141