Passed
Pull Request — develop (#95)
by BENARD
13:04 queued 39s
created

GetCharts   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A __invoke() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Group;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\Intl\Locale;
11
use VideoGamesRecords\CoreBundle\Entity\Chart;
12
use VideoGamesRecords\CoreBundle\Entity\Group;
13
use VideoGamesRecords\CoreBundle\ValueObject\GroupOrderBy;
14
15
class GetCharts extends AbstractController
16
{
17
    public function __construct(private readonly EntityManagerInterface $em)
18
    {
19
    }
20
21
    /**
22
     * @param Group $group
23
     * @param Request $request
24
     * @return array
25
     */
26
    public function __invoke(Group $group, Request $request): array
27
    {
28
        $orderBy = ['id' => 'ASC'];
29
        if ($group->getOrderBy() === GroupOrderBy::NAME) {
30
            $locale = Locale::getDefault();
31
            if ($locale === 'fr') {
32
                $orderBy = ['libChartFr' => 'ASC'];
33
            } else {
34
                $orderBy = ['libChartEn' => 'ASC'];
35
            }
36
        } elseif ($group->getOrderBy() === GroupOrderBy::ID) {
37
            $orderBy = ['id' => 'ASC'];
38
        }
39
        return $this->em->getRepository(Chart::class)->findBy(
40
            ['group' => $group],
41
            $orderBy
42
        );
43
    }
44
}
45