| Conditions | 13 |
| Paths | 469 |
| Total Lines | 121 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | ) |
||
| 177 |