Completed
Push — dev ( ace4ec...6a93ce )
by Zach
03:38
created

Page::hasRelationshipNamed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Models;
4
5
use Larafolio\Helpers\Sluggable;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class Page extends HasContent
9
{
10
    use Sluggable, SoftDeletes;
11
12
    /**
13
     * The table associated with the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'pages';
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'name', 'slug', 'visible', 'order',
26
    ];
27
28
    /**
29
     * Properties to always eager load.
30
     *
31
     * @var array
32
     */
33
    protected $with = ['blocks', 'images', 'links'];
34
35
    /**
36
     * The attributes that should be casted to native types.
37
     *
38
     * @var array
39
     */
40
    protected $casts = [
41
        'visible' => 'boolean',
42
    ];
43
44
    /**
45
     * Fields that are dates.
46
     *
47
     * @var array
48
     */
49
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
50
51
    /**
52
     * Get the route key for the model.
53
     *
54
     * @return string
55
     */
56
    public function getRouteKeyName()
57
    {
58
        return 'slug';
59
    }
60
61
    /**
62
     * Return all visible pages.
63
     *
64
     * @param bool $order If true, order pages by 'order'.
65
     *
66
     * @return \Illuminate\Support\Collection
67
     */
68
    public static function allVisible($order = true)
69
    {
70
        $query = static::where('visible', true);
71
72
        return static::orderAndGroupQuery($query, false, $order);
73
    }
74
75
    /**
76
     * Return all hidden pages.
77
     *
78
     * @param bool $order If true, order pages by 'order'.
79
     *
80
     * @return \Illuminate\Support\Collection
81
     */
82
    public static function allHidden($order = true)
83
    {
84
        $query = static::where('visible', false);
85
86
        return static::orderAndGroupQuery($query, false, $order);
87
    }
88
89
    /**
90
     * Return all pages ordered by 'order'.
91
     *
92
     * @return \Illuminate\Support\Collection
93
     */
94
    public static function allOrdered()
95
    {
96
        $query = static::query();
97
98
        return static::orderAndGroupQuery($query, false, true);
99
    }
100
101
    /**
102
     * Get all pages with given block name.
103
     *
104
     * @param string $blockName Name of block.
105
     *
106
     * @return \Illuminate\Support\Collection
107
     */
108
    public static function hasBlockNamed($blockName)
109
    {
110
        return static::hasRelationshipNamed(Page::class, 'pages', 'text_blocks', $blockName);
111
    }
112
113
    /**
114
     * Get all pages with given image name.
115
     *
116
     * @param string $imageName Name of image.
117
     *
118
     * @return \Illuminate\Support\Collection
119
     */
120
    public static function hasImageNamed($imageName)
121
    {
122
        return static::hasRelationshipNamed(Page::class, 'pages', 'images', $imageName);
123
    }
124
125
    /**
126
     * Get all pages with given link name.
127
     *
128
     * @param string $linkName Name of link.
129
     *
130
     * @return \Illuminate\Support\Collection
131
     */
132
    public static function hasLinkNamed($linkName)
133
    {
134
        return static::hasRelationshipNamed(Page::class, 'pages', 'links', $linkName);
135
    }
136
137
    /**
138
     * Return the page id.
139
     *
140
     * @return int
141
     */
142
    public function id()
143
    {
144
        return $this->id;
1 ignored issue
show
Documentation introduced by
The property id does not exist on object<Larafolio\Models\Page>. 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...
145
    }
146
147
    /**
148
     * Return the page name.
149
     *
150
     * @return string
151
     */
152
    public function name()
153
    {
154
        return $this->name;
1 ignored issue
show
Documentation introduced by
The property name does not exist on object<Larafolio\Models\Page>. 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...
155
    }
156
157
    /**
158
     * Return the page slug.
159
     *
160
     * @return string
161
     */
162
    public function slug()
163
    {
164
        return $this->slug;
1 ignored issue
show
Documentation introduced by
The property slug does not exist on object<Larafolio\Models\Page>. 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...
165
    }
166
167
    /**
168
     * Return the page order value.
169
     *
170
     * @return int
171
     */
172
    public function order()
173
    {
174
        return $this->order;
1 ignored issue
show
Documentation introduced by
The property order does not exist on object<Larafolio\Models\Page>. 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...
175
    }
176
}
177