| Conditions | 16 |
| Paths | 60 |
| Total Lines | 91 |
| 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 |
||
| 91 | protected function process() |
||
| 92 | { |
||
| 93 | |||
| 94 | // initialize the URL key and the category |
||
| 95 | $urlKey = null; |
||
| 96 | $category = array(); |
||
| 97 | |||
| 98 | // prepare the store view code |
||
| 99 | $this->prepareStoreViewCode(); |
||
| 100 | |||
| 101 | // set the entity ID for the category with the passed path |
||
| 102 | try { |
||
| 103 | $this->setIds($category = $this->getCategoryByPath($path = $this->getValue(ColumnKeys::PATH))); |
||
| 104 | } catch (\Exception $e) { |
||
| 105 | $this->setIds(array()); |
||
| 106 | } |
||
| 107 | |||
| 108 | // query whether or not the URL key column has a value |
||
| 109 | if ($this->hasValue(ColumnKeys::URL_KEY)) { |
||
| 110 | $urlKey = $this->getValue(ColumnKeys::URL_KEY); |
||
| 111 | } else { |
||
| 112 | // query whether or not the existing category `url_key` should be re-created from the category name |
||
| 113 | if ($category && !$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::UPDATE_URL_KEY_FROM_NAME, true)) { |
||
|
|
|||
| 114 | // if the category already exists and NO re-creation from the category name has to |
||
| 115 | // be done, load the original `url_key`from the category and use that to proceed |
||
| 116 | $urlKey = $this->loadUrlKey($this->getSubject(), $this->getPrimaryKey()); |
||
| 117 | } |
||
| 118 | |||
| 119 | // try to load the value from column `name` if URL key is still |
||
| 120 | // empty, because we need it to process the the rewrites later on |
||
| 121 | if ($urlKey === null || $urlKey === '' && $this->hasValue(ColumnKeys::NAME)) { |
||
| 122 | $urlKey = $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // stop processing, if no URL key is available |
||
| 127 | if ($urlKey === null || $urlKey === '') { |
||
| 128 | // throw an exception, that the URL key can not be |
||
| 129 | // initialized and we're in the default store view |
||
| 130 | if ($this->getStoreViewCode(StoreViewCodes::ADMIN) === StoreViewCodes::ADMIN) { |
||
| 131 | throw new \Exception(sprintf('Can\'t initialize the URL key for category "%s" because columns "url_key" or "name" have a value set for default store view', $path)); |
||
| 132 | } |
||
| 133 | // stop processing, because we're in a store |
||
| 134 | // view row and a URL key is not mandatory |
||
| 135 | return; |
||
| 136 | } |
||
| 137 | |||
| 138 | // load ID of the actual store view |
||
| 139 | $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
||
| 140 | |||
| 141 | // explode the path into the category names |
||
| 142 | if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) { |
||
| 143 | // initialize the array for the category paths |
||
| 144 | $categoryPaths = array(); |
||
| 145 | // iterate over the parent category names and try |
||
| 146 | // to load the categories to build the URL path |
||
| 147 | for ($i = sizeof($categories) - 1; $i > 1; $i--) { |
||
| 148 | try { |
||
| 149 | // prepare the expected category name |
||
| 150 | $categoryPath = implode('/', array_slice($categories, 0, $i)); |
||
| 151 | // load the existing category and prepend the URL key the array with the category URL keys |
||
| 152 | $existingCategory = $this->getCategoryByPkAndStoreId($this->mapPath($categoryPath), $storeId); |
||
| 153 | // query whether or not an URL key is available or not |
||
| 154 | if (isset($existingCategory[MemberNames::URL_KEY])) { |
||
| 155 | array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]); |
||
| 156 | } else { |
||
| 157 | $this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category "%s"', $categoryPath)); |
||
| 158 | } |
||
| 159 | } catch (\Exception $e) { |
||
| 160 | $this->getSystemLogger()->debug(sprintf('Can\'t load parent category "%s"', $categoryPath)); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // update the URL key with the unique value |
||
| 166 | $this->setValue( |
||
| 167 | ColumnKeys::URL_KEY, |
||
| 168 | $urlKey = $this->makeUnique($this->getSubject(), $urlKey, array(implode('/', $categoryPaths))) |
||
| 169 | ); |
||
| 170 | |||
| 171 | // finally, append the URL key as last element to the path |
||
| 172 | array_push($categoryPaths, $urlKey); |
||
| 173 | |||
| 174 | // create the virtual column for the URL path |
||
| 175 | if ($this->hasHeader(ColumnKeys::URL_PATH) === false) { |
||
| 176 | $this->addHeader(ColumnKeys::URL_PATH); |
||
| 177 | } |
||
| 178 | |||
| 179 | // set the URL path |
||
| 180 | $this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths)); |
||
| 181 | } |
||
| 182 | |||
| 300 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.