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