| Conditions | 9 |
| Paths | 25 |
| Total Lines | 122 |
| 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 |
||
| 51 | public function run() |
||
| 52 | { |
||
| 53 | $total = $errors = 0; |
||
| 54 | $context = $this->getContext(); |
||
| 55 | $config = $context->getConfig(); |
||
| 56 | $logger = $context->getLogger(); |
||
| 57 | $mappings = $this->getDefaultMapping(); |
||
| 58 | |||
| 59 | |||
| 60 | /** controller/jobs/coupon/import/csv/code/mapping |
||
| 61 | * List of mappings between the position in the CSV file and item keys |
||
| 62 | * |
||
| 63 | * This configuration setting overwrites the shared option |
||
| 64 | * "controller/common/coupon/import/csv/mapping" if you need a |
||
| 65 | * specific setting for the job controller. Otherwise, you should |
||
| 66 | * use the shared option for consistency. |
||
| 67 | * |
||
| 68 | * @param array Associative list of processor names and lists of key/position pairs |
||
| 69 | * @since 2017.10 |
||
| 70 | * @category Developer |
||
| 71 | * @see controller/jobs/coupon/import/csv/code/skip-lines |
||
| 72 | * @see controller/jobs/coupon/import/csv/code/max-size |
||
| 73 | */ |
||
| 74 | $mappings = $config->get( 'controller/jobs/coupon/import/csv/code/mapping', $mappings ); |
||
| 75 | |||
| 76 | |||
| 77 | /** controller/jobs/coupon/import/csv/code/max-size |
||
| 78 | * Maximum number of CSV rows to import at once |
||
| 79 | * |
||
| 80 | * It's more efficient to read and import more than one row at a time |
||
| 81 | * to speed up the import. Usually, the bigger the chunk that is imported |
||
| 82 | * at once, the less time the importer will need. The downside is that |
||
| 83 | * the amount of memory required by the import process will increase as |
||
| 84 | * well. Therefore, it's a trade-off between memory consumption and |
||
| 85 | * import speed. |
||
| 86 | * |
||
| 87 | * @param integer Number of rows |
||
| 88 | * @since 2017.10 |
||
| 89 | * @category Developer |
||
| 90 | * @see controller/jobs/coupon/import/csv/code/skip-lines |
||
| 91 | * @see controller/jobs/coupon/import/csv/code/mapping |
||
| 92 | */ |
||
| 93 | $maxcnt = (int) $config->get( 'controller/jobs/coupon/import/csv/code/max-size', 1000 ); |
||
| 94 | |||
| 95 | |||
| 96 | /** controller/jobs/coupon/import/csv/code/skip-lines |
||
| 97 | * Number of rows skipped in front of each CSV files |
||
| 98 | * |
||
| 99 | * Some CSV files contain header information describing the content of |
||
| 100 | * the column values. These data is for informational purpose only and |
||
| 101 | * can't be imported into the database. Using this option, you can |
||
| 102 | * define the number of lines that should be left out before the import |
||
| 103 | * begins. |
||
| 104 | * |
||
| 105 | * @param integer Number of rows |
||
| 106 | * @since 2015.08 |
||
| 107 | * @category Developer |
||
| 108 | * @see controller/jobs/coupon/import/csv/code/mapping |
||
| 109 | * @see controller/jobs/coupon/import/csv/code/max-size |
||
| 110 | */ |
||
| 111 | $skiplines = (int) $config->get( 'controller/jobs/coupon/import/csv/code/skip-lines', 0 ); |
||
| 112 | |||
| 113 | |||
| 114 | try |
||
| 115 | { |
||
| 116 | $processor = $this->getProcessors( $mappings ); |
||
| 117 | $codePos = $this->getCodePosition( $mappings['code'] ); |
||
| 118 | $fs = $context->getFileSystemManager()->get( 'fs-import' ); |
||
| 119 | $dir = 'couponcode/' . $context->getLocale()->getSite()->getCode(); |
||
| 120 | |||
| 121 | if( $fs->isDir( $dir ) === false ) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach( $fs->scan( $dir ) as $filename ) |
||
| 126 | { |
||
| 127 | if( $filename == '.' || $filename == '..' ) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | list( $couponId, ) = explode( '.', $filename ); |
||
| 132 | $container = $this->getContainer( $fs->readf( $dir . '/' . $filename ) ); |
||
| 133 | |||
| 134 | $msg = sprintf( 'Started coupon import from "%1$s" (%2$s)', $filename, __CLASS__ ); |
||
| 135 | $logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 136 | |||
| 137 | foreach( $container as $content ) |
||
| 138 | { |
||
| 139 | for( $i = 0; $i < $skiplines; $i++ ) { |
||
| 140 | $content->next(); |
||
| 141 | } |
||
| 142 | |||
| 143 | while( ( $data = $this->getData( $content, $maxcnt, $codePos ) ) !== [] ) |
||
| 144 | { |
||
| 145 | $items = $this->getCouponCodeItems( array_keys( $data ) ); |
||
| 146 | $errcnt = $this->import( $items, $data, $couponId, $processor ); |
||
| 147 | $chunkcnt = count( $data ); |
||
| 148 | |||
| 149 | $msg = 'Imported coupon lines from "%1$s": %2$d/%3$d (%4$s)'; |
||
| 150 | $logger->log( sprintf( $msg, $filename, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 151 | |||
| 152 | $errors += $errcnt; |
||
| 153 | $total += $chunkcnt; |
||
| 154 | unset( $items, $data ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $container->close(); |
||
| 159 | $fs->rm( $dir . '/' . $filename ); |
||
| 160 | |||
| 161 | $msg = 'Finished coupon import: %1$d successful, %2$s errors, %3$s total (%4$s)'; |
||
| 162 | $logger->log( sprintf( $msg, $total - $errors, $errors, $total, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | catch( \Exception $e ) |
||
| 166 | { |
||
| 167 | $logger->log( 'Coupon import error: ' . $e->getMessage() ); |
||
| 168 | $logger->log( $e->getTraceAsString() ); |
||
| 169 | |||
| 170 | throw $e; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 316 |