| Conditions | 8 |
| Paths | 11 |
| Total Lines | 57 |
| Code Lines | 36 |
| 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 |
||
| 50 | public function importFromFile($fileName, $userId, DateTime $ignoreBeforeDate = null) |
||
| 51 | { |
||
| 52 | $content = file_get_contents($fileName); |
||
| 53 | $content = mb_convert_encoding($content, 'UTF-8', 'UCS-2LE'); |
||
| 54 | $rows = ArrayUtil::trimExplode("\n", $content); |
||
| 55 | foreach ($rows as $row) { |
||
| 56 | $data = str_getcsv($row, ',', '"', '""'); |
||
| 57 | if (count($data) !== 4) { |
||
| 58 | throw new WrongFileFormatException( |
||
| 59 | $this->translator->trans('field_notes.error.wrong_file_format') |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | $date = DateTime::createFromFormat( |
||
| 64 | self::FIELD_NOTE_DATETIME_FORMAT, |
||
| 65 | $data[1], |
||
| 66 | new DateTimeZone('UTC') |
||
| 67 | ); |
||
| 68 | |||
| 69 | if ($ignoreBeforeDate !== null && $date < $ignoreBeforeDate) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (!array_key_exists($data[2], self::LOG_TYPE)) { |
||
| 74 | $this->addError( |
||
| 75 | /** @Desc("Log type ""%type%"" is not implemented.") */ |
||
| 76 | $this->translator->trans('field_notes.error.log_type_not_implemented', ['%type%' => $data[2]]) |
||
| 77 | ); |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | $type = self::LOG_TYPE[$data[2]]; |
||
| 81 | |||
| 82 | $geocache = $this->entityManager->getRepository('AppBundle:Geocache')->findOneBy(['wpOc' => $data[0]]); |
||
| 83 | if (!$geocache) { |
||
| 84 | $this->addError( |
||
| 85 | /** @Desc("Geocache ""%code%"" not found.") */ |
||
| 86 | $this->translator->trans('field_notes.error.geocache_not_found', ['%code%' => $data[0]]) |
||
| 87 | ); |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $fieldNote = new FieldNote(); |
||
| 92 | $fieldNote->setUser($this->entityManager->getReference('AppBundle:User', $userId)); |
||
| 93 | $fieldNote->setGeocache($geocache); |
||
| 94 | $fieldNote->setDate($date); |
||
| 95 | $fieldNote->setType($type); |
||
| 96 | $fieldNote->setText($data[3]); |
||
| 97 | $this->entityManager->persist($fieldNote); |
||
| 98 | } |
||
| 99 | $this->entityManager->flush(); |
||
| 100 | |||
| 101 | if ($this->hasErrors()) { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | return true; |
||
| 106 | } |
||
| 107 | |||
| 145 |