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\Entity\Vacancy\Category; |
24
|
|
|
use Veslo\AnthillBundle\Enum\Route; |
25
|
|
|
use Veslo\AppBundle\Dto\Paginator\CriteriaDto; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Provides non-archived vacancies by a simple concept of journal with pages, using pagination internally |
29
|
|
|
*/ |
30
|
|
|
class Journal |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Vacancy repository |
34
|
|
|
* |
35
|
|
|
* @var VacancyRepository |
36
|
|
|
*/ |
37
|
|
|
private $vacancyRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Options for vacancy provider, ex. number per page |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $options; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Journal constructor. |
48
|
|
|
* |
49
|
|
|
* @param VacancyRepository $vacancyRepository Vacancy repository |
50
|
|
|
* @param array $options Options for vacancy provider, ex. number per page |
51
|
|
|
*/ |
52
|
|
|
public function __construct(VacancyRepository $vacancyRepository, array $options) |
53
|
|
|
{ |
54
|
|
|
$this->vacancyRepository = $vacancyRepository; |
55
|
|
|
|
56
|
|
|
$optionsResolver = new OptionsResolver(); |
57
|
|
|
$optionsResolver->setDefaults( |
58
|
|
|
[ |
59
|
|
|
'per_page' => 10, |
60
|
|
|
'max_days_after_publication' => 30, |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
$optionsResolver->setAllowedTypes('per_page', ['int']); |
64
|
|
|
$optionsResolver->setAllowedTypes('max_days_after_publication', ['int']); |
65
|
|
|
|
66
|
|
|
$this->options = $optionsResolver->resolve($options); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns all vacancies on the specified page from newest(1) to oldest(N) |
71
|
|
|
* |
72
|
|
|
* @param int $page Page for pagination |
73
|
|
|
* |
74
|
|
|
* @return AbstractPagination<Vacancy> |
75
|
|
|
*/ |
76
|
|
|
public function read(int $page = 1): AbstractPagination |
77
|
|
|
{ |
78
|
|
|
$paginationCriteria = $this->buildCommonPaginationCriteria($page); |
79
|
|
|
|
80
|
|
|
$pagination = $this->vacancyRepository->getPagination($paginationCriteria); |
81
|
|
|
$pagination->setUsedRoute(Route::VACANCY_LIST_PAGE); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $pagination; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns all vacancies on the specified page from newest(1) to oldest(N) within the target category |
88
|
|
|
* |
89
|
|
|
* @param Category $category A vacancy category instance |
90
|
|
|
* @param int $page Page for pagination |
91
|
|
|
* |
92
|
|
|
* @return AbstractPagination<Vacancy> |
93
|
|
|
*/ |
94
|
|
|
public function readCategory(Category $category, int $page = 1): AbstractPagination |
95
|
|
|
{ |
96
|
|
|
$paginationCriteria = $this->buildCommonPaginationCriteria($page); |
97
|
|
|
$paginationCriteria->addHint(VacancyRepository::PAGINATION_HINT_CATEGORY, $category); |
98
|
|
|
|
99
|
|
|
$pagination = $this->vacancyRepository->getPagination($paginationCriteria); |
100
|
|
|
$pagination->setUsedRoute(Route::VACANCY_LIST_BY_CATEGORY_PAGE); |
101
|
|
|
|
102
|
|
|
return $pagination; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a criteria instance with common parameters for pagination building |
107
|
|
|
* |
108
|
|
|
* @param int $page Page for pagination |
109
|
|
|
* |
110
|
|
|
* @return CriteriaDto |
111
|
|
|
*/ |
112
|
|
|
private function buildCommonPaginationCriteria(int $page = 1): CriteriaDto |
113
|
|
|
{ |
114
|
|
|
$pageNormalized = max(1, $page); |
115
|
|
|
|
116
|
|
|
$paginationCriteria = new CriteriaDto(); |
117
|
|
|
$paginationCriteria->setPage($pageNormalized); |
118
|
|
|
$paginationCriteria->setLimit($this->options['per_page']); |
119
|
|
|
|
120
|
|
|
$daysFrom = $this->options['max_days_after_publication']; |
121
|
|
|
$dateFrom = new DateTime("-$daysFrom days"); |
122
|
|
|
$paginationCriteria->addHint(VacancyRepository::PAGINATION_HINT_SYNC_DATE_AFTER, $dateFrom); |
123
|
|
|
|
124
|
|
|
return $paginationCriteria; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|