Test Failed
Push — master ( 084381...c83f36 )
by Evgeniy
03:18
created

PostRepository::getMaxUpdatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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;
9
use DateTimeImmutable;
10
use DateTimeInterface;
11
use Throwable;
12
use Yiisoft\Data\Reader\DataReaderInterface;
13
use Yiisoft\Data\Reader\Sort;
14
use Yiisoft\Yii\Cycle\Data\Reader\EntityReader;
15
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
16
17
final class PostRepository extends Select\Repository
18
{
19 1
    private EntityWriter $entityWriter;
20
21 1
    public function __construct(Select $select, EntityWriter $entityWriter)
22 1
    {
23 1
        $this->entityWriter = $entityWriter;
24
        parent::__construct($select);
25
    }
26
27
    /**
28
     * Get posts without filter with preloaded Users and Tags
29
     *
30 1
     * @psalm-return DataReaderInterface<int, Post>
31
     */
32 1
    public function findAllPreloaded(): DataReaderInterface
33 1
    {
34 1
        $query = $this->select()
35
            ->load(['user', 'tags']);
36
        return $this->prepareDataReader($query);
37
    }
38
39
    /**
40
     * @psalm-return DataReaderInterface<int, Post>
41
     */
42
    public function findByTag($tagId): DataReaderInterface
43
    {
44
        $query = $this
45
            ->select()
46
            ->where(['tags.id' => $tagId])
47
            ->load('user', ['method' => Select::SINGLE_QUERY]);
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
        return  $query->fetchOne();
62
    }
63
64
    public function getMaxUpdatedAt(): DateTimeInterface
65
    {
66
        return new DateTimeImmutable($this->select()->max('updated_at') ?? 'now');
67
    }
68
69
    public function findBySlug(string $slug): ?Post
70 1
    {
71
        return $this->select()->where(['slug' => $slug])->fetchOne();
72 1
    }
73 1
74 1
    /**
75
     * @throws Throwable
76
     */
77
    public function save(Post $post): void
78
    {
79
        $this->entityWriter->write([$post]);
80
    }
81
82
    private function prepareDataReader($query): EntityReader
83
    {
84
        return (new EntityReader($query))->withSort(
85
            Sort::only(['id', 'title', 'public', 'updated_at', 'published_at', 'user_id'])
86
                ->withOrder(['published_at' => 'desc'])
87
        );
88
    }
89
}
90