Passed
Pull Request — develop (#64)
by
unknown
08:14
created

PlayerChartController::submit()   F

Complexity

Conditions 13
Paths 469

Size

Total Lines 121
Code Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 79
nc 469
nop 1
dl 0
loc 121
rs 3.0081
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()
88
                        ->getMask(), $values
89
                );
90
                $post[$lib->getIdLibChart()] = $value;
91
            }
92
93
            $playerChart = $this->getDoctrine()
94
                ->getRepository('VideoGamesRecordsCoreBundle:PlayerChart')
95
                ->getFromUnique(
96
                    $idPlayer, $idChart
97
                );
98
99
            $isNew = false;
100
            if ($playerChart === null) {
101
                $isNew = true;
102
                $playerChart = new PlayerChart();
103
                $playerChart->setPlayer($player);
104
                $playerChart->setChart($chart);
105
            }
106
            if ($platform != null) {
107
                $playerChart->setPlatform($platform);
108
            }
109
110
            $playerChart->setStatus($em->getReference('VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus', 1));
111
            $playerChart->setDateModif(new \DateTime());
112
            $em->persist($playerChart);
113
114
            foreach ($chart->getLibs() as $lib) {
115
                $playerChartLib = $this->getDoctrine()
116
                    ->getRepository('VideoGamesRecordsCoreBundle:PlayerChartLib')
117
                    ->find(
118
                        [
119
                            'player' => $idPlayer,
120
                            'libChart' => $lib->getIdLibChart()
121
                        ]
122
                    );
123
                if ($playerChartLib === null) {
124
                    $playerChartLib = new PlayerChartLib();
125
                    $playerChartLib->setPlayer($player);
126
                    $playerChartLib->setLibChart($lib);
127
                }
128
                $playerChartLib->setValue($post[$lib->getIdLibChart()]);
129
                $em->persist($playerChartLib);
130
            }
131
            $em->flush();
132
133
            return new JsonResponse(
134
                array(
135
                    'return' => 0,
136
                    'isNew' => $isNew,
137
                )
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
152
153
    /*public function formAction($id)
154
    {
155
        $chart = $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:Chart')->getWithChartType($id);
156
        $charts = [$chart];
157
158
        $data = [
159
            'id' => $id,
160
            'type' => 'chart',
161
        ];
162
163
        $data = array_merge(
164
            $data,
165
            $this->getDoctrine()->getRepository('VideoGamesRecordsCoreBundle:PlayerChartLib')->getFormValues($this->getPlayer(), $chart)
166
        );
167
168
        $form = SubmitFormFactory::createSubmitForm(
169
            $this->get('form.factory')->create('Symfony\Component\Form\Extension\Core\Type\FormType', $data),
170
            $charts
171
        );
172
173
        $breadcrumbs = $this->getChartBreadcrumbs($chart);
174
        $breadcrumbs->addItem($chart->getLibChart());
175
176
        return $this->render('VideoGamesRecordsCoreBundle:Submit:form.html.twig', ['chart' => $chart, 'charts' => $charts, 'form' => $form->createView()]);
177
    }*/
178
}
179