OrderMasterBadges::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 6
nop 2
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
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