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

Project::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Project::allVisible() 0 6 1
1
<?php
2
3
namespace Larafolio\Models;
4
5
use Larafolio\Helpers\Sluggable;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
class Project extends HasContent
9
{
10
    use Sluggable, SoftDeletes;
11
12
    /**
13
     * The table associated with the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'projects';
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'name', 'slug', 'type', '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 projects.
63
     *
64
     * @param bool $group If true, group projects by 'type'.
65
     * @param bool $order If true, order projects by 'order'.
66
     *
67
     * @return \Illuminate\Support\Collection
68
     */
69
    public static function allVisible($group = true, $order = true)
70
    {
71
        $query = static::where('visible', true);
72
73
        return static::orderAndGroupQuery($query, $group, $order);
74
    }
75
76
    /**
77
     * Return all hidden projects.
78
     *
79
     * @param bool $group If true, group projects by 'type'.
80
     * @param bool $order If true, order projects by 'order'.
81
     *
82
     * @return \Illuminate\Support\Collection
83
     */
84
    public static function allHidden($group = true, $order = true)
85
    {
86
        $query = static::where('visible', false);
87
88
        return static::orderAndGroupQuery($query, $group, $order);
89
    }
90
91
    /**
92
     * Return all projects grouped by 'type'.
93
     *
94
     * @param bool $order If true, order projects by 'order'.
95
     *
96
     * @return \Illuminate\Support\Collection
97
     */
98
    public static function allGrouped($order = true)
99
    {
100
        $query = static::query();
101
102
        return static::orderAndGroupQuery($query, true, $order);
103
    }
104
105
    /**
106
     * Return all projects ordered by 'order'.
107
     *
108
     * @return \Illuminate\Support\Collection
109
     */
110
    public static function allOrdered()
111
    {
112
        $query = static::query();
113
114
        return static::orderAndGroupQuery($query, false, true);
115
    }
116
117
    /**
118
     * Get all projects with given block name.
119
     *
120
     * @param string $blockName Name of block.
121
     *
122
     * @return \Illuminate\Support\Collection
123
     */
124
    public static function hasBlockNamed($blockName)
125
    {
126
        return static::hasRelationshipNamed(Project::class, 'projects', 'text_blocks', $blockName);
127
    }
128
129
    /**
130
     * Get all projects with given image name.
131
     *
132
     * @param string $imageName Name of image.
133
     *
134
     * @return \Illuminate\Support\Collection
135
     */
136
    public static function hasImageNamed($imageName)
137
    {
138
        return static::hasRelationshipNamed(Project::class, 'projects', 'images', $imageName);
139
    }
140
141
    /**
142
     * Get all projects with given link name.
143
     *
144
     * @param string $linkName Name of link.
145
     *
146
     * @return \Illuminate\Support\Collection
147
     */
148
    public static function hasLinkNamed($linkName)
149
    {
150
        return static::hasRelationshipNamed(Project::class, 'projects', 'links', $linkName);
151
    }
152
153
    /**
154
     * Return the project id.
155
     *
156
     * @return int
157
     */
158
    public function id()
159
    {
160
        return $this->id;
1 ignored issue
show
Documentation introduced by
The property id does not exist on object<Larafolio\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...
161
    }
162
163
    /**
164
     * Return the project name.
165
     *
166
     * @return string
167
     */
168
    public function name()
169
    {
170
        return $this->name;
1 ignored issue
show
Documentation introduced by
The property name does not exist on object<Larafolio\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...
171
    }
172
173
    /**
174
     * Return the project type.
175
     *
176
     * @return string
177
     */
178
    public function type()
179
    {
180
        return $this->type;
1 ignored issue
show
Documentation introduced by
The property type does not exist on object<Larafolio\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...
181
    }
182
183
    /**
184
     * Return the project slug.
185
     *
186
     * @return string
187
     */
188
    public function slug()
189
    {
190
        return $this->slug;
1 ignored issue
show
Documentation introduced by
The property slug does not exist on object<Larafolio\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...
191
    }
192
193
    /**
194
     * Return the project order value.
195
     *
196
     * @return int
197
     */
198
    public function order()
199
    {
200
        return $this->order;
1 ignored issue
show
Documentation introduced by
The property order does not exist on object<Larafolio\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...
201
    }
202
203
    /**
204
     * Get formatted text of block with project name or first block.
205
     *
206
     * @return Larafolio\Models\TextBlock
207
     */
208
    public function getProjectBlock()
209
    {
210
        $block = $this->block($this->name());
211
212
        if ($block) {
213
            return $block;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $block; (Illuminate\Database\Eloquent\Model) is incompatible with the return type documented by Larafolio\Models\Project::getProjectBlock of type Larafolio\Models\Larafolio\Models\TextBlock.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
214
        } elseif ($this->hasBlocks()) {
215
            return $this->blocks()->first();
216
        }
217
    }
218
219
    /**
220
     * Get formatted text of block with project name or first block.
221
     *
222
     * @param bool $formatted If true, return formatted text.
223
     *
224
     * @return string
225
     */
226
    public function getProjectBlockText($formatted = true)
227
    {
228
        $block = $this->getProjectBlock();
229
230
        if ($block && $formatted) {
231
            return $block->formattedText();
232
        } elseif ($block) {
233
            return $block->text();
234
        }
235
236
        return $block;
237
    }
238
239
    /**
240
     * Get url of small image with project name or first image in collection.
241
     *
242
     * @return Larafolio\Models\Image
243
     */
244
    public function getProjectImage()
245
    {
246
        $projectImage = $this->image($this->name());
247
248
        if ($projectImage) {
249
            return $projectImage;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $projectImage; (Illuminate\Database\Eloquent\Model) is incompatible with the return type documented by Larafolio\Models\Project::getProjectImage of type Larafolio\Models\Larafolio\Models\Image.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
250
        } elseif ($this->hasImages()) {
251
            return $this->images()->first();
252
        }
253
    }
254
255
    /**
256
     * Get url of small image with project name or first image in collection.
257
     *
258
     * @param string $size The size of the image, name of image cache filter.
259
     *
260
     * @return string
261
     */
262
    public function getProjectImageUrl($size = 'small')
263
    {
264
        $projectImage = $this->getProjectImage();
265
266
        if ($projectImage) {
267
            return $projectImage->{$size}();
268
        }
269
270
        return $projectImage;
271
    }
272
}
273