Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Channel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Channel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Channel extends Resource |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $accountCode; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var CallerId |
||
| 43 | */ |
||
| 44 | private $caller; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var CallerId |
||
| 48 | */ |
||
| 49 | private $connected; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DateTime |
||
| 53 | */ |
||
| 54 | private $creationTime; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DialplanCep |
||
| 58 | */ |
||
| 59 | private $dialplan; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string Unique identifier of the channel. This is the same as the Uniqueid field in AMI. |
||
| 63 | */ |
||
| 64 | private $id; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string Name of the channel (i.e. SIP/foo-0000a7e3) |
||
| 68 | */ |
||
| 69 | private $name; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $state; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function getAccountCode() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return CallerId Caller identification |
||
| 86 | */ |
||
| 87 | public function getCaller() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return CallerId Connected caller identification |
||
| 94 | */ |
||
| 95 | public function getConnected() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return DateTime |
||
| 102 | */ |
||
| 103 | public function getCreationTime() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return DialplanCep Dialplan location (context/extension/priority) |
||
| 110 | */ |
||
| 111 | 1 | public function getDialplan() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return string Unique identifier of the channel. This is the same as the Uniqueid field in AMI. |
||
| 118 | */ |
||
| 119 | 38 | public function getId() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return string Name of the channel (i.e. SIP/foo-0000a7e3) |
||
| 126 | */ |
||
| 127 | public function getName() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getState() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param callable $callback |
||
| 142 | */ |
||
| 143 | public function onStasisEnd(callable $callback) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param callable $callback |
||
| 150 | */ |
||
| 151 | public function onceStasisEnd(callable $callback) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param callable $callback |
||
| 158 | */ |
||
| 159 | public function onStasisStart(callable $callback) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param callable $callback |
||
| 166 | */ |
||
| 167 | public function onceStasisStart(callable $callback) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param callable $callback |
||
| 174 | */ |
||
| 175 | public function onChannelCallerId(callable $callback) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param callable $callback |
||
| 182 | */ |
||
| 183 | public function onceChannelCallerId(callable $callback) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param callable $callback |
||
| 190 | */ |
||
| 191 | public function onChannelCreated(callable $callback) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param callable $callback |
||
| 198 | */ |
||
| 199 | public function onceChannelCreated(callable $callback) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param callable $callback |
||
| 206 | */ |
||
| 207 | public function onChannelDestroyed(callable $callback) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param callable $callback |
||
| 214 | */ |
||
| 215 | public function onceChannelDestroyed(callable $callback) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param callable $callback |
||
| 222 | */ |
||
| 223 | public function removeChannelDestroyedListener(callable $callback) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param callable $callback |
||
| 230 | */ |
||
| 231 | public function onChannelDialplan(callable $callback) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param callable $callback |
||
| 238 | */ |
||
| 239 | public function onceChannelDialplan(callable $callback) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param callable $callback |
||
| 246 | */ |
||
| 247 | public function onChannelDtmfReceived(callable $callback) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param callable $callback |
||
| 254 | */ |
||
| 255 | public function onceChannelDtmfReceived(callable $callback) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param callable $callback |
||
| 262 | */ |
||
| 263 | public function onChannelEnteredBridge(callable $callback) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param callable $callback |
||
| 270 | */ |
||
| 271 | public function onceChannelEnteredBridge(callable $callback) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param callable $callback |
||
| 278 | */ |
||
| 279 | public function onChannelHangupRequest(callable $callback) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param callable $callback |
||
| 286 | */ |
||
| 287 | public function onceChannelHangupRequest(callable $callback) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param callable $callback |
||
| 294 | */ |
||
| 295 | public function onChannelLeftBridge(callable $callback) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param callable $callback |
||
| 302 | */ |
||
| 303 | public function onceChannelLeftBridge(callable $callback) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param callable $callback |
||
| 310 | */ |
||
| 311 | public function onChannelStateChange(callable $callback) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param callable $callback |
||
| 318 | */ |
||
| 319 | public function onceChannelStateChange(callable $callback) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param callable $callback |
||
| 326 | */ |
||
| 327 | public function onChannelHold(callable $callback) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param callable $callback |
||
| 334 | */ |
||
| 335 | public function onceChannelHold(callable $callback) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param callable $callback |
||
| 342 | */ |
||
| 343 | public function onChannelUnhold(callable $callback) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param callable $callback |
||
| 350 | */ |
||
| 351 | public function onceChanneUnhold(callable $callback) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param callable $callback |
||
| 358 | */ |
||
| 359 | public function onChannelTalkingFinished(callable $callback) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param callable $callback |
||
| 366 | */ |
||
| 367 | public function onceChannelTalkingFinished(callable $callback) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param callable $callback |
||
| 374 | */ |
||
| 375 | public function onChannelTalkingStarted(callable $callback) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param callable $callback |
||
| 382 | */ |
||
| 383 | public function onceChannelTalkingStarted(callable $callback) |
||
| 387 | 37 | ||
| 388 | 37 | /** |
|
| 389 | * @param callable $callback |
||
| 390 | */ |
||
| 391 | public function onChannelUserevent(callable $callback) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param callable $callback |
||
| 398 | */ |
||
| 399 | public function onceChannelUserevent(callable $callback) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param callable $callback |
||
| 406 | */ |
||
| 407 | public function onChannelVarset(callable $callback) |
||
| 411 | |||
| 412 | 37 | /** |
|
| 413 | 37 | * @param callable $callback |
|
| 414 | */ |
||
| 415 | public function onceChannelVarset(callable $callback) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Delete (i.e. hangup) a channel. |
||
| 422 | * |
||
| 423 | * @throws NotFoundException |
||
| 424 | */ |
||
| 425 | public function deleteChannel() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Hangup the channel if it still exists. |
||
| 432 | */ |
||
| 433 | public function hangup() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Exit application; continue execution in the dialplan. |
||
| 440 | * |
||
| 441 | * @param string $context The context to continue to. |
||
| 442 | * @param string $extension The extension to continue to. |
||
| 443 | * @param int $priority The priority to continue to. |
||
| 444 | * @throws NotFoundException |
||
| 445 | * @throws ConflictException |
||
| 446 | */ |
||
| 447 | public function continueDialplan($context, $extension, $priority) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Answer a channel. |
||
| 454 | * |
||
| 455 | * @throws NotFoundException |
||
| 456 | * @throws ConflictException |
||
| 457 | */ |
||
| 458 | public function answer() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Indicate ringing to a channel. |
||
| 465 | * |
||
| 466 | * @throws NotFoundException |
||
| 467 | * @throws ConflictException |
||
| 468 | */ |
||
| 469 | public function startRinging() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Stop ringing indication on a channel if locally generated. |
||
| 476 | * |
||
| 477 | * @throws NotFoundException |
||
| 478 | * @throws ConflictException |
||
| 479 | */ |
||
| 480 | public function stopRinging() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Send provided DTMF to a given channel. |
||
| 487 | * |
||
| 488 | * @param string $dtmf DTMF To send. |
||
| 489 | * @param int $before Amount of time to wait before DTMF digits (specified in milliseconds) start. |
||
| 490 | * @param int $between Amount of time in between DTMF digits (specified in milliseconds). Default: 100 |
||
| 491 | * @param int $duration Length of each DTMF digit (specified in milliseconds). Default: 100 |
||
| 492 | * @param int $after Amount of time to wait after DTMF digits (specified in milliseconds) end. |
||
| 493 | * @throws InvalidParameterException |
||
| 494 | * @throws NotFoundException |
||
| 495 | * @throws ConflictException |
||
| 496 | */ |
||
| 497 | public function sendDtmf($dtmf, $before = null, $between = null, $duration = null, $after = null) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Mute a channel. |
||
| 504 | * |
||
| 505 | * @param string $direction (default both) Direction in which to mute audio |
||
| 506 | * @throws NotFoundException |
||
| 507 | * @throws ConflictException |
||
| 508 | */ |
||
| 509 | public function mute($direction) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Unmute a channel. |
||
| 516 | * |
||
| 517 | * @param string $direction (default both) Direction in which to unmute audio |
||
| 518 | * @throws NotFoundException |
||
| 519 | * @throws ConflictException |
||
| 520 | */ |
||
| 521 | public function unmute($direction) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Hold a channel. |
||
| 528 | * |
||
| 529 | * @throws NotFoundException |
||
| 530 | * @throws ConflictException |
||
| 531 | */ |
||
| 532 | public function hold() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Remove a channel from hold. |
||
| 539 | * |
||
| 540 | * @throws NotFoundException |
||
| 541 | * @throws ConflictException |
||
| 542 | */ |
||
| 543 | public function unhold() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in |
||
| 550 | * this manner will suspend MOH without resuming automatically. If continuing music on hold is |
||
| 551 | * desired, the stasis application must reinitiate music on hold. |
||
| 552 | * |
||
| 553 | * @param string $mohClass Music on hold class to use |
||
| 554 | * @throws NotFoundException |
||
| 555 | * @throws ConflictException |
||
| 556 | */ |
||
| 557 | public function startMusicOnHold($mohClass) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Stop playing music on hold to a channel. |
||
| 564 | 2 | * |
|
| 565 | * @throws NotFoundException |
||
| 566 | 2 | * @throws ConflictException |
|
| 567 | */ |
||
| 568 | public function stopMusicOnHold() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Play silence to a channel. Using media operations such as /play on a channel playing silence in this manner will suspend silence without resuming automatically. |
||
| 575 | * |
||
| 576 | * @throws NotFoundException |
||
| 577 | * @throws ConflictException |
||
| 578 | */ |
||
| 579 | public function startSilence() |
||
| 583 | |||
| 584 | /** |
||
| 585 | 4 | * Stop playing silence to a channel. |
|
| 586 | * |
||
| 587 | 4 | * @throws NotFoundException |
|
| 588 | 4 | * @throws ConflictException |
|
| 589 | */ |
||
| 590 | public function stopSilence() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Start playback of media. The media URI may be any of a number of URI's. Currently sound:, |
||
| 597 | * recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a |
||
| 598 | * playback resource that can be used to control the playback of media (pause, rewind, fast forward, |
||
| 599 | * etc.) |
||
| 600 | * |
||
| 601 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
| 602 | * |
||
| 603 | * @param string $media (required) Media's URI to play. |
||
| 604 | * @param string $lang For sounds, selects language for sound. |
||
| 605 | * @param int $offsetms Number of media to skip before playing. |
||
| 606 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
| 607 | * @param string $playbackId Playback Id. |
||
| 608 | * @return Playback |
||
| 609 | * @throws NotFoundException |
||
| 610 | * @throws ConflictException |
||
| 611 | */ |
||
| 612 | public function playMedia($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Start playback of media and specify the playbackId. The media URI may be any of a number of URI's. |
||
| 619 | * Currently sound: and recording: URI's are supported. This operation creates a playback resource |
||
| 620 | * that can be used to control the playback of media (pause, rewind, fast forward, etc.) |
||
| 621 | * |
||
| 622 | * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback |
||
| 623 | * |
||
| 624 | * @param string $media (required) Media's URI to play. |
||
| 625 | * @param string $lang For sounds, selects language for sound. |
||
| 626 | * @param int $offsetms Number of media to skip before playing. |
||
| 627 | * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations. |
||
| 628 | * @param string $playbackId Playback Id. |
||
| 629 | * @return Playback |
||
| 630 | * @throws NotFoundException |
||
| 631 | * @throws ConflictException |
||
| 632 | */ |
||
| 633 | public function playMediaWithId($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the |
||
| 641 | * channel. The bridge itself has a record feature if that's what you want. |
||
| 642 | * |
||
| 643 | * @param string $name (required) Recording's filename |
||
| 644 | * @param string $format (required) Format to encode audio in |
||
| 645 | * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit. Allowed range: Min: 0; Max: None |
||
| 646 | * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit. Allowed range: Min: 0; Max: None |
||
| 647 | * @param string $ifExists = Action to take if a recording with the same name already exists. default: fail, Allowed values: fail, overwrite, append |
||
| 648 | * @param boolean $beep Play beep when recording begins |
||
| 649 | * @param string $terminateOn DTMF input to terminate recording. Default: none, Allowed values: none, any, *, # |
||
| 650 | * @return LiveRecording |
||
| 651 | * @throws InvalidParameterException |
||
| 652 | * @throws NotFoundException |
||
| 653 | * @throws ConflictException |
||
| 654 | * @throws UnprocessableEntityException |
||
| 655 | */ |
||
| 656 | View Code Duplication | public function record( |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Get the value of a channel variable or function. |
||
| 672 | * |
||
| 673 | * @param string $variable |
||
| 674 | * @param null|string $default The value to return if the variable does not exist |
||
| 675 | * @return string|Variable |
||
| 676 | * @throws InvalidParameterException |
||
| 677 | * @throws NotFoundException |
||
| 678 | * @throws ConflictException |
||
| 679 | */ |
||
| 680 | public function getVariable($variable, $default = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Set the value of a channel variable or function. |
||
| 687 | * |
||
| 688 | * @param string $variable |
||
| 689 | 38 | * @param string $value |
|
| 690 | * @return Variable |
||
| 691 | 38 | * @throws InvalidParameterException |
|
| 692 | * @throws NotFoundException |
||
| 693 | 38 | * @throws ConflictException |
|
| 694 | 38 | */ |
|
| 695 | 38 | public function setVariable($variable, $value) |
|
| 699 | 38 | ||
| 700 | 38 | /** |
|
| 701 | 38 | * Start snooping. Snoop (spy/whisper) on a specific channel. |
|
| 702 | * |
||
| 703 | * @param string $spy (default none) Direction of audio to spy on |
||
| 704 | * @param string $whisper (default none) Direction of audio to whisper into |
||
| 705 | * @param string $app (required) Application the snooping channel is placed into |
||
| 706 | * @param string $appArgs The application arguments to pass to the Stasis application |
||
| 707 | * @param string $snoopId Unique ID to assign to snooping channel |
||
| 708 | * @return Channel |
||
| 709 | * @throws InvalidParameterException |
||
| 710 | * @throws NotFoundException |
||
| 711 | */ |
||
| 712 | public function startSnoop($spy, $whisper, $app, $appArgs, $snoopId) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Start snooping. Snoop (spy/whisper) on a specific channel. |
||
| 719 | * |
||
| 720 | * @param string $spy (default none) Direction of audio to spy on |
||
| 721 | * @param string $whisper (default none) Direction of audio to whisper into |
||
| 722 | * @param string $app (required) Application the snooping channel is placed into |
||
| 723 | * @param string $appArgs The application arguments to pass to the Stasis application |
||
| 724 | * @param string $snoopId Unique ID to assign to snooping channel |
||
| 725 | * @return Channel |
||
| 726 | * @throws InvalidParameterException |
||
| 727 | * @throws NotFoundException |
||
| 728 | */ |
||
| 729 | public function startSnoopWithId($spy, $whisper, $app, $appArgs, $snoopId) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param AriClient $client |
||
| 736 | * @param string $response |
||
| 737 | */ |
||
| 738 | public function __construct(AriClient $client, $response) |
||
| 751 | |||
| 752 | } |
||
| 753 |
This method has been deprecated.