Passed
Branch develop (e24c32)
by BENARD
04:32
created

GroupAdminController::addLibChartAction()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 34
rs 9.2248
cc 5
nc 4
nop 2
1
<?php
2
namespace VideoGamesRecords\CoreBundle\Controller\Admin;
3
4
use Sonata\AdminBundle\Controller\CRUDController;
5
use Symfony\Component\HttpFoundation\RedirectResponse;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use VideoGamesRecords\CoreBundle\Entity\Group;
10
use VideoGamesRecords\CoreBundle\Form\Type\ChartTypeType;
11
12
/**
13
 * Class GroupAdminController
14
 */
15
class GroupAdminController extends CRUDController
16
{
17
    /**
18
     * @param $id
19
     * @return RedirectResponse
20
     */
21
    public function copyAction($id)
22
    {
23
        $object = $this->admin->getSubject();
24
25
        if (!$object) {
0 ignored issues
show
introduced by
$object is of type object, thus it always evaluated to true.
Loading history...
26
            throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
27
        }
28
29
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Sonata\AdminBundle\Model\ModelManagerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sonata\AdminBundle\Model\LockInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $em = $this->admin->getModelManager()->/** @scrutinizer ignore-call */ getEntityManager($this->admin->getClass());
Loading history...
30
        $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->copy($id, false);
31
32
        $this->addFlash('sonata_flash_success', 'Copied successfully');
33
34
        return new RedirectResponse($this->admin->generateUrl('list'));
35
    }
36
37
    /**
38
     * @param $id
39
     * @return RedirectResponse
40
     */
41
    public function copyWithLibChartAction($id)
42
    {
43
        $object = $this->admin->getSubject();
44
45
        if (!$object) {
0 ignored issues
show
introduced by
$object is of type object, thus it always evaluated to true.
Loading history...
46
            throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
47
        }
48
49
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
50
        $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->copy($id, true);
51
52
        $this->addFlash('sonata_flash_success', 'Copied with libchart successfully');
53
54
        return new RedirectResponse($this->admin->generateUrl('list'));
55
    }
56
57
    /**
58
     * @param         $id
59
     * @param Request $request
60
     * @return Response
61
     */
62
    public function addLibChartAction($id, Request $request)
63
    {
64
        /** @var Group $object */
65
        $object = $this->admin->getSubject();
66
67
        if ($object->getGame()->getStatus()->isActive()) {
68
            $this->addFlash('sonata_flash_error', 'Game is already activated');
69
            return new RedirectResponse($this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()]));
70
        }
71
72
        $em = $this->admin->getModelManager()->getEntityManager($this->admin->getClass());
73
        $form = $this->createForm(ChartTypeType::class);
74
        $form->handleRequest($request);
75
        if ($form->isSubmitted() && $form->isValid()) {
76
            $data = $form->getData();
77
            $type = $data['type'];
78
            $result = $em->getRepository('VideoGamesRecords\CoreBundle\Entity\Group')->insertLibChart($id, $type->getIdType());
79
            if ($result) {
80
                $this->addFlash('sonata_flash_success', 'Add all libchart on group successfully');
81
                return new RedirectResponse($this->admin->generateUrl('list', ['filter' => $this->admin->getFilterParameters()]));
82
            } else {
83
                $this->addFlash('sonata_flash_error', 'Add all libchart on group successfully');
84
                return new RedirectResponse($this->admin->generateUrl('add-lib-chart'));
85
            }
86
        }
87
88
        return $this->renderForm('@VideoGamesRecordsCore/Admin/group_add_chart_form.html.twig',
89
            [
90
                'base_template' => '@SonataAdmin/standard_layout.html.twig',
91
                'admin' => $this->admin,
92
                'object' => $object,
93
                'form' => $form,
94
                'group' => $object,
95
                'action' => 'edit'
96
            ]
97
        );
98
    }
99
}
100