Passed
Push — 0.x ( e3354a...ec213b )
by Pavel
03:32
created

Archive::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9332
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 DateTime;
19
use Knp\Component\Pager\Pagination\AbstractPagination;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use Veslo\AnthillBundle\Entity\Repository\VacancyRepository;
22
use Veslo\AnthillBundle\Entity\Vacancy;
23
use Veslo\AnthillBundle\Enum\Route;
24
use Veslo\AppBundle\Dto\Paginator\CriteriaDto;
25
26
/**
27
 * Provides archived vacancies, uses pagination internally
28
 */
29
class Archive
30
{
31
    /**
32
     * Vacancy repository
33
     *
34
     * @var VacancyRepository
35
     */
36
    private $vacancyRepository;
37
38
    /**
39
     * Options for vacancy provider, e.g. number per page
40
     *
41
     * @var array
42
     */
43
    private $options;
44
45
    /**
46
     * Archive constructor.
47
     *
48
     * @param VacancyRepository $vacancyRepository Vacancy repository
49
     * @param array             $options           Options for vacancy provider
50
     */
51
    public function __construct(VacancyRepository $vacancyRepository, array $options)
52
    {
53
        $this->vacancyRepository = $vacancyRepository;
54
55
        $optionsResolver = new OptionsResolver();
56
        $optionsResolver->setDefaults(
57
            [
58
                'per_page'                   => 10,
59
                'min_days_after_publication' => 30,
60
            ]
61
        );
62
        $optionsResolver->setAllowedTypes('per_page', ['int']);
63
        $optionsResolver->setAllowedTypes('min_days_after_publication', ['int']);
64
65
        $this->options = $optionsResolver->resolve($options);
66
    }
67
68
    /**
69
     * Returns all vacancies on the specified archive page from newest(1) to oldest(N)
70
     *
71
     * @param int $page Page for pagination
72
     *
73
     * @return AbstractPagination<Vacancy>
74
     */
75
    public function read(int $page = 1): AbstractPagination
76
    {
77
        $pageNormalized = max(1, $page);
78
79
        $paginationCriteria = new CriteriaDto();
80
        $paginationCriteria->setPage($pageNormalized);
81
        $paginationCriteria->setLimit($this->options['per_page']);
82
83
        $daysFrom = $this->options['min_days_after_publication'];
84
        $dateFrom = new DateTime("-$daysFrom days");
85
        $paginationCriteria->addHint(VacancyRepository::PAGINATION_HINT_SYNC_DATE_BEFORE, $dateFrom);
86
87
        $pagination = $this->vacancyRepository->getPagination($paginationCriteria);
88
        $pagination->setUsedRoute(Route::VACANCY_ARCHIVE_PAGE);
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

88
        $pagination->/** @scrutinizer ignore-call */ 
89
                     setUsedRoute(Route::VACANCY_ARCHIVE_PAGE);
Loading history...
89
90
        return $pagination;
91
    }
92
}
93