public function loadData() { file_put_contents($this->dataFile, ''); $newSql = new SqliteUtils($this->dataFile); $newSql->createDbStructureAsNecessary(); $results = $newSql->recordData('directory', 'user', '{"primaryEmail":"*****@*****.**","id":1,"password":"******"}'); $results = $newSql->recordData('directory', 'users_alias', '{"primaryEmail":"*****@*****.**","alias":"*****@*****.**"}'); $results = $newSql->recordData('app_engine', 'webapp', 'webapp3 test data'); $results = $newSql->recordData('directory', 'user', '{"primaryEmail":"*****@*****.**","id":4,"password":"******"}'); $results = $newSql->recordData('directory', 'users_alias', '{"primaryEmail":"*****@*****.**","alias":"*****@*****.**"}'); return $newSql; }
/** * Adds data to the database based on the input * * @param array of arrays $fixtures ... * Each array should have 3 string elements ... * - the type of Google Service (e.g. "directory") * - the class (e.g. "user") * - the corresponding json formatted data */ public function addFixtures($fixtures) { $newSqlite = new SqliteUtils($this->_dbFile); foreach ($fixtures as $nextFixture) { $type = $nextFixture[0]; $class = $nextFixture[1]; $data = $nextFixture[2]; $newSqlite->recordData($type, $class, $data); } }
/** * Creates a user (users.insert) and sets its aliases property if any * are given. * * @param Google_User $postBody * @return null|a real Google_Service_Directory_User instance * @throws \Exception with code 201407101120, if the user already exists */ public function insert($postBody) { $defaults = array('id' => intval(str_replace(array(' ', '.'), '', microtime())), 'suspended' => false, 'changePasswordAtNextLogin' => false, 'isAdmin' => false, 'isDelegatedAdmin' => false, 'lastLoginTime' => time(), 'creationTime' => time()); // array_merge will not work, since $postBody is an object which only // implements ArrayAccess foreach ($defaults as $key => $value) { if (!isset($postBody[$key])) { $postBody[$key] = $value; } } $currentUser = $this->get($postBody->primaryEmail); if ($currentUser) { throw new \Exception("Account already exists: " . $postBody['primaryEmail'], 201407101120); } $newUser = new \Google_Service_Directory_User(); ObjectUtils::initialize($newUser, $postBody); $userData = json_encode($newUser); // record the user in the database $sqliteUtils = new SqliteUtils($this->_dbFile); $sqliteUtils->recordData($this->_dataType, $this->_dataClass, $userData); // record the user's aliases in the database if ($postBody->aliases) { $usersAliases = new UsersAliasesResource($this->_dbFile); foreach ($postBody->aliases as $alias) { $newAlias = new Alias(); $newAlias->alias = $alias; $newAlias->kind = "personal"; $newAlias->primaryEmail = $postBody->primaryEmail; $insertedAlias = $usersAliases->insertAssumingUserExists($newAlias); } } // Get (and return) the new user that was just created back out of the database return $this->get($postBody->primaryEmail); }
/** * Adds an alias for a user that it assumes is already in the database (aliases.insert) * * @param Alias $postBody The array/object with the data for that alias * @return Alias - a real Google_Service_Directory_Alias instance */ public function insertAssumingUserExists($postBody) { $entryData = json_encode(get_object_vars($postBody)); $sqliteUtils = new SqliteUtils($this->_dbFile); $sqliteUtils->recordData($this->_dataType, $this->_dataClass, $entryData, true); $allAliases = $sqliteUtils->getData($this->_dataType, $this->_dataClass); if (!$allAliases) { return null; } $newAlias = new \Google_Service_Directory_Alias(); ObjectUtils::initialize($newAlias, $postBody); return $newAlias; }