Test Failed
Pull Request — master (#87)
by Dmitriy
02:46
created

BlogService::getPost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
    public function __construct(PostRepository $postRepository)
19
    {
20
        $this->postRepository = $postRepository;
21
    }
22
23
    public function getPosts(int $page): PaginatorInterface
24
    {
25
        $dataReader = $this->postRepository->findAll();
26
27
        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
    public function getPost(int $id): Post
40
    {
41
        /**
42
         * @var Post|null $post
43
         */
44
        $post = $this->postRepository->findOne(['id' => $id]);
45
        if ($post === null) {
46
            throw new NotFoundException();
47
        }
48
49
        return $post;
50
    }
51
}
52