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

HasContent::generateProps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Models;
4
5
use Larafolio\Helpers\Sluggable;
6
use Larafolio\Models\ContentTraits\HasLinks;
7
use Larafolio\Models\ContentTraits\HasBlocks;
8
use Larafolio\Models\ContentTraits\HasImages;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
12
class HasContent extends Model
13
{
14
    use HasBlocks, HasImages, HasLinks, Sluggable, SoftDeletes;
15
16
    /**
17
     * Properties to always eager load.
18
     *
19
     * @var array
20
     */
21
    protected $with = ['blocks', 'images', 'links'];
22
23
    /**
24
     * The attributes that should be casted to native types.
25
     *
26
     * @var array
27
     */
28
    protected $casts = [
29
        'visible' => 'boolean',
30
    ];
31
32
    /**
33
     * Fields that are dates.
34
     *
35
     * @var array
36
     */
37
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
38
39
    /**
40
     * Get the route key for the model.
41
     *
42
     * @return string
43
     */
44
    public function getRouteKeyName()
45
    {
46
        return 'slug';
47
    }
48
49
    /**
50
     * Bootstrap model.
51
     */
52 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...
53
    {
54
        parent::boot();
55
56
        static::creating(function (Model $model) {
57
            $model->setSlug('name');
58
        });
59
60
        static::updating(function (Model $model) {
61
            $model->setSlug('name');
62
        });
63
    }
64
65
    /**
66
     * Order and group query, return results.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Builder $query Query to be ordered.
69
     * @param bool    $group If true, group projects by 'type'.
70
     * @param bool    $order If true, order projects by 'order'.
71
     *
72
     * @return \Illuminate\Support\Collection
73
     */
74
    protected static function orderAndGroupQuery($query, $group, $order)
75
    {
76
        if ($order) {
77
            $query->orderBy('order');
78
        }
79
80
        $query->orderRelationship('links');
81
82
        $query->orderRelationship('blocks');
83
84
        if ($group) {
85
            return $query->get()
86
                ->each(function (Model $model, $key) {
87
                    $model->index = $key;
88
                })
89
                ->groupBy('type');
90
        }
91
92
        return $query->get();
1 ignored issue
show
Bug Compatibility introduced by
The expression $query->get(); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 92 which is incompatible with the return type documented by Larafolio\Models\HasContent::orderAndGroupQuery of type Illuminate\Support\Collection.
Loading history...
93
    }
94
95
    /**
96
     * Get a model from a relationship by model name.
97
     *
98
     * @param string $relationship Name of relationship.
99
     * @param string $name         Name of model to get.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Model|null
102
     */
103
    protected function getFromRelationshipByName($relationship, $name)
104
    {
105
        $items = $this->{$relationship}->where('name', $name);
106
107
        if ($items->isEmpty()) {
108
            return;
109
        }
110
111
        return $items->first();
112
    }
113
114
    /**
115
     * Get blocks sorted by order.
116
     *
117
     * @param Illuminate\Database\Eloquent\Builder $query Query builder.
118
     * @param string   $slug  Project slug.
119
     *
120
     * @return Illuminate\Database\Eloquent\Builder
121
     */
122
    public function scopeWithBlocks($query, $slug)
123
    {
124
        return $query->orderRelationship('blocks')
125
            ->where('slug', $slug);
126
    }
127
128
    /**
129
     * Get full model info (blocks and links sorted by order).
130
     *
131
     * @param Illuminate\Database\Eloquent\Builder $query Query builder.
132
     * @param string   $slug  Project slug.
133
     *
134
     * @return Illuminate\Database\Eloquent\Builder
135
     */
136
    public function scopeFull($query, $slug)
137
    {
138
        return $query->orderRelationship('blocks')
139
            ->orderRelationship('links')
140
            ->where('slug', $slug);
141
    }
142
143
    /**
144
     * Order given relationship by order value.
145
     *
146
     * @param Illuminate\Database\Eloquent\Builder $query        Query builder.
147
     * @param string   $relationship Name of relationship to order.
148
     *
149
     * @return Illuminate\Database\Eloquent\Builder
150
     */
151
    public function scopeOrderRelationship($query, $relationship)
152
    {
153
        return $query->with([$relationship => function ($query) {
154
            $query->orderBy('order');
155
        }]);
156
    }
157
    
158
    /**
159
     * Return page properties to be passed to js.
160
     *
161
     * @return array
162
     */
163
    public function generateProps()
164
    {
165
        return [
166
            'deletedAt' => $this->deleted_at->diffForHumans(),
1 ignored issue
show
Documentation introduced by
The property deleted_at does not exist on object<Larafolio\Models\HasContent>. 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...
167
            'id'        => $this->id(),
0 ignored issues
show
Documentation Bug introduced by
The method id does not exist on object<Larafolio\Models\HasContent>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
168
            'name'      => $this->name(),
0 ignored issues
show
Documentation Bug introduced by
The method name does not exist on object<Larafolio\Models\HasContent>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
169
            'slug'      => $this->slug(),
0 ignored issues
show
Documentation Bug introduced by
The method slug does not exist on object<Larafolio\Models\HasContent>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
170
        ];
171
    }
172
}
173