Passed
Push — 0.x ( fc5333...ca02fa )
by Pavel
03:09
created

Journal::buildCommonPaginationCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AnthillBundle\Vacancy\Provider;
17
18
use Knp\Component\Pager\Pagination\AbstractPagination;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Veslo\AnthillBundle\Entity\Repository\VacancyRepository;
21
use Veslo\AnthillBundle\Entity\Vacancy;
22
use Veslo\AnthillBundle\Entity\Vacancy\Category;
23
use Veslo\AnthillBundle\Enum\Route;
24
use Veslo\AppBundle\Dto\Paginator\CriteriaDto;
25
26
/**
27
 * Provides vacancies by a simple concept of journal with pages, using pagination internally
28
 */
29
class Journal
30
{
31
    /**
32
     * Vacancy repository
33
     *
34
     * @var VacancyRepository
35
     */
36
    private $vacancyRepository;
37
38
    /**
39
     * Options for vacancy provider, ex. number per page
40
     *
41
     * @var array
42
     */
43
    private $options;
44
45
    /**
46
     * Journal constructor.
47
     *
48
     * @param VacancyRepository $vacancyRepository Vacancy repository
49
     * @param array             $options           Options for vacancy provider, ex. number per page
50
     */
51
    public function __construct(VacancyRepository $vacancyRepository, array $options)
52
    {
53
        $this->vacancyRepository = $vacancyRepository;
54
55
        // For readability purposes.
56
        $optionsResolver = new OptionsResolver();
57
        $optionsResolver->setDefaults(['per_page' => 10]);
58
59
        $this->options = $optionsResolver->resolve($options);
60
    }
61
62
    /**
63
     * Returns all vacancies on the specified page from newest(1) to oldest(N)
64
     *
65
     * @param int $page Page for pagination
66
     *
67
     * @return AbstractPagination<Vacancy>
68
     */
69
    public function read(int $page = 1): AbstractPagination
70
    {
71
        $paginationCriteria = $this->buildCommonPaginationCriteria($page);
72
73
        $pagination = $this->vacancyRepository->getPagination($paginationCriteria);
74
        $pagination->setUsedRoute(Route::VACANCY_LIST);
0 ignored issues
show
Bug introduced by
The method setUsedRoute() does not exist on Knp\Component\Pager\Pagination\AbstractPagination. It seems like you code against a sub-type of Knp\Component\Pager\Pagination\AbstractPagination such as Knp\Bundle\PaginatorBund...ation\SlidingPagination. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $pagination->/** @scrutinizer ignore-call */ 
75
                     setUsedRoute(Route::VACANCY_LIST);
Loading history...
75
76
        return $pagination;
77
    }
78
79
    /**
80
     * Returns all vacancies on the specified page from newest(1) to oldest(N) within the target category
81
     *
82
     * @param Category $category A vacancy category instance
83
     * @param int      $page     Page for pagination
84
     *
85
     * @return AbstractPagination<Vacancy>
86
     */
87
    public function readCategory(Category $category, int $page = 1): AbstractPagination
88
    {
89
        $paginationCriteria = $this->buildCommonPaginationCriteria($page);
90
        $paginationCriteria->addHint(VacancyRepository::PAGINATION_HINT_CATEGORY, $category);
91
92
        $pagination = $this->vacancyRepository->getPagination($paginationCriteria);
93
        $pagination->setUsedRoute(Route::VACANCY_LIST_BY_CATEGORY);
94
95
        return $pagination;
96
    }
97
98
    /**
99
     * Returns a criteria instance with common parameters for pagination building
100
     *
101
     * @param int $page Page for pagination
102
     *
103
     * @return CriteriaDto
104
     */
105
    private function buildCommonPaginationCriteria(int $page = 1): CriteriaDto
106
    {
107
        $pageNormalized = max(1, $page);
108
109
        $paginationCriteria = new CriteriaDto();
110
        $paginationCriteria->setPage($pageNormalized);
111
        $paginationCriteria->setLimit($this->options['per_page']);
112
113
        return $paginationCriteria;
114
    }
115
}
116