Skip to content

Commit

Permalink
Merge pull request #40 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 0.9.0
  • Loading branch information
mtompset authored Jan 11, 2021
2 parents 165c63a + a7b5b42 commit fdb9089
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 80 deletions.
14 changes: 6 additions & 8 deletions SilMock/Google/Service/Directory/ObjectUtils.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<?php
namespace SilMock\Google\Service\Directory;

namespace SilMock\Google\Service\Directory;

class ObjectUtils
{

/**
* Assigns given values to the matching properties of a Google Mock object
*
* @param object $newObject -- a Google mock object
* @param object|array $properties -- object|associative array
* @returns null
**/
public static function initialize($newObject, $properties)
* @return void
*/
public static function initialize(object $newObject, $properties)
{
$propArray = $properties;
if (is_object($properties)) {
$propArray = get_object_vars($properties);
}

foreach ($propArray as $key=>$value) {
foreach ($propArray as $key => $value) {
$newObject->$key = $value;
}
}

}
}
40 changes: 24 additions & 16 deletions SilMock/Google/Service/Directory/UsersAliasesResource.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

namespace SilMock\Google\Service\Directory;

use SilMock\DataStore\Sqlite\SqliteUtils;

class UsersAliasesResource {
class UsersAliasesResource
{

private $_dbFile; // string for the path (with file name) for the Sqlite database
private $_dataType = 'directory'; // string to put in the 'type' field in the database
private $_dataClass = 'users_alias'; // string to put in the 'class' field in the database


public function __construct($dbFile=null)
public function __construct($dbFile = null)
{
$this->_dbFile = $dbFile;
}
Expand All @@ -27,9 +29,8 @@ public function delete($userKey, $alias)
{
// If the $userKey is not an email address, it must be an id
$key = 'primaryEmail';
if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

// ensure that user exists in db
Expand All @@ -42,10 +43,14 @@ public function delete($userKey, $alias)

// Get all the aliases for that user
$sqliteUtils = new SqliteUtils($this->_dbFile);
$aliases = $sqliteUtils->getAllRecordsByDataKey($this->_dataType,
$this->_dataClass, $key, $userKey);
$aliases = $sqliteUtils->getAllRecordsByDataKey(
$this->_dataType,
$this->_dataClass,
$key,
$userKey
);

if ( ! $aliases) {
if (! $aliases) {
return null;
}

Expand Down Expand Up @@ -74,9 +79,8 @@ public function insert($userKey, $postBody)
{
// If the $userKey is not an email address, it must be an id
$key = 'primaryEmail';
if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

// ensure that user exists in db
Expand Down Expand Up @@ -111,7 +115,7 @@ public function insertAssumingUserExists($postBody)
);
$allAliases = $sqliteUtils->getData($this->_dataType, $this->_dataClass);

if ( ! $allAliases) {
if (! $allAliases) {
return null;
}

Expand All @@ -134,9 +138,8 @@ public function listUsersAliases($userKey)
{
// If the $userKey is not an email address, it must be an id
$key = 'primaryEmail';
if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}
// ensure that user exists in db
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
Expand All @@ -160,12 +163,17 @@ public function listUsersAliases($userKey)
* @param string $userKey - The Email or immutable Id of the user
* @return null|Google_Service_Directory_Aliases
*/
public function fetchAliasesByUser($keyType, $userKey) {
public function fetchAliasesByUser($keyType, $userKey)
{
$sqliteUtils = new SqliteUtils($this->_dbFile);
$aliases = $sqliteUtils->getAllRecordsByDataKey($this->_dataType,
$this->_dataClass, $keyType, $userKey);
$aliases = $sqliteUtils->getAllRecordsByDataKey(
$this->_dataType,
$this->_dataClass,
$keyType,
$userKey
);

if ( ! $aliases) {
if (! $aliases) {
return null;
}

Expand Down
19 changes: 7 additions & 12 deletions SilMock/Google/Service/Directory/UsersResource.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace SilMock\Google\Service\Directory;

use SilMock\DataStore\Sqlite\SqliteUtils;

use Google_Service_Directory_User;

class UsersResource
Expand Down Expand Up @@ -87,7 +87,6 @@ protected function getAliasesForUser($userKey)
$key = 'primaryEmail';
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

$usersAliases = new UsersAliasesResource($this->_dbFile);
Expand Down Expand Up @@ -157,7 +156,7 @@ protected function getAllDbUsers()
public function insert($postBody)
{
$defaults = array(
'id' => intval(str_replace(array(' ', '.'), '', microtime())),
'id' => str_replace(array(' ', '.'), '', microtime()),
'suspended' => false,
'changePasswordAtNextLogin' => false,
'isAdmin' => false,
Expand Down Expand Up @@ -219,11 +218,10 @@ public function insert($postBody)
*/
public function update($userKey, $postBody)
{

$userEntry = $this->getDbUser($userKey);
if ($userEntry === null) {
throw new \Exception(
"Account doesn't exist: " . $userKey,
"Account doesn't exist: " . json_encode($userKey, true),
201407101130
);
}
Expand All @@ -232,7 +230,6 @@ public function update($userKey, $postBody)
* only keep the non-null properties of the $postBody user,
* except for suspensionReason.
*/

$dbUserProps = json_decode($userEntry['data'], true);
$newUserProps = get_object_vars($postBody);

Expand All @@ -257,7 +254,6 @@ public function update($userKey, $postBody)

// Save the user's aliases
if (isset($postBody->aliases) && $postBody->aliases) {

foreach ($postBody->aliases as $alias) {
$newAlias = new \Google_Service_Directory_Alias();
$newAlias->alias = $alias;
Expand All @@ -275,15 +271,14 @@ public function update($userKey, $postBody)
* Retrieves a user record from the database (users.delete)
*
* @param string $userKey - The Email or immutable Id of the user
* @return null|nested array for the matching database entry
* @return null|array -- nested array for the matching database entry
*/
private function getDbUser($userKey)
private function getDbUser(string $userKey)
{

$key = 'primaryEmail';
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

$sqliteUtils = new SqliteUtils($this->_dbFile);
Expand Down Expand Up @@ -345,7 +340,7 @@ public function listUsers($parameters = [])
$allResultsUsers[] = $newEntry;
$results->setUsers($allResultsUsers);
}
if (count($results->getUsers())>= $parameters['maxResults']) {
if (count($results->getUsers()) >= $parameters['maxResults']) {
break;
}
}
Expand All @@ -354,7 +349,7 @@ public function listUsers($parameters = [])

private function doesUserMatch($entry, $query = '')
{
if ($query==='') {
if ($query === '') {
return true;
}
$query = str_replace('*', '', $query);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SilMock\Google\Service\Directory;

use SilMock\DataStore\Sqlite\SqliteUtils;
Expand Down Expand Up @@ -29,7 +30,6 @@ public function invalidate($userKey)
$key = 'primaryEmail';
if (!filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

// ensure that user exists in db
Expand Down Expand Up @@ -69,7 +69,6 @@ public function listVerificationCodes($userKey)
$key = 'primaryEmail';
if (!filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
$userKey = intval($userKey);
}

$sqliteUtils = new SqliteUtils($this->_dbFile);
Expand Down
Loading

0 comments on commit fdb9089

Please sign in to comment.