Completed
Push — develop ( bf82f6...64da93 )
by Serhii
03:59 queued 01:48
created

EmployeesController::getAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Entity\Employee;
6
use App\Entity\Performance;
7
use App\Entity\Role;
8
use App\Model\EmployeesResponse;
9
use App\Model\PerformancesResponse;
10
use FOS\RestBundle\Request\ParamFetcher;
11
use FOS\RestBundle\Controller\Annotations\QueryParam;
12
use JMS\Serializer\SerializerInterface;
13
use Nelmio\ApiDocBundle\Annotation\Model;
14
use Swagger\Annotations as SWG;
15
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16
use Symfony\Component\Routing\Annotation\Route;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
/**
20
 * @Route("/api/employees")
21
 */
22
class EmployeesController extends AbstractController
23
{
24
    private TranslatorInterface $translator;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    public function __construct(TranslatorInterface $translator)
27
    {
28
        $this->translator = $translator;
29
    }
30
31
    /**
32
     * @Route("", name="get_employees", methods={"GET"})
33
     * @SWG\Response(
34
     *     response=200,
35
     *     description="Returns a collection of theatre employees.",
36
     *     @SWG\Schema(ref=@Model(type=EmployeesResponse::class))
37
     * )
38
     * @SWG\Response(
39
     *     response=404,
40
     *     description="Returned when the entities with given limit and offset are not found",
41
     * )
42
     *
43
     * @QueryParam(name="limit", requirements="\d+|all", default="all", description="Count entries at one page")
44
     * @QueryParam(name="random", requirements="\d+", default=0, description="Should we suffle the order. Use seed in response to keep the same order")
45
     * @QueryParam(name="seed", requirements="\d+", default=0, description="Ignored if random is 1")
46
     * @QueryParam(name="page", requirements="\d+|middle", default="1", description="Number of page to be shown or 'middle' for middle page")
47
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
48
     */
49
    public function cgetAction(ParamFetcher $paramFetcher)
50
    {
51
        $em = $this->getDoctrine()->getManager();
52
        $page = $paramFetcher->get('page');
53
        $overAllCount = $em->getRepository('App:Employee')->count([]);
54
        $limit = $paramFetcher->get('limit', $strict = true) == "all"
55
            ? $overAllCount
56
            : $paramFetcher->get('limit');
57
58
        $rand = 0 != $paramFetcher->get('random');
59
        $seed = $paramFetcher->get('seed');
60
        if ($rand) {
61
            $seed = rand(1, 1000);
62
        }
63
        if ('middle' == $page) {
64
            $page = round($overAllCount/$limit/2);
65
        }
66
67
        $employeesTranslated = $em->getRepository('App:Employee')
68
            ->rand($limit, $page, $seed, $paramFetcher->get('locale'));
69
70
        $response = new EmployeesResponse();
71
        $response->employees = $employeesTranslated;
72
        $response->currentPage = $page;
73
        $response->overAllCount = $overAllCount;
74
        $response->seed = $seed;
75
        $response->rand = $rand;
76
77
        return $response;
78
    }
79
80
    /**
81
     * @Route("/{slug}", name="get_employee", methods={"GET"})
82
     * @SWG\Response(
83
     *     response=200,
84
     *     description="Returns an Employee by unique property {slug}",
85
     *     @Model(type=Employee::class)
86
     * )
87
     * @SWG\Response(
88
     *     response=404,
89
     *     description="Returns when employee by {slug} not found in database",
90
     * )
91
     *
92
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
93
     */
94
    public function getAction(ParamFetcher $paramFetcher, $slug)
95
    {
96
        $em = $this->getDoctrine()->getManager();
97
98
        /** @var Employee $employee */
99
        $employee = $em->getRepository('App:Employee')->findOneByslug($slug);
100
101
        if (!$employee) {
102
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
103
        }
104
105
        $employee->setLocale($paramFetcher->get('locale'));
106
        $em->refresh($employee);
107
108
        if ($employee->getTranslations()) {
109
            $employee->unsetTranslations();
110
        }
111
112
        $this->translator->setLocale($paramFetcher->get('locale'));
113
        $employee->setPosition($this->translator->trans($employee->getPosition()));
114
115
        return $employee;
116
    }
117
118
    /**
119
     * @Route("/{slug}/roles", name="get_employee_roles", methods={"GET"})
120
     * @SWG\Response(
121
     *     response=200,
122
     *     description="Returns employee roles by employee {slug}",
123
     *     @SWG\Schema(
124
     *         type="array",
125
     *         @SWG\Items(ref=@Model(type=Role::class))
126
     *     )
127
     * )
128
     * @SWG\Response(
129
     *     response=404,
130
     *     description="Returns when employee by {slug} not found in database",
131
     * )
132
     *
133
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
134
     */
135
    public function getRolesAction(ParamFetcher $paramFetcher, $slug)
136
    {
137
        $em = $this->getDoctrine()->getManager();
138
139
        /** @var Employee $employee */
140
        $employee = $em->getRepository('App:Employee')->findOneByslug($slug);
141
142
        if (!$employee) {
143
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
144
        }
145
146
        $employee->setLocale($paramFetcher->get('locale'));
147
        $em->refresh($employee);
148
149
        $employee->unsetTranslations();
150
151
        $this->translator->setLocale($paramFetcher->get('locale'));
152
        $employee->setPosition($this->translator->trans($employee->getPosition()));
153
154
        $roles = $employee->getRoles();
155
156
        $rolesTranslated = [];
157
158
        foreach ($roles as $role) {
159
            /** @var Performance $performance */
160
            $performance = $role->getPerformance();
161
162
            $role->setLocale($paramFetcher->get('locale'));
163
            $em->refresh($role);
164
            $performance->setLocale($paramFetcher->get('locale'));
165
            $em->refresh($performance);
166
167
            $role->unsetTranslations();
168
            $performance->unsetTranslations();
169
170
            $rolesTranslated[] = $role;
171
        }
172
173
        $roles = $rolesTranslated;
174
175
        return $roles;
176
    }
177
}
178