| Conditions | 15 |
| Paths | 3120 |
| Total Lines | 129 |
| Code Lines | 78 |
| 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 |
||
| 118 | public function modifyAction(string $id = ''): void |
||
| 119 | { |
||
| 120 | // Find the OutWorkTimes data based on the provided ID |
||
| 121 | // or create a new one if the ID is not provided or not found. |
||
| 122 | $timeFrame = OutWorkTimes::findFirstById($id); |
||
| 123 | if ($timeFrame === null) { |
||
| 124 | $timeFrame = new OutWorkTimes(); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Create an array to store the extensions to forward calls to and populate it with the default value. |
||
| 128 | $forwardingExtensions = []; |
||
| 129 | $forwardingExtensions[""] = $this->translation->_("ex_SelectNumber"); |
||
| 130 | |||
| 131 | // Set the parameters to find extensions matching the extension in the OutWorkTimes data. |
||
| 132 | $parameters = [ |
||
| 133 | 'conditions' => 'number = :extension:', |
||
| 134 | 'bind' => [ |
||
| 135 | 'extension' => $timeFrame->extension, |
||
| 136 | ], |
||
| 137 | ]; |
||
| 138 | |||
| 139 | // Find the extensions matching the specified parameters and add them to the $forwardingExtensions array. |
||
| 140 | $extensions = Extensions::find($parameters); |
||
| 141 | foreach ($extensions as $record) { |
||
| 142 | $forwardingExtensions[$record->number] = $record->getRepresent(); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Create an array to store the available audio messages and populate it with the default value. |
||
| 146 | $audioMessages = []; |
||
| 147 | $audioMessages[""] = $this->translation->_("sf_SelectAudioFile"); |
||
| 148 | |||
| 149 | // Find the sound files with the "custom" category and add them to the $audioMessages array. |
||
| 150 | $soundFiles = SoundFiles::find('category="custom"'); |
||
| 151 | foreach ($soundFiles as $record) { |
||
| 152 | $audioMessages[$record->id] = $record->name; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Create an array to store the available actions and populate it with the default values. |
||
| 156 | $availableActions = [ |
||
| 157 | 'playmessage' => $this->translation->_('tf_SelectActionPlayMessage'), |
||
| 158 | 'extension' => $this->translation->_('tf_SelectActionRedirectToExtension'), |
||
| 159 | ]; |
||
| 160 | |||
| 161 | // Create an array to store the available week days and populate it with the default values. |
||
| 162 | $weekDays = ['-1' => '-']; |
||
| 163 | for ($i = "1"; $i <= 7; $i++) { |
||
| 164 | $weekDays[$i] = $this->translation->_(date('D', strtotime("Sunday +$i days"))); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Create a new TimeFrameEditForm object with the $timeFrame |
||
| 168 | // and arrays for the forwarding extensions, audio messages, available actions, and week days. |
||
| 169 | $form = new TimeFrameEditForm( |
||
| 170 | $timeFrame, |
||
| 171 | [ |
||
| 172 | 'extensions' => $forwardingExtensions, |
||
| 173 | 'audio-message' => $audioMessages, |
||
| 174 | 'available-actions' => $availableActions, |
||
| 175 | 'week-days' => $weekDays, |
||
| 176 | ] |
||
| 177 | ); |
||
| 178 | |||
| 179 | // Set the form and the represented value of the $timeFrame object to the view. |
||
| 180 | $this->view->form = $form; |
||
| 181 | $this->view->represent = $timeFrame->getRepresent(); |
||
| 182 | |||
| 183 | // Get the list of allowed routing rules for the specified time condition ID. |
||
| 184 | $parameters = [ |
||
| 185 | 'columns' => 'routId AS rule_id', |
||
| 186 | 'conditions' => 'timeConditionId=:timeConditionId:', |
||
| 187 | 'bind' => [ |
||
| 188 | 'timeConditionId' => $id, |
||
| 189 | ], |
||
| 190 | ]; |
||
| 191 | $allowedRules = OutWorkTimesRouts::find($parameters)->toArray(); |
||
| 192 | $allowedRulesIds = array_column($allowedRules, 'rule_id'); |
||
| 193 | |||
| 194 | |||
| 195 | $filter = [ |
||
| 196 | 'conditions' => 'type="friend"', |
||
| 197 | 'columns' => 'host,port,uniqid,registration_type', |
||
| 198 | ]; |
||
| 199 | $data = Sip::find($filter)->toArray(); |
||
| 200 | $providersId = []; |
||
| 201 | foreach ($data as $providerData) { |
||
| 202 | if ($providerData['registration_type'] === Sip::REG_TYPE_INBOUND || empty($providerData['host'])) { |
||
| 203 | $providersId[$providerData['uniqid']] = $providerData['uniqid']; |
||
| 204 | } else { |
||
| 205 | $providersId[$providerData['uniqid']] |
||
| 206 | = SIPConf::getContextId($providerData['host'], $providerData['port']); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | unset($data); |
||
| 210 | |||
| 211 | // Get the list of allowed routing rules |
||
| 212 | $rules = IncomingRoutingTable::find(['order' => 'provider,number,priority', 'conditions' => 'id>1']); |
||
| 213 | $routingTable = []; |
||
| 214 | foreach ($rules as $rule) { |
||
| 215 | $provider = $rule->Providers; |
||
| 216 | if ($provider) { |
||
| 217 | $modelType = ucfirst($provider->type); |
||
| 218 | $provByType = $provider->$modelType; |
||
| 219 | } else { |
||
| 220 | $provByType = new SIP(); |
||
| 221 | } |
||
| 222 | $extension = $rule->Extensions; |
||
| 223 | $values = [ |
||
| 224 | 'id' => $rule->id, |
||
| 225 | 'rulename' => $rule->rulename, |
||
| 226 | 'priority' => $rule->priority, |
||
| 227 | 'number' => $rule->number, |
||
| 228 | 'timeout' => $rule->timeout, |
||
| 229 | 'provider' => $rule->Providers ? $rule->Providers->getRepresent() : '', |
||
| 230 | 'provider-uniqid' => $rule->Providers ? $rule->Providers->uniqid : 'none', |
||
| 231 | 'context-id' => $rule->Providers ? $providersId[$rule->Providers->uniqid] : 'none', |
||
| 232 | 'disabled' => $provByType->disabled, |
||
| 233 | 'extension' => $rule->extension, |
||
| 234 | 'callerid' => $extension ? $extension->getRepresent() : '', |
||
| 235 | 'note' => $rule->note, |
||
| 236 | 'status' => in_array($rule->id, $allowedRulesIds, false) ? '' : 'disabled', |
||
| 237 | ]; |
||
| 238 | |||
| 239 | $routingTable[] = $values; |
||
| 240 | } |
||
| 241 | |||
| 242 | $this->view->rules = $routingTable; |
||
| 243 | |||
| 244 | // Prepare time zone offset |
||
| 245 | $dateTime = new \DateTime(); |
||
| 246 | $this->view->setVar('serverOffset', $dateTime->getOffset() / 60); |
||
| 247 | } |
||
| 407 |