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

Project::scopeFull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
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 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
     * Bootstrap model.
63
     */
64 View Code Duplication
    public static function boot()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        parent::boot();
67
68
        static::creating(function (Project $project) {
69
            $project->setSlug('name');
70
        });
71
72
        static::updating(function (Project $project) {
73
            $project->setSlug('name');
74
        });
75
    }
76
77
    /**
78
     * Return all visible projects.
79
     *
80
     * @param bool $group If true, group projects by 'type'.
81
     * @param bool $order If true, order projects by 'order'.
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85
    public static function allVisible($group = true, $order = true)
86
    {
87
        $query = static::where('visible', true);
88
89
        return static::orderAndGroupQuery($query, $group, $order);
90
    }
91
92
    /**
93
     * Return all hidden projects.
94
     *
95
     * @param bool $group If true, group projects by 'type'.
96
     * @param bool $order If true, order projects by 'order'.
97
     *
98
     * @return \Illuminate\Support\Collection
99
     */
100
    public static function allHidden($group = true, $order = true)
101
    {
102
        $query = static::where('visible', false);
103
104
        return static::orderAndGroupQuery($query, $group, $order);
105
    }
106
107
    /**
108
     * Return all projects grouped by 'type'.
109
     *
110
     * @param bool $order If true, order projects by 'order'.
111
     *
112
     * @return \Illuminate\Support\Collection
113
     */
114
    public static function allGrouped($order = true)
115
    {
116
        $query = static::query();
117
118
        return static::orderAndGroupQuery($query, true, $order);
119
    }
120
121
    /**
122
     * Return all projects ordered by 'order'.
123
     *
124
     * @return \Illuminate\Support\Collection
125
     */
126
    public static function allOrdered()
127
    {
128
        $query = static::query();
129
130
        return static::orderAndGroupQuery($query, false, true);
131
    }
132
133
    /**
134
     * Get all projects with given block name.
135
     *
136
     * @param string $blockName Name of block.
137
     *
138
     * @return \Illuminate\Support\Collection
139
     */
140
    public static function hasBlockNamed($blockName)
141
    {
142
        return static::hasRelationshipNamed('text_blocks', $blockName);
143
    }
144
145
    /**
146
     * Get all projects with given image name.
147
     *
148
     * @param string $imageName Name of image.
149
     *
150
     * @return \Illuminate\Support\Collection
151
     */
152
    public static function hasImageNamed($imageName)
153
    {
154
        return static::hasRelationshipNamed('images', $imageName);
155
    }
156
157
    /**
158
     * Get all projects with given link name.
159
     *
160
     * @param string $linkName Name of link.
161
     *
162
     * @return \Illuminate\Support\Collection
163
     */
164
    public static function hasLinkNamed($linkName)
165
    {
166
        return static::hasRelationshipNamed('links', $linkName);
167
    }
168
169
    /**
170
     * Get all projects with relationship on table that has given name.
171
     *
172
     * @param string $table Name of table relationship is on.
173
     * @param string $name  Relationship name.
174
     *
175
     * @return \Illuminate\Support\Collection
176
     */
177
    protected static function hasRelationshipNamed($table, $name)
178
    {
179
        return static::join($table, 'projects.id', '=', "{$table}.resource_id")
180
            ->where("{$table}.name", '=', $name)
181
            ->where("{$table}.resource_type", '=', Project::class)
182
            ->select('projects.*')
183
            ->get();
184
    }
185
186
    /**
187
     * Return the project id.
188
     *
189
     * @return int
190
     */
191
    public function id()
192
    {
193
        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...
194
    }
195
196
    /**
197
     * Return the project name.
198
     *
199
     * @return string
200
     */
201
    public function name()
202
    {
203
        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...
204
    }
205
206
    /**
207
     * Return the project type.
208
     *
209
     * @return string
210
     */
211
    public function type()
212
    {
213
        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...
214
    }
215
216
    /**
217
     * Return the project slug.
218
     *
219
     * @return string
220
     */
221
    public function slug()
222
    {
223
        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...
224
    }
225
226
    /**
227
     * Return the project order value.
228
     *
229
     * @return int
230
     */
231
    public function order()
232
    {
233
        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...
234
    }
235
236
    /**
237
     * Get formatted text of block with project name or first block.
238
     *
239
     * @return Larafolio\Models\TextBlock
240
     */
241
    public function getProjectBlock()
242
    {
243
        $block = $this->block($this->name());
244
245
        if ($block) {
246
            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...
247
        } elseif ($this->hasBlocks()) {
248
            return $this->blocks()->first();
249
        }
250
    }
251
252
    /**
253
     * Get formatted text of block with project name or first block.
254
     *
255
     * @param bool $formatted If true, return formatted text.
256
     *
257
     * @return string
258
     */
259
    public function getProjectBlockText($formatted = true)
260
    {
261
        $block = $this->getProjectBlock();
262
263
        if ($block && $formatted) {
264
            return $block->formattedText();
265
        } elseif ($block) {
266
            return $block->text();
267
        }
268
269
        return $block;
270
    }
271
272
    /**
273
     * Get url of small image with project name or first image in collection.
274
     *
275
     * @return Larafolio\Models\Image
276
     */
277
    public function getProjectImage()
278
    {
279
        $projectImage = $this->image($this->name());
280
281
        if ($projectImage) {
282
            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...
283
        } elseif ($this->hasImages()) {
284
            return $this->images()->first();
285
        }
286
    }
287
288
    /**
289
     * Get url of small image with project name or first image in collection.
290
     *
291
     * @param string $size The size of the image, name of image cache filter.
292
     *
293
     * @return string
294
     */
295
    public function getProjectImageUrl($size = 'small')
296
    {
297
        $projectImage = $this->getProjectImage();
298
299
        if ($projectImage) {
300
            return $projectImage->{$size}();
301
        }
302
303
        return $projectImage;
304
    }
305
}
306