Permalink
Browse files

重構 1 式 : Extract Method

  • Loading branch information...
1 parent c6bac6e commit a8b5872eb489f121d17e49d4ff2cac660db8d56b Sam.IO.Hsiao committed Nov 27, 2016
Showing with 52 additions and 20 deletions.
  1. +52 −20 app/Services/ShippingService.php
@@ -16,35 +16,67 @@ public function calculateFee(array $weightArray, string $companyName): int
switch ($companyName) {
case 'BlackCat':
- $weights = collect($weightArray);
-
- foreach ($weights as $weight) {
- $amount = $amount + (100 + $weight * 10);
- }
+ $amount = $this->blackCatCalculateFee($weightArray, $amount);
break;
case 'Hsinchu':
- $weights = collect($weightArray);
-
- foreach ($weights as $weight) {
- $amount = $amount + (80 + $weight * 15);
- }
+ $amount = $this->hsinchuCalculateFee($weightArray, $amount);
break;
case 'PostOffice':
- $weights = collect($weightArray);
-
- foreach ($weights as $weight) {
- $amount = $amount + (60 + $weight * 20);
- }
+ $amount = $this->postCalculateFee($weightArray, $amount);
break;
default:
- $weights = collect($weightArray);
-
- foreach ($weights as $weight) {
- $amount = $amount + (100 + $weight * 10);
- }
+ $amount = $this->blackCatCalculateFee($weightArray, $amount);
break;
}
return $amount;
}
+
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function blackCatCalculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (100 + $weight * 10);
+ }
+
+ return $amount;
+ }
+
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function hsinchuCalculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (80 + $weight * 15);
+ }
+
+ return $amount;
+ }
+
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function postCalculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (60 + $weight * 20);
+ }
+
+ return $amount;
+ }
}

0 comments on commit a8b5872

Please sign in to comment.