Permalink
Browse files

重構 9 式 : Replace Interface with Closure

  • Loading branch information...
1 parent 6368f90 commit a3c0f64c7c088a831c448c2f8f0947d22afdb82a Sam.IO.Hsiao committed Nov 27, 2016
Showing with 57 additions and 13 deletions.
  1. +41 −0 app/Services/Logistics.php
  2. +4 −3 app/Services/ShippingService.php
  3. +12 −10 tests/ShippingServiceTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Collection;
+
+class Logistics
+{
+ use LogTrait;
+
+ /**
+ * @param array $weightArray
+ * @return Collection
+ */
+ protected function arrayToCollection(array $weightArray): Collection
+ {
+ $weights = collect($weightArray);
+
+ return $weights;
+ }
+
+ /**
+ * @param int $amount
+ * @param array $weightArray
+ * @param callable $closure
+ * @return int
+ */
+ public function calculateFee(array $weightArray, int $amount, callable $closure): int
+ {
+ $weights = $this->arrayToCollection($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + $closure($weight);
+ }
+
+ $this->writeLog($amount);
+
+ return $amount;
+ }
+
+}
@@ -7,13 +7,14 @@ class ShippingService
/**
* 計算運費
* @param array $weightArray
- * @param LogisticsInterface $logistics
+ * @param callable $closure
+ * @param Logistics $logistics
* @return int
*/
- public function calculateFee(array $weightArray, LogisticsInterface $logistics) : int
+ public function calculateFee(array $weightArray, callable $closure, Logistics $logistics) : int
{
$amount = 0;
- return $logistics->calculateFee($weightArray, $amount);
+ return $logistics->calculateFee($weightArray, $amount, $closure);
}
}
@@ -1,9 +1,5 @@
<?php
-use App\Services\BlackCat;
-use App\Services\Hsinchu;
-use App\Services\LogisticsInterface;
-use App\Services\Post;
use App\Services\ShippingService;
class ShippingServiceTest extends TestCase
@@ -12,12 +8,14 @@ class ShippingServiceTest extends TestCase
public function 黑貓_當重量為1_2_3_費用為360()
{
/** arrange */
- App::bind(LogisticsInterface::class, BlackCat::class);
/** act */
$weights = [1, 2, 3];
$actual = App::call(ShippingService::class . '@calculateFee', [
- 'weightArray' => $weights
+ 'weightArray' => $weights,
+ 'closure' => function (int $weight) {
+ return (100 + $weight * 10);
+ },
]);
/** assert */
@@ -29,12 +27,14 @@ public function 黑貓_當重量為1_2_3時_費用為360()
public function 新竹_當重量為1_2_3_費用為330()
{
/** arrange */
- App::bind(LogisticsInterface::class, Hsinchu::class);
/** act */
$weights = [1, 2, 3];
$actual = App::call(ShippingService::class . '@calculateFee', [
- 'weightArray' => $weights
+ 'weightArray' => $weights,
+ 'closure' => function (int $weight) {
+ return (80 + $weight * 15);
+ },
]);
/** assert */
@@ -46,12 +46,14 @@ public function 新竹_當重量為1_2_3時_費用為330()
public function 郵局_當重量為1_2_3_費用為300()
{
/** arrange */
- App::bind(LogisticsInterface::class, Post::class);
/** act */
$weights = [1, 2, 3];
$actual = App::call(ShippingService::class . '@calculateFee', [
- 'weightArray' => $weights
+ 'weightArray' => $weights,
+ 'closure' => function (int $weight) {
+ return (60 + $weight * 20);
+ },
]);
/** assert */

0 comments on commit a3c0f64

Please sign in to comment.