Passed
Push — develop ( e091bc...cf0cee )
by
unknown
01:07
created

PlayerChartController::submit()   F

Complexity

Conditions 13
Paths 469

Size

Total Lines 121
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 80
nc 469
nop 1
dl 0
loc 121
rs 2.9863
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
8
use Symfony\Component\HttpFoundation\Request;
9
use VideoGamesRecords\CoreBundle\Tools\Score;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use VideoGamesRecords\CoreBundle\Entity\PlayerChartLib;
12
use Doctrine\DBAL\DBALException;
13
14
/**
15
 * Class PlayerChartController
16
 */
17
class PlayerChartController extends Controller
18
{
19
20
    /**
21
     * @param Request $request
22
     * @return mixed
23
     */
24
    public function submit(Request $request)
25
    {
26
27
        /** @var \VideoGamesRecords\CoreBundle\Model\Api\PlayerChart $object */
28
        $object =  $this->get('serializer')->deserialize($request->getContent(), 'VideoGamesRecords\CoreBundle\Model\Api\PlayerChart', 'json');
29
30
        //@todo get idPlayer From Authentification
31
        $idPlayer = $object->getIdPlayer();
32
        $idChart = $object->getIdChart();
33
        $idPlaform = $object->getIdPlatform();
34
        $values = $object->getValues();
35
36
        $em = $this->getDoctrine()
37
            ->getManager();
38
39
        try {
40
            $player = $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:Player')->find($idPlayer);
41
            $chart = $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:Chart')->find($idChart);
42
            $platform = null;
43
            if ($idPlaform != null) {
44
                $platform = $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:Platform')->find($idPlaform);
45
            }
46
47
            if ($player === null) {
48
                return new JsonResponse(
49
                    array(
50
                        'return' => 201,
51
                        'message' => 'Player not found'
52
                    )
53
                );
54
            }
55
56
            if ($chart === null) {
57
                return new JsonResponse(
58
                    array(
59
                        'return' => 202,
60
                        'message' => 'Chart not found'
61
                    )
62
                );
63
            }
64
65
            $post = array();
66
            foreach ($chart->getLibs() as $lib) {
67
                $idLibChart = $lib->getIdLibChart();
68
                if (!array_key_exists($idLibChart, $values)) {
69
                    return new JsonResponse(
70
                        array(
71
                            'return' => 203,
72
                            'message' => 'Wrong libChart'
73
                        )
74
                    );
75
                }
76
                $values = $values[$idLibChart];
77
                $value = implode('', $values);
78
                if (($value === '') || !ctype_digit(trim($value, '-+'))) {
79
                    return new JsonResponse(
80
                        array(
81
                            'return' => 204,
82
                            'message' => 'Value is Null'
83
                        )
84
                    );
85
                }
86
                $value = Score::formToBdd(
87
                    $lib->getType()->getMask(),
88
                    $values
89
                );
90
                $post[$lib->getIdLibChart()] = $value;
91
            }
92
93
            $playerChart = $this->getDoctrine()
94
                ->getRepository('VideoGamesRecordsCoreBundle:PlayerChart')
95
                ->getFromUnique(
96
                    $idPlayer,
97
                    $idChart
98
                );
99
100
            $isNew = false;
101
            if ($playerChart === null) {
102
                $isNew = true;
103
                $playerChart = new PlayerChart();
104
                $playerChart->setPlayer($player);
105
                $playerChart->setChart($chart);
106
            }
107
            if ($platform != null) {
108
                $playerChart->setPlatform($platform);
109
            }
110
111
            $playerChart->setStatus($em->getReference('VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus', 1));
112
            $playerChart->setDateModif(new \DateTime());
113
            $em->persist($playerChart);
114
115
            foreach ($chart->getLibs() as $lib) {
116
                $playerChartLib = $this->getDoctrine()
117
                    ->getRepository('VideoGamesRecordsCoreBundle:PlayerChartLib')
118
                    ->find(
119
                        [
120
                            'player' => $idPlayer,
121
                            'libChart' => $lib->getIdLibChart()
122
                        ]
123
                    );
124
                if ($playerChartLib === null) {
125
                    $playerChartLib = new PlayerChartLib();
126
                    $playerChartLib->setPlayer($player);
127
                    $playerChartLib->setLibChart($lib);
128
                }
129
                $playerChartLib->setValue($post[$lib->getIdLibChart()]);
130
                $em->persist($playerChartLib);
131
            }
132
            $em->flush();
133
134
            return new JsonResponse(
135
                array(
136
                    'return' => 0,
137
                    'isNew' => $isNew,
138
                )
139
            );
140
        } catch (DBALException $e) {
141
            return new JsonResponse(
142
                array(
143
                    'return' => 101,
144
                    'message' => 'ERROR SQL'
145
                )
146
            );
147
        }
148
    }
149
150
151
    /*public function formAction($id)
152
    {
153
        $chart = $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:Chart')->getWithChartType($id);
154
        $charts = [$chart];
155
156
        $data = [
157
            'id' => $id,
158
            'type' => 'chart',
159
        ];
160
161
        $data = array_merge(
162
            $data,
163
            $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:PlayerChartLib')->getFormValues($this->getPlayer(), $chart)
164
        );
165
166
        $form = SubmitFormFactory::createSubmitForm(
167
            $this->get('form.factory')->create('Symfony\Component\Form\Extension\Core\Type\FormType', $data),
168
            $charts
169
        );
170
171
        $breadcrumbs = $this->getChartBreadcrumbs($chart);
172
        $breadcrumbs->addItem($chart->getLibChart());
173
174
        return $this->render('VideoGamesRecordsCoreBundle:Submit:form.html.twig', ['chart' => $chart, 'charts' => $charts, 'form' => $form->createView()]);
175
    }*/
176
}
177