/** * Given a collection type and Item (Either User or Person), try to find an existing model. If the model does * not exist, create it and populate the Item and type. * @param string $type * @param Item $person * @return The found or created model. * @throws NotSupportedException */ public static function resolveByTypeAndPerson($type, Item $person) { assert('is_string($type)'); assert('$person->id > 0'); assert('$person instanceof Contact || $person instanceof User'); $searchAttributeData = array(); $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'equals', 'value' => $type), 2 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $person->getClassId('Item'))); $searchAttributeData['structure'] = '1 and 2'; $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GameCollection'); $where = RedBeanModelDataProvider::makeWhere('GameCollection', $searchAttributeData, $joinTablesAdapter); $models = self::getSubset($joinTablesAdapter, null, 2, $where, null); if (count($models) > 1) { $logContent = 'Duplicate Game Collection for Person: ' . $person->id . ' with type ' . $type; GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent); return $models[0]; } if (count($models) == 0) { $gameCollectionRules = GameCollectionRulesFactory::createByType($type); $gameCollection = new GameCollection(); $gameCollection->type = $type; $gameCollection->person = $person; $gameCollection->serializedData = serialize($gameCollectionRules::makeDefaultData()); return $gameCollection; } return $models[0]; }
/** * The best we can cover for is making sure the notification is created and it is not marked as critical. */ public function testLogAndNotifyOnDuplicateGameModel() { $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(0, Yii::app()->emailHelper->getSentCount()); $this->assertEquals(0, count(Notification::getAll())); GamificationUtil::logAndNotifyOnDuplicateGameModel('some content'); //It should not send an email because it is non-critical $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(0, Yii::app()->emailHelper->getSentCount()); $this->assertEquals(1, count(Notification::getAll())); }
/** * Given an Item (Either User or Person), try to find an existing model. If the model does * not exist, create it and populate the Item. * @param Item $person * @return The found or created model. * @throws NotSupportedException */ public static function resolveByPerson(Item $person) { assert('$person->id > 0'); assert('$person instanceof Contact || $person instanceof User'); $searchAttributeData = array(); $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $person->getClassId('Item'))); $searchAttributeData['structure'] = '1'; $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GameCoin'); $where = RedBeanModelDataProvider::makeWhere('GameCoin', $searchAttributeData, $joinTablesAdapter); $models = self::getSubset($joinTablesAdapter, null, 2, $where, null); if (count($models) > 1) { $logContent = 'Duplicate Game Coin for Person: ' . $person->id; GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent); return $models[0]; } if (count($models) == 0) { $gameCoin = new GameCoin(); $gameCoin->person = $person; $gameCoin->value = 0; return $gameCoin; } return $models[0]; }