Passed
Pull Request — master (#545)
by Alexander
01:45
created

BlogService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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;
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...
9
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...
10
11
final class BlogService
12
{
13
    private const POSTS_PER_PAGE = 10;
14
    private PostRepository $postRepository;
15
16
    public function __construct(PostRepository $postRepository)
17
    {
18
        $this->postRepository = $postRepository;
19
    }
20
21
    public function getPosts(int $page): PaginatorInterface
22
    {
23
        $dataReader = $this->postRepository->findAll();
24
25
        return (new OffsetPaginator($dataReader))
26
            ->withPageSize(self::POSTS_PER_PAGE)
27
            ->withCurrentPage($page);
28
    }
29
30
    /**
31
     * @param int $id
32
     *
33
     * @throws NotFoundException
34
     *
35
     * @return Post
36
     */
37
    public function getPost(int $id): Post
38
    {
39
        /**
40
         * @var Post|null $post
41
         */
42
        $post = $this->postRepository->findOne(['id' => $id]);
43
        if ($post === null) {
44
            throw new NotFoundException();
45
        }
46
47
        return $post;
48
    }
49
}
50