Skip to content

Commit

Permalink
Stragety に Order も渡せるようにした
Browse files Browse the repository at this point in the history
- OrderDetails の引数は要らない子か...
  • Loading branch information
nanasess committed Dec 2, 2016
1 parent a60b8e4 commit b034643
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace Eccube\Service\Calculator\Strategy;

use Eccube\Application;
use Eccube\Entity\Order;

interface CalculateStrategyInterface
{
// 引数に型パラメータを指定したい
public function execute(&$OrderDetails);

public function setApplication(Application $app);

public function setOrder(Order $Order);
}
33 changes: 30 additions & 3 deletions src/Eccube/Service/Calculator/Strategy/ShippingStrategy.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
<?php
namespace Eccube\Service\Calculator\Strategy;

use Eccube\Application;
use Eccube\Entity\Order;
use Eccube\Entity\OrderDetail;

class ShippingStrategy implements CalculateStrategyInterface
{
protected $app;
protected $Order;

public function __construct($app)
public function __construct(Application $app = null)
{
$this->app = $app;
}

public function execute(&$OrderDetails)
{
// 送料をすべて足す
$delivery_fee_total = array_reduce(
array_map(
function ($Shipping) {
return $Shipping->getShippingDeliveryFee();
},
$this->Order->getShippings()->toArray()
),
function ($carry, $item) {
return $carry += $item;
}
);
$OrderDetail = new OrderDetail();
$OrderDetail->setProductName("送料")
->setPrice(1000)
->setPriceIncTax(1000)
->setPrice($delivery_fee_total)
->setPriceIncTax($delivery_fee_total)
->setQuantity(1);
$OrderDetails[] = $OrderDetail;
}

public function setApplication(Application $app)
{
$this->app = $app;
return $this;
}

public function setOrder(Order $Order)
{
$this->Order = $Order;
return $this;
}
}
18 changes: 17 additions & 1 deletion src/Eccube/Service/Calculator/Strategy/TaxStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace Eccube\Service\Calculator\Strategy;

use Eccube\Application;
use Eccube\Entity\Order;
use Eccube\Entity\OrderDetail;

class TaxStrategy implements CalculateStrategyInterface
{
protected $app;
protected $Order;

public function __construct($app)
public function __construct(Application $app = null)
{
$this->app = $app;
}

public function execute(&$OrderDetails)
{
foreach ($OrderDetails as &$OrderDetail) {
Expand All @@ -20,4 +24,16 @@ public function execute(&$OrderDetails)
$OrderDetail->setPriceIncTax($OrderDetail->getPrice() + $tax);
}
}

public function setApplication(Application $app)
{
$this->app = $app;
return $this;
}

public function setOrder(Order $Order)
{
$this->Order = $Order;
return $this;
}
}
24 changes: 15 additions & 9 deletions src/Eccube/ServiceProvider/EccubeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,31 @@ public function register(BaseApplication $app)
$app['eccube.service.calculate'] = $app->protect(function ($Order, $Customer) use ($app) {
$Service = new \Eccube\Service\CalculateService($Order, $Customer);
$Context = $app['eccube.calculate.context'];
$Context->setCalculateStrategies($app['eccube.calculate.strategies']);
$Context->setCalculateStrategies($app['eccube.calculate.strategies']($Order));
$Context->setOrder($Order);
$Service->setContext($Context);
return $Service;
});

$app['eccube.calculate.strategies'] = function () use ($app) {
$app['eccube.calculate.strategies'] = $app->protect(function ($Order) use ($app) {
// デフォルトのストラテジーをセットしておく
$Strategies = [ // ArrayIterator にしたい
$app['eccube.calculate.strategy.shipping'],
$app['eccube.calculate.strategy.tax']
$app['eccube.calculate.strategy.shipping']($Order),
$app['eccube.calculate.strategy.tax']($Order)
];
return $Strategies;
};
$app['eccube.calculate.strategy.shipping'] = $app->share(function () use ($app) {
return new \Eccube\Service\Calculator\Strategy\ShippingStrategy($app);
});
$app['eccube.calculate.strategy.tax'] = $app->share(function () use ($app) {
return new \Eccube\Service\Calculator\Strategy\TaxStrategy($app);
$app['eccube.calculate.strategy.shipping'] = $app->protect(function ($Order) use ($app) {
$Strategy = new \Eccube\Service\Calculator\Strategy\ShippingStrategy();
$Strategy->setApplication($app);
$Strategy->setOrder($Order);
return $Strategy;
});
$app['eccube.calculate.strategy.tax'] = $app->protect(function ($Order) use ($app) {
$Strategy = new \Eccube\Service\Calculator\Strategy\TaxStrategy();
$Strategy->setApplication($app);
$Strategy->setOrder($Order);
return $Strategy;
});
$app['eccube.service.csv.export'] = $app->share(function () use ($app) {
$csvService = new \Eccube\Service\CsvExportService();
Expand Down

0 comments on commit b034643

Please sign in to comment.