Passed
Pull Request — master (#87)
by Dmitriy
02:45
created

BlogService::getPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Application\Blog\Service;
6
7
use App\Application\Blog\Entity\Post\Post;
8
use App\Application\Blog\Entity\Post\PostRepository;
9
use App\Application\Exception\NotFoundException;
10
use Yiisoft\Data\Paginator\OffsetPaginator;
11
use Yiisoft\Data\Paginator\PaginatorInterface;
12
13
final class BlogService
14
{
15
    private const POSTS_PER_PAGE = 10;
16
    private PostRepository $postRepository;
17
18 3
    public function __construct(PostRepository $postRepository)
19
    {
20 3
        $this->postRepository = $postRepository;
21
    }
22
23 1
    public function getPosts(int $page): PaginatorInterface
24
    {
25 1
        $dataReader = $this->postRepository->findAll();
26
27 1
        return (new OffsetPaginator($dataReader))
28
            ->withPageSize(self::POSTS_PER_PAGE)
29
            ->withCurrentPage($page);
30
    }
31
32
    /**
33
     * @param int $id
34
     *
35
     * @throws NotFoundException
36
     *
37
     * @return Post
38
     */
39 2
    public function getPost(int $id): Post
40
    {
41
        /**
42
         * @var Post|null $post
43
         */
44 2
        $post = $this->postRepository->findOne(['id' => $id]);
45 2
        if ($post === null) {
46
            throw new NotFoundException();
47
        }
48
49 2
        return $post;
50
    }
51
}
52