BlogService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 2
b 0
f 0
dl 0
loc 37
ccs 11
cts 12
cp 0.9167
rs 10

3 Methods

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