Test Failed
Push — feature-laravel-5.4 ( 475d96...446986 )
by Kirill
07:30
created

Article::scopePublishedByBot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Models;
12
13
use Carbon\Carbon;
14
use Illuminate\Database\Eloquent\Relations\MorphTo;
15
use Illuminate\Support\Str;
16
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Database\Eloquent\Builder;
18
use Illuminate\Contracts\Auth\Authenticatable;
19
use Illuminate\Database\Eloquent\Relations\BelongsTo;
20
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
21
22
/**
23
 * Class Article.
24
 */
25
class Article extends Model
26
{
27
    /**
28
     * Published date and time field name.
29
     */
30
    private const PUBLISHED_AT = 'published_at';
31
32
    /**
33
     * Directory of article images.
34
     */
35
    public const DEFAULT_IMAGE_PATH = '/static/articles/';
36
37
    /**
38
     * @var array
39
     */
40
    protected $dates = [
41
        self::PUBLISHED_AT,
42
    ];
43
44
    /**
45
     * @var string
46
     */
47
    protected $table = 'articles';
48
49
    /**
50
     * @var array
51
     */
52
    protected $fillable = [
53
        'user_id', 'title', 'image', 'content_source',
54
        'status', 'published_at',
55
    ];
56
57
    /**
58
     * @param  Builder $builder
59
     * @return Builder
60
     */
61
    public static function scopeLatestPublished(Builder $builder): Builder
62
    {
63
        return $builder
64
            ->with('user')
65
            ->with('tags')
66
            ->latest('published_at')
67
            ->published();
68
    }
69
70
    /**
71
     * @param  Builder $builder
72
     * @return Builder
73
     */
74
    public static function scopeLatest(Builder $builder): Builder
75
    {
76
        return $builder->orderBy('published_at', 'desc');
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
    }
78
79
    /**
80
     * @param  Builder $builder
81
     * @return Builder
82
     */
83
    public static function scopePublished(Builder $builder): Builder
84
    {
85
        return $builder
86
            ->where('published_at', '<=', Carbon::now())
87
            ->where('status', Article\Status::PUBLISHED);
88
    }
89
90
    /**
91
     * @param Builder $builder
92
     * @return Builder
93
     */
94
    public static function scopePublishedByBot(Builder $builder): Builder
95
    {
96
        return $builder
97
            ->where('user_type', Bot::class);
98
    }
99
100
    /**
101
     * @param  Authenticatable|User|null $user
102
     * @return bool
103
     */
104
    public function isAllowedForUser(?Authenticatable $user): bool
105
    {
106
        $isAuthor = $user === null ? false : ($this->user->id === $user->getAuthIdentifier());
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<App\Models\Article>. 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...
107
108
        $isPublished = $this->status === Article\Status::PUBLISHED;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\Models\Article>. 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...
109
110
        $isAllowPublishedTime = $this->published_at <= Carbon::now();
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Models\Article>. 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...
111
112
        return $isAuthor || ($isPublished && $isAllowPublishedTime);
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getImageUrlAttribute(): string
119
    {
120
        if (Str::startsWith($this->image, ['http:', 'https:', '//'])) {
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<App\Models\Article>. 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...
121
            return $this->image;
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<App\Models\Article>. 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...
122
        }
123
124
        return static::DEFAULT_IMAGE_PATH . $this->image;
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<App\Models\Article>. 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...
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getCapitalizeTitleAttribute(): string
131
    {
132
        return Str::ucfirst($this->title);
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Models\Article>. 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...
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getNicePublishedDateAttribute(): string
139
    {
140
        if ($this->published_at > Carbon::now()->subMonth()) {
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Models\Article>. 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...
141
            return $this->published_at->diffForHumans();
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Models\Article>. 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...
142
        }
143
144
        return $this->published_at->toDateTimeString();
0 ignored issues
show
Documentation introduced by
The property published_at does not exist on object<App\Models\Article>. 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...
145
    }
146
147
    /**
148
     * @return MorphTo
149
     */
150
    public function user(): MorphTo
151
    {
152
        return $this->morphTo();
153
    }
154
155
    /**
156
     * @return BelongsToMany
157
     */
158
    public function tags(): BelongsToMany
159
    {
160
        return $this->belongsToMany(Tag::class, 'article_tags');
161
    }
162
}
163