/** * Create a Voucher Rule * * @param VoucherGenRule $oVoucherRule The Voucher Gen Rule * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherGenRule $oVoucherRule) { $oGateway = $this->oGateway; $oBuilder = $oGateway->getEntityBuilder(); if (false === empty($oVoucherRule->getVoucherGenRuleId())) { throw new VoucherException('Unable to create new voucher rule the Entity has a database id assigned already'); } try { $oQuery = $oGateway->insertQuery()->start(); foreach ($oBuilder->demolish($oVoucherRule) as $sColumn => $mValue) { if ($sColumn !== 'voucher_gen_rule_id' && $sColumn !== 'date_created') { $oQuery->addColumn($sColumn, $mValue); } elseif ($sColumn === 'date_created') { $oQuery->addColumn('date_created', $this->oNow); } } $bSuccess = $oQuery->end()->insert(); if ($bSuccess) { $oVoucherRule->setVoucherGenRuleId($oGateway->lastInsertId()); } } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $bSuccess; }
/** * Update a Voucher Rule * * @param VoucherGenRule $oVoucherRule The Voucher Gen Rule * @throws VoucherException if the database query fails or entity has id assigned. * @returns boolean true if the insert operation was successful */ public function execute(VoucherGenRule $oVoucherRule) { $oGateway = $this->oGateway; $oBuilder = $oGateway->getEntityBuilder(); if (true === empty($oVoucherRule->getVoucherGenRuleId())) { throw new VoucherException('Unable to update voucher rule the Entity requires a database id be assigned'); } try { $oQuery = $oGateway->updateQuery()->start(); foreach ($oBuilder->demolish($oVoucherRule) as $sColumn => $mValue) { if ($sColumn !== 'voucher_gen_rule_id' && $sColumn !== 'date_created') { $oQuery->addColumn($sColumn, $mValue); } } $bSuccess = $oQuery->where()->filterByRule($oVoucherRule->getVoucherGenRuleId())->end()->update(); } catch (DBALGatewayException $e) { throw new VoucherException($e->getMessage(), 0, $e); } return $bSuccess; }
/** * Convert data array into entity * * @return VoucherGroup * @param array $data * @access public */ public function build($data) { $oEntity = new VoucherGenRule(); $sAlias = $this->getTableQueryAlias(); $iVoucherGenId = $this->getField($data, 'voucher_gen_rule_id', $sAlias); $sVoucherRuleNameSlug = $this->getField($data, 'voucher_rule_slug', $sAlias); $sVoucherRuleName = $this->getField($data, 'voucher_rule_name', $sAlias); $sVoucherPaddingCharacter = $this->getField($data, 'voucher_padding_char', $sAlias); $sVoucherSuffix = $this->getField($data, 'voucher_suffix', $sAlias); $sVoucherPrefix = $this->getField($data, 'voucher_prefix', $sAlias); $iVoucherLength = $this->getField($data, 'voucher_length', $sAlias); $oDateCreated = $this->getField($data, 'date_created', $sAlias); $sSequenceStrategy = $this->getField($data, 'voucher_sequence_strategy', $sAlias); $oEntity->setVoucherGenRuleId($iVoucherGenId); $oEntity->setSlugRuleName($sVoucherRuleNameSlug); $oEntity->setVoucherRuleName($sVoucherRuleName); $oEntity->setVoucherPaddingCharacter($sVoucherPaddingCharacter); $oEntity->setVoucherSuffix($sVoucherSuffix); $oEntity->setVoucherPrefix($sVoucherPrefix); $oEntity->setVoucherLength($iVoucherLength); $oEntity->setDateCreated($oDateCreated); $oEntity->setSequenceStrategyName($sSequenceStrategy); return $oEntity; }
public function testVoucherEntityFailure() { $oRule = new VoucherGenRule(); $iVoucherGeneratorRuleId = 1; $sVoucherRuleNameSlug = ''; $sVoucherRuleName = ''; $sVoucherPaddingCharacter = 'A'; $sVoucherSuffix = str_repeat('a', 100); $sVoucherPrefix = str_repeat('a', 100); $iVoucherLength = 101; $oDateCreated = new DateTime(); $sSequenceStrategy = 'BAD'; $oRule->setVoucherGenRuleId($iVoucherGeneratorRuleId); $oRule->setSlugRuleName($sVoucherRuleNameSlug); $oRule->setVoucherRuleName($sVoucherRuleName); $oRule->setVoucherPaddingCharacter($sVoucherPaddingCharacter); $oRule->setVoucherSuffix($sVoucherSuffix); $oRule->setVoucherPrefix($sVoucherPrefix); $oRule->setVoucherLength($iVoucherLength); $oRule->setDateCreated($oDateCreated); $oRule->setSequenceStrategyName($sSequenceStrategy); $oResults = $oRule->validate(); $this->assertInternalType('array', $oResults); $this->assertEquals(count($oResults['voucherSuffix']), 1); $this->assertEquals(count($oResults['voucherPrefix']), 1); $this->assertEquals(count($oResults['sequenceStrategy']), 1); $this->assertEquals(count($oResults['voucherRuleName']), 1); $this->assertEquals(count($oResults['slugName']), 3); $this->assertEquals(count($oResults['voucherLength']), 1); }
public function testVoucherRuleUpdate() { $oContainer = $this->getContainer(); $aOperations = $oContainer->getVoucherRuleOperations(); $oOperation = $aOperations['update']; # assert correct operation was returned $this->assertInstanceOf('\\IComeFromTheNet\\Ledger\\Voucher\\Operations\\RuleRevise', $oOperation); $oRule = new VoucherGenRule(); $oRule->setVoucherRuleName('Rule A'); $oRule->setSlugRuleName('rule_a'); $oRule->setVoucherPaddingCharacter('a'); $oRule->setVoucherSuffix('_rule'); $oRule->setVoucherPrefix('my_'); $oRule->setVoucherLength(5); $oRule->setSequenceStrategyName('SEQUENCE'); $oRule->setVoucherGenRuleId(1); $oOperation->execute($oRule); $this->assertNotEmpty($oRule->getVoucherGenRuleId()); }