Permalink
Browse files

重構 2 式 : Extract Class

  • Loading branch information...
1 parent a8b5872 commit a724b603159d67785688192dd23d0cb6851e76dd Sam.IO.Hsiao committed Nov 27, 2016
Showing with 78 additions and 46 deletions.
  1. +22 −0 app/Services/BlackCat.php
  2. +22 −0 app/Services/Hsinchu.php
  3. +22 −0 app/Services/Post.php
  4. +12 −46 app/Services/ShippingService.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Services;
+
+class BlackCat
+{
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function calculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (100 + $weight * 10);
+ }
+
+ return $amount;
+ }
+}
@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Services;
+
+class Hsinchu
+{
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function calculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (80 + $weight * 15);
+ }
+
+ return $amount;
+ }
+}
View
@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Services;
+
+class Post
+{
+ /**
+ * @param array $weightArray
+ * @param int $amount
+ * @return int
+ */
+ public function calculateFee(array $weightArray, int $amount) : int
+ {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (60 + $weight * 20);
+ }
+
+ return $amount;
+ }
+}
@@ -16,67 +16,33 @@ public function calculateFee(array $weightArray, string $companyName): int
switch ($companyName) {
case 'BlackCat':
- $amount = $this->blackCatCalculateFee($weightArray, $amount);
+ $blackCat = new BlackCat();
+ $amount = $blackCat->calculateFee($weightArray, $amount);
+
break;
case 'Hsinchu':
- $amount = $this->hsinchuCalculateFee($weightArray, $amount);
+ $hsinchu = new Hsinchu();
+ $amount = $hsinchu->calculateFee($weightArray, $amount);
+
break;
case 'PostOffice':
- $amount = $this->postCalculateFee($weightArray, $amount);
+ $post = new Post();
+ $amount = $post->calculateFee($weightArray, $amount);
+
break;
default:
- $amount = $this->blackCatCalculateFee($weightArray, $amount);
+ $blackCat = new BlackCat();
+ $amount = $blackCat->calculateFee($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 a724b60

Please sign in to comment.