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

VacancyController::listByCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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\Controller;
17
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Templating\EngineInterface;
21
use Veslo\AnthillBundle\Entity\Vacancy;
22
use Veslo\AnthillBundle\Entity\Vacancy\Category;
23
use Veslo\AnthillBundle\Enum\Route;
24
use Veslo\AnthillBundle\Vacancy\Provider\Archive;
25
use Veslo\AnthillBundle\Vacancy\Provider\Journal;
26
use Veslo\SanityBundle\Entity\Repository\Vacancy\IndexRepository;
27
28
/**
29
 * Vacancy controller.
30
 */
31
class VacancyController
32
{
33
    /**
34
     * Template engine
35
     *
36
     * @var EngineInterface
37
     */
38
    private $templateEngine;
39
40
    /**
41
     * Provides vacancies by a simple abstract concept of journal with pages, using pagination internally
42
     *
43
     * @var Journal
44
     */
45
    private $vacancyJournal;
46
47
    /**
48
     * Provides archived vacancies, uses pagination internally
49
     *
50
     * @var Archive
51
     */
52
    private $vacancyArchive;
53
54
    /**
55
     * Sanity index repository
56
     *
57
     * @var IndexRepository
58
     */
59
    private $indexRepository;
60
61
    /**
62
     * VacancyController constructor.
63
     *
64
     * @param EngineInterface $templateEngine  Template engine
65
     * @param Journal         $vacancyJournal  Provides vacancies by a simple abstract concept of journal with pages
66
     * @param Archive         $vacancyArchive  Provides archived vacancies, uses pagination internally
67
     * @param IndexRepository $indexRepository Sanity index repository
68
     */
69
    public function __construct(
70
        EngineInterface $templateEngine,
71
        Journal $vacancyJournal,
72
        Archive $vacancyArchive,
73
        IndexRepository $indexRepository
74
    ) {
75
        $this->templateEngine  = $templateEngine;
76
        $this->vacancyJournal  = $vacancyJournal;
77
        $this->vacancyArchive  = $vacancyArchive;
78
        $this->indexRepository = $indexRepository;
79
    }
80
81
    /**
82
     * Renders a list of vacancies
83
     *
84
     * @param int $page Page in the vacancy journal
85
     *
86
     * @return Response
87
     */
88
    public function list(int $page): Response
89
    {
90
        $pagination = $this->vacancyJournal->read($page);
91
92
        $content = $this->templateEngine->render(
93
            '@VesloAnthill/Vacancy/list.html.twig',
94
            [
95
                'pagination'                     => $pagination,
96
                'route_vacancy_show'             => Route::VACANCY_SHOW,
97
                'route_vacancy_list_by_category' => Route::VACANCY_LIST_BY_CATEGORY,
98
            ]
99
        );
100
101
        $response = new Response($content);
102
103
        return $response;
104
    }
105
106
    /**
107
     * Renders a list of vacancies for a target category
108
     *
109
     * @param Category $category Vacancy category
110
     * @param int      $page     Page in the vacancy journal
111
     *
112
     * @return Response
113
     *
114
     * @ParamConverter(name="category", converter="doctrine.orm", options={"mapping"={"categoryName": "name"}})
115
     */
116
    public function listByCategory(Category $category, int $page): Response
117
    {
118
        $pagination = $this->vacancyJournal->readCategory($category, $page);
119
120
        $content = $this->templateEngine->render(
121
            '@VesloAnthill/Vacancy/list.html.twig',
122
            [
123
                'pagination'                     => $pagination,
124
                'route_vacancy_show'             => Route::VACANCY_SHOW,
125
                'route_vacancy_list_by_category' => Route::VACANCY_LIST_BY_CATEGORY,
126
            ]
127
        );
128
129
        $response = new Response($content);
130
131
        return $response;
132
    }
133
134
    /**
135
     * Renders a list of archived vacancies
136
     *
137
     * @param int $page Page in the vacancy archive
138
     *
139
     * @return Response
140
     */
141
    public function listArchive(int $page): Response
142
    {
143
        $pagination = $this->vacancyArchive->read($page);
144
145
        $content = $this->templateEngine->render(
146
            '@VesloAnthill/Vacancy/list.html.twig',
147
            [
148
                'pagination'                     => $pagination,
149
                'route_vacancy_show'             => Route::VACANCY_SHOW,
150
                'route_vacancy_list_by_category' => Route::VACANCY_LIST_BY_CATEGORY,
151
            ]
152
        );
153
154
        $response = new Response($content);
155
156
        return $response;
157
    }
158
159
    /**
160
     * Renders a vacancy show page
161
     *
162
     * @param Vacancy $vacancy Vacancy entity
163
     *
164
     * @return Response
165
     *
166
     * @ParamConverter(name="vacancy", converter="doctrine.orm", options={"mapping"={"vacancySlug": "slug"}})
167
     */
168
    public function show(Vacancy $vacancy): Response
169
    {
170
        $vacancyId   = $vacancy->getId();
171
        $sanityIndex = $this->indexRepository->findByVacancyId($vacancyId);
172
173
        $content = $this->templateEngine->render(
174
            '@VesloAnthill/Vacancy/show.html.twig',
175
            [
176
                'vacancy'                        => $vacancy,
177
                'sanity_index'                   => $sanityIndex,
178
                'route_vacancy_list_by_category' => Route::VACANCY_LIST_BY_CATEGORY,
179
            ]
180
        );
181
182
        $response = new Response($content);
183
184
        return $response;
185
    }
186
}
187