Passed
Pull Request — master (#49)
by
unknown
11:28
created

PostRepository::getArchive()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 33
rs 9.536
1
<?php
2
3
namespace App\Blog\Post;
4
5
use App\Blog\Comment\Scope\PublicScope;
6
use App\Blog\Entity\Post;
7
use Cycle\ORM\Select;
8
use Spiral\Database\DatabaseInterface;
9
use Spiral\Database\Driver\DriverInterface;
10
use Spiral\Database\Injection\Fragment;
11
use Yiisoft\Yii\Cycle\DataReader\SelectDataReader;
12
13
class PostRepository extends Select\Repository
14
{
15
    public function findAllPreloaded(): SelectDataReader
16
    {
17
        $query = $this->select()
18
                ->load(['user', 'tags']);
19
        return new SelectDataReader($query);
20
    }
21
22
    public function findArchivedPublic(int $year, int $month): SelectDataReader
23
    {
24
        $begin = (new \DateTimeImmutable())->setDate($year, $month, 1)->setTime(0, 0, 0);
25
        $end = $begin->setDate($year, $month + 1, 1)->setTime(0, 0, -1);
26
27
        $query = $this->select()
28
                    ->andWhere('published_at', 'between', $begin, $end)
29
                    ->load(['user', 'tags']);
30
        return new SelectDataReader($query);
31
    }
32
33
    public function findByTag($tagId): SelectDataReader
34
    {
35
        $query = $this->select()
36
                    ->where(['tags.id' => $tagId])
37
                    ->load(['user']);
38
        return new SelectDataReader($query);
39
    }
40
41
    public function fullPostPage(string $slug, ?string $userId = null): ?Post
42
    {
43
        $query = $this->select()
44
                      ->where(['slug' => $slug])
45
                      ->load('user', [
46
                          'method' => Select::SINGLE_QUERY,
47
                      ])
48
                      ->load('tags', [
49
                          'method' => Select::OUTER_QUERY,
50
                      ])
51
                      // force loading in single query with comments
52
                      ->load('comments.user', ['method' => Select::SINGLE_QUERY])
53
                      ->load('comments', [
54
                          'method' => Select::OUTER_QUERY,
55
                          // not works (default Constraint would not be replaced):
56
                          'load' => new PublicScope($userId === null ? null : ['user_id' => $userId]),
57
                      ]);
58
        /** @var null|Post $post */
59
        $post = $query->fetchOne();
60
        // /** @var Select\Repository $commentRepo */
61
        // $commentRepo = $this->orm->getRepository(Comment::class);
62
        // $commentRepo->select()->load('user')->where('post_id', $post->getId())->fetchAll();
63
        return $post;
64
    }
65
    /**
66
     * @return array Array of Array('Count' => '123', 'Month' => '8', 'Year' => '2019')
67
     */
68
    public function getArchive(): array
69
    {
70
        try {
71
            if ($this->getDriver() instanceof \Spiral\Database\Driver\SQLite\SQLiteDriver) {
72
                return $this->select()
73
                            ->buildQuery()
74
                            ->columns(
75
                                [
76
                                    'count(post.id) count',
77
                                    new Fragment('strftime(\'%m\', post.published_at) month'),
78
                                    new Fragment('strftime(\'%Y\', post.published_at) year'),
79
                                ]
80
                            )
81
                            ->orderBy('year', 'DESC')
82
                            ->orderBy('month', 'DESC')
83
                            ->groupBy('year, month')
84
                            ->fetchAll();
85
            }
86
            return $this->select()
87
                        ->buildQuery()
88
                        ->columns(
89
                            [
90
                                'count(post.id) count',
91
                                new Fragment('extract(month from post.published_at) month'),
92
                                new Fragment('extract(year from post.published_at) year'),
93
                            ]
94
                        )
95
                        ->orderBy('year', 'DESC')
96
                        ->orderBy('month', 'DESC')
97
                        ->groupBy('year, month')
98
                        ->fetchAll();
99
        } catch (\Spiral\Database\Exception\StatementException $d) {
100
            return [];
101
        }
102
    }
103
104
    private function getDriver(): DriverInterface
105
    {
106
        return $this->select()
107
                    ->getBuilder()
108
                    ->getLoader()
109
                    ->getSource()
110
                    ->getDatabase()
111
                    ->getDriver(DatabaseInterface::READ);
112
    }
113
}
114