Passed
Push — master ( af00ba...368e6c )
by
unknown
10:47
created

LpReorderController::reorder()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 9
nc 64
nop 1
dl 0
loc 15
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller\Api;
8
9
use Chamilo\CourseBundle\Repository\CLpRepository;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
13
final class LpReorderController
14
{
15
    public function __construct(private CLpRepository $lpRepo) {}
16
17
    public function __invoke(Request $request): JsonResponse
18
    {
19
        return $this->reorder($request);
20
    }
21
22
    public function reorder(Request $request): JsonResponse
23
    {
24
        $data       = json_decode($request->getContent() ?: '[]', true);
25
        $courseId   = isset($data['courseId']) ? (int) $data['courseId'] : null;
26
        $sid        = array_key_exists('sid', $data) ? ($data['sid'] !== null ? (int)$data['sid'] : null) : null;
27
        $order      = $data['order'] ?? $data['ids'] ?? null;
28
        $categoryId = array_key_exists('categoryId', $data) ? ($data['categoryId'] !== null ? (int)$data['categoryId'] : null) : null;
29
30
        if (!$courseId || !\is_array($order)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $courseId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
31
            return new JsonResponse(['error' => 'Invalid payload'], 400);
32
        }
33
34
        $this->lpRepo->reorderByIds($courseId, $sid, array_map('intval', $order), $categoryId);
35
36
        return new JsonResponse(null, 204);
37
    }
38
}
39