Completed
Push — dev ( 40c7a5...93901c )
by Zach
03:40
created

Project::order()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
     * Return all visible projects.
30
     *
31
     * @param bool $group If true, group projects by 'type'.
32
     * @param bool $order If true, order projects by 'order'.
33
     *
34
     * @return \Illuminate\Support\Collection
35
     */
36
    public static function allVisible($group = true, $order = true)
37
    {
38
        $query = static::where('visible', true);
39
40
        return static::orderAndGroupQuery($query, $group, $order);
41
    }
42
43
    /**
44
     * Return all hidden projects.
45
     *
46
     * @param bool $group If true, group projects by 'type'.
47
     * @param bool $order If true, order projects by 'order'.
48
     *
49
     * @return \Illuminate\Support\Collection
50
     */
51
    public static function allHidden($group = true, $order = true)
52
    {
53
        $query = static::where('visible', false);
54
55
        return static::orderAndGroupQuery($query, $group, $order);
56
    }
57
58
    /**
59
     * Return all projects grouped by 'type'.
60
     *
61
     * @param bool $order If true, order projects by 'order'.
62
     *
63
     * @return \Illuminate\Support\Collection
64
     */
65
    public static function allGrouped($order = true)
66
    {
67
        $query = static::query();
68
69
        return static::orderAndGroupQuery($query, true, $order);
70
    }
71
72
    /**
73
     * Return all projects ordered by 'order'.
74
     *
75
     * @return \Illuminate\Support\Collection
76
     */
77
    public static function allOrdered()
78
    {
79
        $query = static::query();
80
81
        return static::orderAndGroupQuery($query, false, true);
82
    }
83
84
    /**
85
     * Get all projects with given block name.
86
     *
87
     * @param string $blockName Name of block.
88
     *
89
     * @return \Illuminate\Support\Collection
90
     */
91
    public static function hasBlockNamed($blockName)
92
    {
93
        return static::hasRelationshipNamed('projects', 'text_blocks', $blockName);
94
    }
95
96
    /**
97
     * Get all projects with given image name.
98
     *
99
     * @param string $imageName Name of image.
100
     *
101
     * @return \Illuminate\Support\Collection
102
     */
103
    public static function hasImageNamed($imageName)
104
    {
105
        return static::hasRelationshipNamed('projects', 'images', $imageName);
106
    }
107
108
    /**
109
     * Get all projects with given link name.
110
     *
111
     * @param string $linkName Name of link.
112
     *
113
     * @return \Illuminate\Support\Collection
114
     */
115
    public static function hasLinkNamed($linkName)
116
    {
117
        return static::hasRelationshipNamed('projects', 'links', $linkName);
118
    }
119
120
    /**
121
     * Get formatted text of block with project name or first block.
122
     *
123
     * @return \Larafolio\Models\TextBlock
124
     */
125
    public function getProjectBlock()
126
    {
127
        $block = $this->block($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...
128
129
        if ($block) {
130
            return $block;
131
        } elseif ($this->hasBlocks()) {
132
            return $this->blocks()->first();
133
        }
134
    }
135
136
    /**
137
     * Get formatted text of block with project name or first block.
138
     *
139
     * @param bool $formatted If true, return formatted text.
140
     *
141
     * @return string
142
     */
143
    public function getProjectBlockText($formatted = true)
144
    {
145
        $block = $this->getProjectBlock();
146
147
        if ($block && $formatted) {
148
            return $block->formattedText;
1 ignored issue
show
Documentation introduced by
The property formattedText does not exist on object<Larafolio\Models\TextBlock>. 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...
149
        } elseif ($block) {
150
            return $block->text;
1 ignored issue
show
Documentation introduced by
The property text does not exist on object<Larafolio\Models\TextBlock>. 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...
151
        }
152
153
        return $block;
154
    }
155
156
    /**
157
     * Get url of small image with project name or first image in collection.
158
     *
159
     * @return \Larafolio\Models\Image
160
     */
161
    public function getProjectImage()
162
    {
163
        $projectImage = $this->image($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...
164
165
        if ($projectImage) {
166
            return $projectImage;
167
        } elseif ($this->hasImages()) {
168
            return $this->images()->first();
169
        }
170
    }
171
172
    /**
173
     * Get url of small image with project name or first image in collection.
174
     *
175
     * @param string $size The size of the image, name of image cache filter.
176
     *
177
     * @return string
178
     */
179
    public function getProjectImageUrl($size = 'small')
180
    {
181
        $projectImage = $this->getProjectImage();
182
183
        if ($projectImage) {
184
            return $projectImage->{$size}();
185
        }
186
187
        return $projectImage;
188
    }
189
}
190