| Conditions | 19 |
| Paths | 3940 |
| Total Lines | 131 |
| 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 |
||
| 39 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
| 40 | |||
| 41 | $this->em = $this->getContainer()->get('doctrine')->getManager(); |
||
| 42 | $uploadFileId = $input->getArgument('csv_file'); |
||
| 43 | $adminCode = $input->getArgument('admin_code'); |
||
| 44 | $encode = strtolower($input->getArgument('encode')); |
||
| 45 | $fileLoaderId = $input->getArgument('file_loader'); |
||
| 46 | |||
| 47 | /** @var UploadFile $uploadFile */ |
||
| 48 | $uploadFile = $this->em->getRepository('DoctrsSonataImportBundle:UploadFile')->find($uploadFileId); |
||
| 49 | $fileLoaders = $this->getContainer()->getParameter('doctrs_sonata_import.class_loaders'); |
||
| 50 | $fileLoader = isset($fileLoaders[$fileLoaderId], $fileLoaders[$fileLoaderId]['class']) ? |
||
| 51 | $fileLoaders[$fileLoaderId]['class'] : null; |
||
| 52 | |||
| 53 | if (!class_exists($fileLoader)) { |
||
| 54 | $uploadFile->setStatusError('class_loader not found'); |
||
| 55 | $this->em->flush($uploadFile); |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | $fileLoader = new $fileLoader(); |
||
| 59 | if (!$fileLoader instanceof FileLoaderInterface) { |
||
| 60 | $uploadFile->setStatusError('class_loader must be instanceof "FileLoaderInterface"'); |
||
| 61 | $this->em->flush($uploadFile); |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | try { |
||
| 67 | $fileLoader->setFile(new File($uploadFile->getFile())); |
||
| 68 | |||
| 69 | $pool = $this->getContainer()->get('sonata.admin.pool'); |
||
| 70 | /** @var AbstractAdmin $instance */ |
||
| 71 | $instance = $pool->getInstance($adminCode); |
||
| 72 | $entityClass = $instance->getClass(); |
||
| 73 | $meta = $this->em->getClassMetadata($entityClass); |
||
| 74 | $identifier = $meta->getSingleIdentifierFieldName(); |
||
| 75 | $exportFields = $instance->getExportFields(); |
||
| 76 | $form = $instance->getFormBuilder(); |
||
| 77 | foreach ($fileLoader->getIteration() as $line => $data) { |
||
| 78 | |||
| 79 | $log = new ImportLog(); |
||
| 80 | $log |
||
| 81 | ->setLine($line) |
||
| 82 | ->setUploadFile($uploadFile) |
||
| 83 | ; |
||
| 84 | |||
| 85 | $entity = new $entityClass(); |
||
| 86 | $errors = []; |
||
| 87 | foreach ($exportFields as $key => $name) { |
||
| 88 | $value = isset($data[$key]) ? $data[$key] : ''; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * В случае если указан ID (первый столбец) |
||
| 92 | * ищем сущность в базе |
||
| 93 | */ |
||
| 94 | if ($name === $identifier) { |
||
| 95 | if ($value) { |
||
| 96 | $oldEntity = $instance->getObject($value); |
||
| 97 | if ($oldEntity) { |
||
| 98 | $entity = $oldEntity; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | /** |
||
| 104 | * Поля форм не всегда соответствуют тому, что есть на сайте, и что в админке |
||
| 105 | * Поэтому если поле не указано в админке, то просто пропускаем его |
||
| 106 | */ |
||
| 107 | if (!$form->has($name)) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | $formBuilder = $form->get($name); |
||
| 111 | /** |
||
| 112 | * Многие делают ошибки в стандартной кодировке, |
||
| 113 | * поэтому на всякий случай провверяем оба варианта написания |
||
| 114 | */ |
||
| 115 | if ($encode !== 'utf8' && $encode !== 'utf-8') { |
||
| 116 | $value = iconv($encode, 'utf8//TRANSLIT', $value); |
||
| 117 | } |
||
| 118 | try { |
||
| 119 | $method = $this->getSetMethod($name); |
||
| 120 | $entity->$method($this->getValue($value, $formBuilder, $instance)); |
||
| 121 | } catch (\Exception $e) { |
||
| 122 | $errors[] = $e->getMessage(); |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | |||
| 126 | } |
||
| 127 | if (!count($errors)) { |
||
| 128 | $validator = $this->getContainer()->get('validator'); |
||
| 129 | $errors = $validator->validate($entity); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!count($errors)) { |
||
| 133 | $idMethod = $this->getSetMethod($identifier, 'get'); |
||
| 134 | /** |
||
| 135 | * Если у сещности нет ID, то она новая - добавляем ее |
||
| 136 | */ |
||
| 137 | if (!$entity->$idMethod()) { |
||
| 138 | $this->em->persist($entity); |
||
| 139 | $log->setStatus(ImportLog::STATUS_SUCCESS); |
||
| 140 | } else { |
||
| 141 | $log->setStatus(ImportLog::STATUS_EXISTS); |
||
| 142 | } |
||
| 143 | $this->em->flush($entity); |
||
| 144 | $log->setForeignId($entity->$idMethod()); |
||
| 145 | } else { |
||
| 146 | $log->setMessage(json_encode($errors)); |
||
| 147 | $log->setStatus(ImportLog::STATUS_ERROR); |
||
| 148 | } |
||
| 149 | $this->em->persist($log); |
||
| 150 | $this->em->flush($log); |
||
| 151 | } |
||
| 152 | $uploadFile->setStatus(UploadFile::STATUS_SUCCESS); |
||
| 153 | $this->em->flush($uploadFile); |
||
| 154 | } catch (\Exception $e) { |
||
| 155 | /** |
||
| 156 | * Данный хак нужен в случае бросания ORMException |
||
| 157 | * В случае бросания ORMException entity manager останавливается |
||
| 158 | * и его требуется перезагрузить |
||
| 159 | */ |
||
| 160 | if (!$this->em->isOpen()) { |
||
| 161 | $this->em = $this->em->create( |
||
| 162 | $this->em->getConnection(), |
||
| 163 | $this->em->getConfiguration() |
||
| 164 | ); |
||
| 165 | $uploadFile = $this->em->getRepository('DoctrsSonataImportBundle:UploadFile')->find($uploadFileId); |
||
| 166 | } |
||
| 167 | |||
| 168 | $uploadFile->setStatusError($e->getMessage()); |
||
| 169 | $this->em->flush($uploadFile); |
||
| 170 | } |
||
| 209 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths