Phalcon Entity

<?php
/**
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 *
 * Encoding: UTF-8
 *
 * @package CesarEngineExample\Entity
 * @author cromero - 2015-10-30 14:59:34
 */

namespace CesarEngineExample\Entity;

use CesarEngineExample\Entity\Base\AbstractTransaction;

/**
 * Transaction
 *
 * @QueryParser(
 *   filters={
 *     "id", "transaction_type_id", "provider_id", "provider_config_id", "merchant_id", "player_id",
 *     "game_id", "game_config_id", "currency_code", "external_round_id",
 *     "external_transaction_id", "success", "modified_balance"
 *   },
 *   ignore={"group"},
 *   defaults={
 *     "limit"=500
 *   }
 * )
 *
 * @package CesarEngineExample\Entity
 * @uses \CesarEngineExample\Entity\Base\AbstractTransaction
 *
 * @method static \CesarEngineExample\Entity\Transaction|boolean findFirstByProviderTransactionId(integer $id) Get Transaction
 *         by Provider Transaction ID
 */
class Transaction extends AbstractTransaction
{
    /**
     * Real amount of Bet
     *
     * @var integer
     */
    protected $bet_amount_real = 0;

    /**
     * Bonus amount of Bet
     *
     * @var integer
     */
    protected $bet_amount_bonus = 0;

    /**
     * Real amount of Win
     *
     * @var integer
     */
    protected $win_amount_real = 0;

    /**
     * Bonus amount of Win
     *
     * @var integer
     */
    protected $win_amount_bonus = 0;

    /**
     * Whether the Transaction was successful or not
     *
     * @var boolean
     */
    protected $success = 0;

    /**
     * Whether the Player balance has been modified during the Transaction or no
     *
     * @var boolean
     */
    protected $modified_balance = 0;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('id', 'CesarEngineExample\Entity\Latency', 'transaction_id', array(
            'alias' => 'Latency',
            'foreignKey' => array(
                'message' => 'Unable to delete the Transaction; detected a dependency with Logs.',
            ),
        ));
        $this->hasMany('id', 'CesarEngineExample\Entity\MerchantCommunication', 'transaction_id', array(
            'alias' => 'MerchantCommunication',
            'foreignKey' => array(
                'message' => 'Unable to delete the Transaction; detected a dependency with Logs.',
            ),
        ));
        $this->hasMany('id', 'CesarEngineExample\Entity\ProviderCommunication', 'transaction_id', array(
            'alias' => 'ProviderCommunication',
            'foreignKey' => array(
                'message' => 'Unable to delete the Transaction; detected a dependency with Logs.',
            ),
        ));
        $this->belongsTo('transaction_type_id', 'CesarEngineExample\Entity\TransactionType', 'id', array(
            'alias' => 'TransactionType',
            'foreignKey' => array(
                'message' => 'Transaction Type ID has not been set or it is not valid.',
            ),
        ));
        $this->belongsTo('merchant_id', 'CesarEngineExample\Entity\Merchant', 'id', array(
            'alias' => 'Merchant',
            'foreignKey' => array(
                'message' => 'Merchant ID has not been set or it is not valid.',
            ),
        ));
        $this->belongsTo('provider_configuration_id', 'CesarEngineExample\Entity\ProviderConfiguration', 'id', array(
            'alias' => 'ProviderConfiguration',
            'foreignKey' => array(
                'message' => 'Provider Configuration ID has not been set or it is not valid.',
            ),
        ));
        $this->belongsTo('player_id', 'CesarEngineExample\Entity\Player', 'id', array(
            'alias' => 'Player',
            'foreignKey' => array(
                'message' => 'Player ID has not been set or it is not valid.',
            ),
        ));
        $this->belongsTo('player_token_id', 'CesarEngineExample\Entity\PlayerToken', 'id', array(
            'alias' => 'PlayerToken',
            'foreignKey' => array(
                'allowNulls' => true,
                'message' => 'Player Token ID is not valid.',
            ),
        ));
        $this->belongsTo('game_configuration_id', 'CesarEngineExample\Entity\GameConfiguration', 'id', array(
            'alias' => 'GameConfiguration',
            'foreignKey' => array(
                'message' => 'Game Configuration ID has not been set or it is not valid.',
            ),
        ));
        $this->belongsTo('currency_code', 'CesarEngineExample\Entity\Currency', 'code', array(
            'alias' => 'Currency',
            'foreignKey' => array(
                'message' => 'Currency Code has not been set or it is not valid.',
            ),
        ));
    }

    /**
     * Normalize fields before creation
     *
     * @return boolean
     */
    public function beforeValidationOnCreate()
    {
        if ($this->provider_configuration_id
            && false !== ($model = ProviderConfiguration::findFirstById($this->provider_configuration_id))
        ) {
            $this->provider_id = $model->getProviderId();
        }
        if ($this->game_configuration_id
            && false !== ($model = GameConfiguration::findFirstById($this->game_configuration_id))
        ) {
            $this->game_id = $model->getGameId();
        }
        return true;
    }

    /**
     * Disallow to set the value
     *
     * @param integer $provider_id
     * @return \CesarEngineExample\Entity\Transaction
     * @throws \DomainException
     */
    public function setProviderId($provider_id)
    {
        throw new \DomainException('You are not allowed to set the Provider ID.');
    }

    /**
     * Disallow to set the value
     *
     * @param integer $game_id
     * @return \CesarEngineExample\Entity\Transaction
     * @throws \DomainException
     */
    public function setGameId($game_id)
    {
        throw new \DomainException('You are not allowed to set the Game ID.');
    }

    /**
     * One way mapping of the provider transaction
     *
     * @param integer $providerId
     * @param string $transactionId
     * @return string
     */
    public function transformProviderTransaction($providerId, $transactionId)
    {
        return  chr(96 + $providerId) . $transactionId;
    }

    /**
     * One way mapping of the provider round
     *
     * @param integer $providerId
     * @param string $roundId
     * @return string
     */
    public function transformProviderRound($providerId, $roundId)
    {
        return $providerId . '000' . $roundId;
    }
}