| Conditions | 10 |
| Paths | 10 |
| Total Lines | 111 |
| Code Lines | 29 |
| 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 |
||
| 131 | protected function import( $filename ) |
||
| 132 | { |
||
| 133 | $context = $this->getContext(); |
||
| 134 | $config = $context->getConfig(); |
||
| 135 | $domains = ['attribute', 'media', 'price', 'product', 'product/property', 'text']; |
||
| 136 | |||
| 137 | /** controller/jobs/product/import/xml/domains |
||
| 138 | * List of item domain names that should be retrieved along with the product items |
||
| 139 | * |
||
| 140 | * This configuration setting overwrites the shared option |
||
| 141 | * "controller/common/product/import/xml/domains" if you need a |
||
| 142 | * specific setting for the job controller. Otherwise, you should |
||
| 143 | * use the shared option for consistency. |
||
| 144 | * |
||
| 145 | * @param array Associative list of MShop item domain names |
||
| 146 | * @since 2019.04 |
||
| 147 | * @category Developer |
||
| 148 | * @see controller/jobs/product/import/xml/backup |
||
| 149 | * @see controller/jobs/product/import/xml/max-query |
||
| 150 | */ |
||
| 151 | $domains = $config->get( 'controller/jobs/product/import/xml/domains', $domains ); |
||
| 152 | |||
| 153 | /** controller/jobs/product/import/xml/backup |
||
| 154 | * Name of the backup for sucessfully imported files |
||
| 155 | * |
||
| 156 | * After a XML file was imported successfully, you can move it to another |
||
| 157 | * location, so it won't be imported again and isn't overwritten by the |
||
| 158 | * next file that is stored at the same location in the file system. |
||
| 159 | * |
||
| 160 | * You should use an absolute path to be sure but can be relative path |
||
| 161 | * if you absolutely know from where the job will be executed from. The |
||
| 162 | * name of the new backup location can contain placeholders understood |
||
| 163 | * by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
||
| 164 | * which would create "backup/2000-01-01". For more information about the |
||
| 165 | * strftime() placeholders, please have a look into the PHP documentation of |
||
| 166 | * the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
||
| 167 | * |
||
| 168 | * '''Note:''' If no backup name is configured, the file or directory |
||
| 169 | * won't be moved away. Please make also sure that the parent directory |
||
| 170 | * and the new directory are writable so the file or directory could be |
||
| 171 | * moved. |
||
| 172 | * |
||
| 173 | * @param integer Name of the backup file, optionally with date/time placeholders |
||
| 174 | * @since 2019.04 |
||
| 175 | * @category Developer |
||
| 176 | * @see controller/jobs/product/import/xml/domains |
||
| 177 | * @see controller/jobs/product/import/xml/max-query |
||
| 178 | */ |
||
| 179 | $backup = $config->get( 'controller/jobs/product/import/xml/backup' ); |
||
| 180 | |||
| 181 | /** controller/jobs/product/import/xml/max-query |
||
| 182 | * Maximum number of XML nodes processed at once |
||
| 183 | * |
||
| 184 | * Processing and fetching several product items at once speeds up importing |
||
| 185 | * the XML files. The more items can be processed at once, the faster the |
||
| 186 | * import. More items also increases the memory usage of the importer and |
||
| 187 | * thus, this parameter should be low enough to avoid reaching the memory |
||
| 188 | * limit of the PHP process. |
||
| 189 | * |
||
| 190 | * @param integer Number of XML nodes |
||
| 191 | * @since 2019.04 |
||
| 192 | * @category Developer |
||
| 193 | * @category User |
||
| 194 | * @see controller/jobs/product/import/xml/domains |
||
| 195 | * @see controller/jobs/product/import/xml/backup |
||
| 196 | */ |
||
| 197 | $maxquery = $config->get( 'controller/jobs/product/import/xml/max-query', 1000 ); |
||
| 198 | |||
| 199 | |||
| 200 | $nodes = []; |
||
| 201 | $total = $slice = 0; |
||
| 202 | $xml = new \XMLReader(); |
||
| 203 | |||
| 204 | if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) { |
||
| 205 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) ); |
||
| 206 | } |
||
| 207 | |||
| 208 | while( $xml->read() === true ) |
||
| 209 | { |
||
| 210 | if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'productitem' ) |
||
| 211 | { |
||
| 212 | if( ( $dom = $xml->expand() ) === false ) |
||
| 213 | { |
||
| 214 | $msg = sprintf( 'Expanding "%1$s" node failed', 'productitem' ); |
||
| 215 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
| 216 | } |
||
| 217 | |||
| 218 | $nodes[] = $dom; |
||
| 219 | |||
| 220 | if( $slice++ >= $maxquery ) |
||
| 221 | { |
||
| 222 | $this->importNodes( $nodes, $domains ); |
||
| 223 | unset( $nodes ); |
||
| 224 | $nodes = []; |
||
| 225 | $slice = 0; |
||
| 226 | } |
||
| 227 | |||
| 228 | $total++; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | $this->importNodes( $nodes, $domains ); |
||
| 233 | unset( $nodes ); |
||
| 234 | |||
| 235 | if( !empty( $backup ) && @rename( $filename, strftime( $backup ) ) === false ) |
||
| 236 | { |
||
| 237 | $msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup ); |
||
| 238 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $total; |
||
| 242 | } |
||
| 310 |