Skip to content

Commit

Permalink
Added AbstractPurchase, ErrorPurchaseException
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Jan 14, 2016
1 parent a62c53e commit cb53d22
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/cart/ErrorPurchaseException.php
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';
}
}
76 changes: 76 additions & 0 deletions src/models/AbstractPurchase.php
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'],
];
}
}

0 comments on commit cb53d22

Please sign in to comment.