| Conditions | 9 |
| Paths | 15 |
| Total Lines | 64 |
| Code Lines | 43 |
| 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 |
||
| 52 | public function importFromFile($fileName, $userId, DateTime $ignoreBeforeDate = null) |
||
| 53 | { |
||
| 54 | $content = file_get_contents($fileName); |
||
| 55 | $content = str_replace('\xFF\xFE', '', $content); // remove UTF16(LE) BOM |
||
| 56 | $content = mb_convert_encoding($content, 'UTF-8', 'UCS-2LE'); |
||
| 57 | $rows = ArrayUtil::trimExplode("\n", $content); |
||
| 58 | foreach ($rows as $row) { |
||
| 59 | $data = str_getcsv($row, ',', '"', '""'); |
||
| 60 | if (count($data) !== 4) { |
||
| 61 | throw new WrongFileFormatException( |
||
| 62 | $this->translator->trans('field_notes.error.wrong_file_format') |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | |||
| 66 | $date = $this->getDate($data[1]); |
||
| 67 | |||
| 68 | if ($ignoreBeforeDate !== null && $date < $ignoreBeforeDate) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!array_key_exists($data[2], self::LOG_TYPE)) { |
||
| 73 | $this->addError( |
||
| 74 | /** @Desc("Log type ""%type%"" is not implemented.") */ |
||
| 75 | $this->translator->trans('field_notes.error.log_type_not_implemented', ['%type%' => $data[2]]) |
||
| 76 | ); |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | $type = self::LOG_TYPE[$data[2]]; |
||
| 80 | |||
| 81 | if (strtoupper(substr($data[0], 0, 2)) == 'GC') { |
||
| 82 | $query = $this->entityManager->createQueryBuilder() |
||
| 83 | ->select('g') |
||
| 84 | ->from('AppBundle:Geocache', 'g') |
||
| 85 | ->where("IFELSE(g.wpGcMaintained = '', g.wpGc, g.wpGcMaintained) = :code") |
||
| 86 | ->setParameter('code', $data[0]) |
||
| 87 | ->getQuery(); |
||
| 88 | $geocache = $query->getOneOrNullResult(); |
||
| 89 | } else { |
||
| 90 | $geocache = $this->entityManager->getRepository('AppBundle:Geocache')->findOneBy(['wpOc' => $data[0]]); |
||
| 91 | } |
||
| 92 | if (!$geocache) { |
||
| 93 | $this->addError( |
||
| 94 | /** @Desc("Geocache ""%code%"" not found.") */ |
||
| 95 | $this->translator->trans('field_notes.error.geocache_not_found', ['%code%' => $data[0]]) |
||
| 96 | ); |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $fieldNote = new FieldNote(); |
||
| 101 | $fieldNote->setUser($this->entityManager->getReference('AppBundle:User', $userId)); |
||
| 102 | $fieldNote->setGeocache($geocache); |
||
| 103 | $fieldNote->setDate($date); |
||
| 104 | $fieldNote->setType($type); |
||
| 105 | $fieldNote->setText($data[3]); |
||
| 106 | $this->entityManager->persist($fieldNote); |
||
| 107 | } |
||
| 108 | $this->entityManager->flush(); |
||
| 109 | |||
| 110 | if ($this->hasErrors()) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 184 |