Completed
Push — master ( a7fff6...5ea170 )
by Jeroen
18:57 queued 10s
created

EntityLockCheckController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 49
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityManager() 0 4 1
A checkAction() 0 23 3
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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Doctrine\Common\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 Request $request
35
     * @param $id
36
     * @param $repository
37
     *
38
     * @return JsonResponse
39
     */
40
    public function checkAction(Request $request, $id, $repository)
41
    {
42
        $entityIsLocked = false;
43
        $message = '';
44
45
        /** @var LockableEntityInterface $entity */
46
        $entity = $this->getEntityManager()->getRepository($repository)->find($id);
47
48
        try {
49
            /** @var EntityVersionLockService $entityVersionLockservice */
50
            $entityVersionLockService = $this->get('kunstmaan_entity.admin_entity.entity_version_lock_service');
51
52
            $entityIsLocked = $entityVersionLockService->isEntityLocked($this->getUser(), $entity);
53
54
            if ($entityIsLocked) {
55
                $user = $entityVersionLockService->getUsersWithEntityVersionLock($entity, $this->getUser());
56
                $message = $this->get('translator')->trans('kuma_admin_list.edit.flash.locked', array('%user%' => implode(', ', $user)));
57
            }
58
        } catch (AccessDeniedException $ade) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
59
        }
60
61
        return new JsonResponse(['lock' => $entityIsLocked, 'message' => $message]);
62
    }
63
}
64