Passed
Pull Request — master (#572)
by Dmitriy
01:33
created

BlogService::getPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Paginator\OffsetPaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Data\Paginator\PaginatorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Data\Paginator\PaginatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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