Permalink
Browse files

重構 3 式 : Extract Super Class

  • Loading branch information...
1 parent a724b60 commit 82be134f7a5c4488d1b4a4f0515eeb29534a9bad Sam.IO.Hsiao committed Nov 27, 2016
Showing with 27 additions and 6 deletions.
  1. +19 −0 app/Services/AbstractLogistics.php
  2. +4 −2 app/Services/BlackCat.php
  3. +2 −2 app/Services/Hsinchu.php
  4. +2 −2 app/Services/Post.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Collection;
+
+abstract class AbstractLogistics
+{
+ /**
+ * @param array $weightArray
+ * @return Collection
+ */
+ protected function arrayToCollection(array $weightArray): Collection
+ {
+ $weights = collect($weightArray);
+
+ return $weights;
+ }
+}
@@ -2,7 +2,9 @@
namespace App\Services;
-class BlackCat
+use Illuminate\Support\Collection;
+
+class BlackCat extends AbstractLogistics
{
/**
* @param array $weightArray
@@ -11,7 +13,7 @@ class BlackCat
*/
public function calculateFee(array $weightArray, int $amount) : int
{
- $weights = collect($weightArray);
+ $weights = $this->arrayToCollection($weightArray);
foreach ($weights as $weight) {
$amount = $amount + (100 + $weight * 10);
@@ -2,7 +2,7 @@
namespace App\Services;
-class Hsinchu
+class Hsinchu extends AbstractLogistics
{
/**
* @param array $weightArray
@@ -11,7 +11,7 @@ class Hsinchu
*/
public function calculateFee(array $weightArray, int $amount) : int
{
- $weights = collect($weightArray);
+ $weights = $this->arrayToCollection($weightArray);
foreach ($weights as $weight) {
$amount = $amount + (80 + $weight * 15);
@@ -2,7 +2,7 @@
namespace App\Services;
-class Post
+class Post extends AbstractLogistics
{
/**
* @param array $weightArray
@@ -11,7 +11,7 @@ class Post
*/
public function calculateFee(array $weightArray, int $amount) : int
{
- $weights = collect($weightArray);
+ $weights = $this->arrayToCollection($weightArray);
foreach ($weights as $weight) {
$amount = $amount + (60 + $weight * 20);

0 comments on commit 82be134

Please sign in to comment.