NewsItemRepository::paginate()   A
last analyzed

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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\NewsItem;
6
use Illuminate\Support\Collection;
7
use Illuminate\Contracts\Pagination\Paginator;
8
9
class NewsItemRepository
10
{
11
    public static function getAll(): Collection
12
    {
13
        return NewsItem::orderBy('publish_date', 'desc')->get();
14
    }
15
16
    public static function getLatest(int $amount): Collection
17
    {
18
        return NewsItem::orderBy('publish_date', 'desc')
19
            ->take($amount)
20
            ->get();
21
    }
22
23
    public static function findById(int $id): NewsItem
24
    {
25
        return NewsItem::findOrFail($id);
26
    }
27
28
    public static function findBySlug(string $slug): NewsItem
29
    {
30
        return NewsItem::where('slug->'.content_locale(), $slug)->firstOrFail();
31
    }
32
33
    /**
34
     * @return \App\Models\NewsItem|null
35
     */
36
    public static function findNext(NewsItem $newsItem)
37
    {
38
        return NewsItem::online()
0 ignored issues
show
Bug introduced by
The method online() does not exist on App\Models\NewsItem. Did you maybe mean scopeOnline()?

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...
39
            ->where('publish_date', '>', $newsItem->publish_date)
0 ignored issues
show
Documentation introduced by
The property publish_date does not exist on object<App\Models\NewsItem>. 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...
40
            ->orderBy('publish_date', 'desc')
41
            ->first();
42
    }
43
44
    /**
45
     * @return \App\Models\NewsItem|null
46
     */
47
    public static function findPrevious(NewsItem $newsItem)
48
    {
49
        return NewsItem::online()
0 ignored issues
show
Bug introduced by
The method online() does not exist on App\Models\NewsItem. Did you maybe mean scopeOnline()?

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...
50
            ->where('publish_date', '<', $newsItem->publish_date)
0 ignored issues
show
Documentation introduced by
The property publish_date does not exist on object<App\Models\NewsItem>. 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...
51
            ->orderBy('publish_date', 'desc')
52
            ->first();
53
    }
54
55
    public static function paginate(int $perPage): Paginator
56
    {
57
        return NewsItem::online()->paginate($perPage);
0 ignored issues
show
Bug introduced by
The method online() does not exist on App\Models\NewsItem. Did you maybe mean scopeOnline()?

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...
58
    }
59
}
60