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

BlogService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 37
ccs 9
cts 10
cp 0.9
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPost() 0 11 2
A getPosts() 0 7 1
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