Completed
Push — dev ( 1b2c72...d03be0 )
by Zach
03:53
created

Page::generateProps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
nc 1
dl 9
loc 9
c 0
b 0
f 0
cc 1
eloc 6
nop 0
rs 9.6666
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('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('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('links', $linkName);
135
    }
136
137
    /**
138
     * Get all pages with relationship on table that has given name.
139
     *
140
     * @param string $table Name of table relationship is on.
141
     * @param string $name  Relationship name.
142
     *
143
     * @return \Illuminate\Support\Collection
144
     */
145
    protected static function hasRelationshipNamed($table, $name)
146
    {
147
        return static::join($table, 'pages.id', '=', "{$table}.resource_id")
148
            ->where("{$table}.name", '=', $name)
149
            ->where("{$table}.resource_type", '=', Page::class)
150
            ->select('pages.*')
151
            ->get();
152
    }
153
154
    /**
155
     * Return the page id.
156
     *
157
     * @return int
158
     */
159
    public function id()
160
    {
161
        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...
162
    }
163
164
    /**
165
     * Return the page name.
166
     *
167
     * @return string
168
     */
169
    public function name()
170
    {
171
        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...
172
    }
173
174
    /**
175
     * Return the page slug.
176
     *
177
     * @return string
178
     */
179
    public function slug()
180
    {
181
        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...
182
    }
183
184
    /**
185
     * Return the page order value.
186
     *
187
     * @return int
188
     */
189
    public function order()
190
    {
191
        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...
192
    }
193
}
194