| Conditions | 13 |
| Paths | 55 |
| Total Lines | 90 |
| Code Lines | 66 |
| 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 applyChangesAction() |
||
| 51 | { |
||
| 52 | $type = $this->Request()->getParam('type'); |
||
| 53 | $value = $this->Request()->getParam('value'); |
||
| 54 | $detailId = $this->Request()->getParam('detailId'); |
||
| 55 | |||
| 56 | /** @var \Shopware\Models\Article\Detail $detail */ |
||
| 57 | $detail = $this->getModelManager()->getRepository('\Shopware\Models\Article\Detail')->find($detailId); |
||
| 58 | |||
| 59 | if (!$detail) { |
||
| 60 | $this->View()->assign('success', false); |
||
| 61 | $message = Shopware()->Snippets()->getNamespace('backend/connect/view/main')->get( |
||
| 62 | 'changed_products/error/wrongArticleDetailId', |
||
| 63 | 'The product was not found', |
||
| 64 | true |
||
| 65 | ); |
||
| 66 | $this->View()->assign('message', $message); |
||
| 67 | |||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** @var \Shopware\Models\Article\Article $article */ |
||
| 72 | $article = $detail->getArticle(); |
||
| 73 | $connectAttribute = $this->getHelper()->getOrCreateConnectAttributeByModel($detail); |
||
| 74 | |||
| 75 | $updateFlags = $this->getHelper()->getUpdateFlags(); |
||
| 76 | $updateFlagsByName = array_flip($updateFlags); |
||
| 77 | $flag = $updateFlagsByName[$type]; |
||
| 78 | |||
| 79 | switch ($type) { |
||
| 80 | case 'shortDescription': |
||
| 81 | $article->setDescription($value); |
||
| 82 | break; |
||
| 83 | case 'longDescription': |
||
| 84 | $article->setDescriptionLong($value); |
||
| 85 | break; |
||
| 86 | case 'additionalDescription': |
||
| 87 | $detail->getAttribute()->setConnectProductDescription($value); |
||
| 88 | break; |
||
| 89 | case 'name': |
||
| 90 | $article->setName($value); |
||
| 91 | break; |
||
| 92 | case 'image': |
||
| 93 | $lastUpdate = json_decode($connectAttribute->getLastUpdate(), true); |
||
| 94 | $this->getImageImport()->importImagesForArticle( |
||
| 95 | array_diff($lastUpdate['image'], $lastUpdate['variantImages']), |
||
| 96 | $article |
||
| 97 | ); |
||
| 98 | $this->getImageImport()->importImagesForDetail( |
||
| 99 | $lastUpdate['variantImages'], |
||
| 100 | $detail |
||
| 101 | ); |
||
| 102 | break; |
||
| 103 | case 'mainImage': |
||
| 104 | $lastUpdate = json_decode($connectAttribute->getLastUpdate(), true); |
||
| 105 | if (isset($lastUpdate['image'][0])) { |
||
| 106 | $this->getImageImport()->importMainImage( |
||
| 107 | $lastUpdate['image'][0], $article->getId() |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | case 'price': |
||
| 112 | $netPrice = $value / (1 + ($article->getTax()->getTax()/100)); |
||
| 113 | $customerGroup = $this->getHelper()->getDefaultCustomerGroup(); |
||
| 114 | |||
| 115 | $detail->getPrices()->clear(); |
||
| 116 | $price = new Price(); |
||
| 117 | $price->fromArray([ |
||
| 118 | 'from' => 1, |
||
| 119 | 'price' => $netPrice, |
||
| 120 | 'basePrice' => $connectAttribute->getPurchasePrice(), |
||
| 121 | 'customerGroup' => $customerGroup, |
||
| 122 | 'article' => $article |
||
| 123 | ]); |
||
| 124 | $detail->setPrices([$price]); |
||
| 125 | break; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($connectAttribute->getLastUpdateFlag() & $flag) { |
||
| 129 | $connectAttribute->flipLastUpdateFlag($flag); |
||
| 130 | } |
||
| 131 | if ($type == 'image') { |
||
| 132 | if ($connectAttribute->getLastUpdateFlag() & $updateFlagsByName['imageInitialImport']) { |
||
| 133 | $connectAttribute->flipLastUpdateFlag($updateFlagsByName['imageInitialImport']); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->getModelManager()->flush(); |
||
| 138 | $this->View()->assign('success', true); |
||
| 139 | } |
||
| 140 | |||
| 182 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.