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

EmployeeRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Employee;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Symfony\Contracts\Translation\TranslatorInterface;
8
9
class EmployeeRepository extends AbstractRepository
10
{
11
    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...
12
13
    public function __construct(ManagerRegistry $registry, TranslatorInterface $translator)
14
    {
15
        parent::__construct($registry, Employee::class);
16
        $this->translator = $translator;
17
    }
18
19
    /**
20
     * @return Employee[]
21
     * @throws \Doctrine\ORM\ORMException
22
     */
23
    public function rand(int $limit, int $page, int $seed, string $locale): array
24
    {
25
        $qb = $this->createQueryBuilder('e')
26
            ->setFirstResult(($page-1) * $limit)
27
            ->setMaxResults($limit);
28
        if (0 != $seed) {
29
            $qb->orderBy('RAND(:seed)')
30
                ->setParameter('seed', $seed);
31
        } else {
32
            $qb->orderBy('e.lastName', 'ASC');
33
        }
34
35
        $employees = $qb
36
            ->getQuery()
37
            ->execute()
38
        ;
39
40
        $employeesTranslated = [];
41
42
        foreach ($employees as $employee) {
43
            $employee->setLocale($locale);
44
            $this->_em->refresh($employee);
45
46
            if ($employee->getTranslations()) {
47
                $employee->unsetTranslations();
48
            }
49
50
            $this->translator->setLocale($locale);
51
            $employee->setPosition($this->translator->trans($employee->getPosition()));
52
53
            $employeesTranslated[] = $employee;
54
        }
55
56
        return $employeesTranslated;
57
    }
58
}
59