OrderMasterBadges   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 30 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Team;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use VideoGamesRecords\CoreBundle\Entity\Team;
12
use VideoGamesRecords\CoreBundle\Repository\TeamBadgeRepository;
13
14
class OrderMasterBadges extends AbstractController
15
{
16
    public function __construct(
17
        private readonly TeamBadgeRepository $teamBadgeRepository,
18
        private readonly EntityManagerInterface $entityManager
19
    ) {
20
    }
21
22
    public function __invoke(Team $team, Request $request): Response
23
    {
24
        $data = json_decode($request->getContent(), true);
25
26
        if (!is_array($data)) {
27
            return $this->json(['error' => 'Invalid JSON data'], 400);
28
        }
29
30
        foreach ($data as $item) {
31
            if (!isset($item['id']) || !isset($item['mbOrder'])) {
32
                return $this->json(['error' => 'Missing id or mbOrder in item'], 400);
33
            }
34
35
            $teamBadge = $this->teamBadgeRepository->find($item['id']);
36
37
            if (!$teamBadge) {
38
                return $this->json(['error' => 'TeamBadge not found'], 404);
39
            }
40
41
            if ($teamBadge->getTeam()->getId() !== $team->getId()) {
42
                return $this->json(['error' => 'TeamBadge does not belong to this team'], 403);
43
            }
44
45
            $teamBadge->setMbOrder((int) $item['mbOrder']);
46
            $this->entityManager->persist($teamBadge);
47
        }
48
49
        $this->entityManager->flush();
50
51
        return $this->json(['success' => true]);
52
    }
53
}
54