PostRepository::getMaxUpdatedAt()   A
last analyzed

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 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Post;
6
7
use App\Blog\Entity\Post;
8
use Cycle\ORM\Select;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Select was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use DateTimeImmutable;
10
use DateTimeInterface;
11
use Throwable;
12
use Yiisoft\Data\Cycle\Reader\EntityReader;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Cycle\Reader\EntityReader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Data\Cycle\Writer\EntityWriter;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Cycle\Writer\EntityWriter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Data\Reader\DataReaderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Reader\DataReaderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Data\Reader\Sort;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Reader\Sort was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
final class PostRepository extends Select\Repository
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Select\Repository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
{
19
    public function __construct(private EntityWriter $entityWriter, Select $select)
20
    {
21
        parent::__construct($select);
22
    }
23
24
    /**
25
     * Get posts without filter with preloaded Users and Tags.
26
     *
27
     * @psalm-return DataReaderInterface<int, Post>
28
     */
29
    public function findAllPreloaded(): DataReaderInterface
30
    {
31
        $query = $this
32
            ->select()
33
            ->load(['user', 'tags']);
34
35
        return $this->prepareDataReader($query);
36
    }
37
38
    /**
39
     * @psalm-return DataReaderInterface<int, Post>
40
     */
41
    public function findByTag($tagId): DataReaderInterface
42
    {
43
        $query = $this
44
            ->select()
45
            ->where(['tags.id' => $tagId])
46
            ->load('user', ['method' => Select::SINGLE_QUERY]);
47
48
        return $this->prepareDataReader($query);
49
    }
50
51
    public function fullPostPage(string $slug): ?Post
52
    {
53
        $query = $this
54
            ->select()
55
            ->where(['slug' => $slug])
56
            ->load('user', ['method' => Select::SINGLE_QUERY])
57
            ->load(['tags'])
58
            // force loading in single query with comments
59
            ->load('comments.user', ['method' => Select::SINGLE_QUERY])
60
            ->load('comments', ['method' => Select::OUTER_QUERY]);
61
62
        return  $query->fetchOne();
63
    }
64
65
    public function getMaxUpdatedAt(): DateTimeInterface
66
    {
67
        return new DateTimeImmutable($this
68
                ->select()
69
                ->max('updated_at') ?? 'now');
70
    }
71
72
    public function findBySlug(string $slug): ?Post
73
    {
74
        return $this
75
            ->select()
76
            ->where(['slug' => $slug])
77
            ->fetchOne();
78
    }
79
80
    /**
81
     * @throws Throwable
82
     */
83
    public function save(Post $post): void
84
    {
85
        $this->entityWriter->write([$post]);
86
    }
87
88
    private function prepareDataReader($query): EntityReader
89
    {
90
        return (new EntityReader($query))->withSort(
91
            Sort::only(['id', 'title', 'public', 'updated_at', 'published_at', 'user_id'])
92
                ->withOrder(['published_at' => 'desc'])
93
        );
94
    }
95
}
96