-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AbstractPurchase, ErrorPurchaseException
- Loading branch information
1 parent
a62c53e
commit cb53d22
Showing
2 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace hipanel\modules\finance\cart; | ||
|
||
use yii\base\Exception; | ||
|
||
/** | ||
* Exception represents an exception occurred during cart position purchase | ||
* @package hipanel\modules\finance\cart | ||
*/ | ||
class ErrorPurchaseException extends Exception | ||
{ | ||
/** | ||
* @var AbstractCartPosition | ||
*/ | ||
public $position; | ||
|
||
/** | ||
* ErrorPurchaseException constructor | ||
* | ||
* @param string $message | ||
* @param AbstractCartPosition $position | ||
* @param Exception $previous | ||
*/ | ||
public function __construct($message, $position, Exception $previous) | ||
{ | ||
$this->position = $position; | ||
parent::__construct($message, null, $previous); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getName() | ||
{ | ||
return 'Error occurred during item "' . $this->position->getName() . '"" purchase'; | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
namespace hipanel\modules\finance\models; | ||
|
||
use hipanel\modules\finance\cart\AbstractCartPosition; | ||
use Yii; | ||
use yii\base\InvalidConfigException; | ||
|
||
/** | ||
* Class Purchase | ||
* @package hipanel\modules\finance\models | ||
*/ | ||
abstract class AbstractPurchase extends \hipanel\base\Model | ||
{ | ||
use \hipanel\base\ModelTrait; | ||
|
||
/** | ||
* @var AbstractCartPosition | ||
*/ | ||
protected $position; | ||
|
||
/** @inheritdoc */ | ||
public static function index() | ||
{ | ||
throw new InvalidConfigException('Method "index" must be declared'); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public static function type() | ||
{ | ||
throw new InvalidConfigException('Method "index" must be declared'); | ||
|
||
} | ||
|
||
/** @inheritdoc */ | ||
public static function primaryKey() | ||
{ | ||
return ['cart_position_id']; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->synchronize(); | ||
} | ||
|
||
/** | ||
* Synchronises the model to represent actual state of [[position]] | ||
* The method must update values, that affects the calculation and | ||
* can be changed in cart without position re-adding. | ||
* For example: quantity | ||
*/ | ||
public function synchronize() | ||
{ | ||
$this->cart_position_id = $this->position->getId(); | ||
$this->amount = $this->position->getQuantity(); | ||
} | ||
|
||
/** | ||
* Executes the purchase. | ||
* Calls proper API commands to purchase the product. | ||
* @return boolean whether the item was purchased | ||
*/ | ||
abstract public function execute(); | ||
|
||
/** @inheritdoc */ | ||
public function rules() | ||
{ | ||
return [ | ||
[['cart_position_id', 'object', 'client', 'type', 'currency', 'item'], 'safe'], | ||
[['amount'], 'number'], | ||
]; | ||
} | ||
} |