Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace hiqdev\billing\hiapi\sale\Close; | ||
|
||
use hiapi\endpoints\Module\Multitenant\Tenant; | ||
use hiapi\Core\Endpoint\BuilderFactory; | ||
use hiapi\Core\Endpoint\Endpoint; | ||
use hiapi\Core\Endpoint\EndpointBuilder; | ||
use hiqdev\php\billing\sale\Sale; | ||
use hiqdev\billing\hiapi\customer\CustomerLoader; | ||
use hiqdev\billing\hiapi\plan\PlanLoader; | ||
use hiqdev\billing\hiapi\target\TargetLoader; | ||
|
||
final class SaleBulkClose | ||
{ | ||
public function __invoke(BuilderFactory $build): Endpoint | ||
{ | ||
return $this->create($build)->build(); | ||
} | ||
|
||
public function create(BuilderFactory $build): EndpointBuilder | ||
{ | ||
return $build->endpoint(self::class) | ||
->exportTo(Tenant::ALL) | ||
->take($build->many(SaleCloseCommand::class)) | ||
// XXX anybody can purchase? ->checkPermission('object.buy') | ||
->middlewares( | ||
$build->repeat(CustomerLoader::class), | ||
$build->repeat(PlanLoader::class), | ||
$build->repeat(TargetLoader::class), | ||
$build->repeat(SaleCloseAction::class) | ||
) | ||
->return($build->many(Sale::class)) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace hiqdev\billing\hiapi\sale\Close; | ||
|
||
use hiapi\endpoints\Module\Multitenant\Tenant; | ||
use hiapi\Core\Endpoint\BuilderFactory; | ||
use hiapi\Core\Endpoint\Endpoint; | ||
use hiapi\Core\Endpoint\EndpointBuilder; | ||
use hiqdev\php\billing\sale\Sale; | ||
use hiqdev\billing\hiapi\customer\CustomerLoader; | ||
use hiqdev\billing\hiapi\plan\PlanLoader; | ||
use hiqdev\billing\hiapi\target\TargetLoader; | ||
|
||
final class SaleClose | ||
{ | ||
public function __invoke(BuilderFactory $build): Endpoint | ||
{ | ||
return $this->create($build)->build(); | ||
} | ||
|
||
public function create(BuilderFactory $build): EndpointBuilder | ||
{ | ||
return $build->endpoint(self::class) | ||
->exportTo(Tenant::ALL) | ||
->take(SaleCloseCommand::class) | ||
// XXX anybody can purchase? ->checkPermission('object.buy') | ||
->middlewares( | ||
CustomerLoader::class, | ||
PlanLoader::class, | ||
TargetLoader::class, | ||
$build->call(SaleCloseAction::class) | ||
) | ||
->return(Sale::class) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace hiqdev\billing\hiapi\sale\Close; | ||
|
||
use hiqdev\billing\hiapi\sale\SaleRepository; | ||
use hiqdev\php\billing\plan\Plan; | ||
use hiqdev\php\billing\sale\Sale; | ||
use hiapi\exceptions\domain\RequiredInputException; | ||
|
||
class SaleCloseAction | ||
{ | ||
/** | ||
* @var SaleRepository | ||
*/ | ||
private $repo; | ||
|
||
public function __construct(SaleRepository $repo) | ||
{ | ||
$this->repo = $repo; | ||
} | ||
|
||
public function __invoke(SaleCloseCommand $command): Sale | ||
{ | ||
$this->checkRequiredInput($command); | ||
$plan = new Plan($command->plan_id, null); | ||
$sale = new Sale(null, $command->target, $command->customer, $plan, $command->time); | ||
$this->repo->delete($sale); | ||
|
||
return $sale; | ||
} | ||
|
||
protected function checkRequiredInput(SaleCloseCommand $command) | ||
{ | ||
if (empty($command->customer)) { | ||
throw new RequiredInputException('customer'); | ||
} | ||
if (empty($command->target)) { | ||
throw new RequiredInputException('target'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace hiqdev\billing\hiapi\sale\Close; | ||
|
||
use hiapi\commands\BaseCommand; | ||
use hiapi\validators\IdValidator; | ||
|
||
class SaleCloseCommand extends BaseCommand | ||
{ | ||
public $customer_id; | ||
|
||
public $plan_id; | ||
|
||
public $target_id; | ||
|
||
public $time; | ||
|
||
public $customer; | ||
|
||
public $plan; | ||
|
||
public $target; | ||
|
||
public function rules() | ||
{ | ||
return [ | ||
[['customer_id'], IdValidator::class], | ||
|
||
[['plan_id'], IdValidator::class], | ||
|
||
[['target_id'], IdValidator::class], | ||
[['target_id'], 'required'], | ||
|
||
[['time'], 'datetime'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters