Test Setup Failed
Branch master (1b2352)
by Valery
09:42
created

PropertyController::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\User\Ajax;
6
7
use App\Controller\AjaxController;
8
use App\Entity\Property;
9
use App\Repository\UserPropertyRepository;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
final class PropertyController extends AbstractController implements AjaxController
17
{
18
    /**
19
     * @Route("/user/property/{id<\d+>}/update", methods={"GET"}, name="user_property_update")
20
     * @IsGranted("PROPERTY_EDIT", subject="property", message="You cannot change this property.")
21
     */
22
    public function update(Request $request, Property $property, UserPropertyRepository $repository): JsonResponse
23
    {
24
        $state = $request->query->get('state');
25
26
        if (!\in_array($state, ['published', 'private'], true)) {
27
            return new JsonResponse(['status' => 'fail'], 422);
28
        }
29
30
        if ($repository->changeState($property, $state)) {
31
            return new JsonResponse(['status' => 'ok']);
32
        }
33
34
        return new JsonResponse(['status' => 'fail'], 500);
35
    }
36
}
37