Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Controller/EntityLockCheckController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Controller;
4
5
use Kunstmaan\AdminListBundle\Service\EntityVersionLockService;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
11
12
/**
13
 * EntityLockCheckController
14
 */
15
class EntityLockCheckController extends Controller
16
{
17
    /**
18
     * You can override this method to return the correct entity manager when using multiple databases ...
19
     *
20
     * @return \Doctrine\Common\Persistence\ObjectManager|object
0 ignored issues
show
Consider making the return type a bit more specific; maybe use \Doctrine\Persistence\ObjectManager.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
21
     */
22
    protected function getEntityManager()
23
    {
24
        return $this->getDoctrine()->getManager();
25
    }
26
27
    /**
28
     * @Route(
29
     *      "check/{id}/{repository}",
30
     *      requirements={"id" = "\d+"},
31
     *      name="KunstmaanAdminListBundle_entity_lock_check"
32
     * )
33
     *
34
     * @param $id
35
     * @param $repository
36
     *
37
     * @return JsonResponse
38
     */
39
    public function checkAction(Request $request, $id, $repository)
40
    {
41
        $entityIsLocked = false;
42
        $message = '';
43
44
        /** @var LockableEntityInterface $entity */
45
        $entity = $this->getEntityManager()->getRepository($repository)->find($id);
46
47
        try {
48
            /** @var EntityVersionLockService $entityVersionLockservice */
49
            $entityVersionLockService = $this->get('kunstmaan_entity.admin_entity.entity_version_lock_service');
50
51
            $entityIsLocked = $entityVersionLockService->isEntityLocked($this->getUser(), $entity);
52
53
            if ($entityIsLocked) {
54
                $user = $entityVersionLockService->getUsersWithEntityVersionLock($entity, $this->getUser());
55
                $message = $this->get('translator')->trans('kuma_admin_list.edit.flash.locked', ['%user%' => implode(', ', $user)]);
56
            }
57
        } catch (AccessDeniedException $ade) {
58
        }
59
60
        return new JsonResponse(['lock' => $entityIsLocked, 'message' => $message]);
61
    }
62
}
63