Skip to content

Commit

Permalink
Merge pull request #51 from silinternational/develop
Browse files Browse the repository at this point in the history
Add delete() to Google mock token object
  • Loading branch information
mtompset authored Mar 21, 2022
2 parents b71eccf + 59db342 commit 2feb7d3
Show file tree
Hide file tree
Showing 26 changed files with 576 additions and 369 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ COPY ./ /data

RUN cd /data && ./composer-install.sh
RUN mv /data/composer.phar /usr/bin/composer
RUN /usr/bin/composer install
9 changes: 5 additions & 4 deletions SilMock/DataStore/Sqlite/SqliteUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SilMock\DataStore\Sqlite;

use PDO;
Expand Down Expand Up @@ -100,8 +101,8 @@ public function getRecordByDataKey($dataType, $dataClass, $dataKey, $dataValue)

foreach ($allOfClass as $nextEntry) {
$nextData = json_decode($nextEntry['data'], true);
if (isset($nextData[$dataKey]) &&
$nextData[$dataKey] === $dataValue) {
$nextDataValue = $nextData[$dataKey] ?? null;
if ($nextDataValue === $dataValue) {
return $nextEntry;
}
}
Expand All @@ -126,8 +127,8 @@ public function getAllRecordsByDataKey($dataType, $dataClass, $dataKey, $dataVal

foreach ($allOfClass as $nextEntry) {
$nextData = json_decode($nextEntry['data'], true);
if (isset($nextData[$dataKey]) &&
$nextData[$dataKey] === $dataValue) {
$nextDataValue = $nextData[$dataKey] ?? null;
if ($nextDataValue === $dataValue) {
$foundEntries[] = $nextEntry;
}
}
Expand Down
67 changes: 67 additions & 0 deletions SilMock/Google/Service/DbClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace SilMock\Google\Service;

use Exception;
use SilMock\DataStore\Sqlite\SqliteUtils;
use Sil\Psr3Adapters\Psr3EchoLogger;

class DbClass
{
/** @var string|null - The path (with file name) to the SQLite database. */
protected ?string $dbFile;

/** @var string - The 'type' field to use in the database. */
protected string $dataType;

/** @var string - The 'class' field to use in the database */
protected string $dataClass;

protected Psr3EchoLogger $logger;

public function __construct(?string $dbFile, string $dataType, string $dataClass)
{
$this->dbFile = $dbFile;
$this->dataType = $dataType;
$this->dataClass = $dataClass;
$this->logger = new Psr3EchoLogger();
if (empty($dbFile)) {
$exception = new Exception();
$exceptions = explode("\n", $exception->getTraceAsString());
$previousLocationMessage = $exceptions[1];
$this->logger->warning(
sprintf(
"Empty dbFile provided:\n%s",
$previousLocationMessage
)
);
}
}

protected function getSqliteUtils(): SqliteUtils
{
return new SqliteUtils($this->dbFile);
}

protected function getRecords(): array
{
$sqliteUtils = $this->getSqliteUtils();
return $sqliteUtils->getData($this->dataType, $this->dataClass);
}

protected function deleteRecordById(int $recordId)
{
$sqliteUtils = $this->getSqliteUtils();
$sqliteUtils->deleteRecordById($recordId);
}

protected function addRecord(string $data)
{
$sqliteUtils = $this->getSqliteUtils();
$sqliteUtils->recordData(
$this->dataType,
$this->dataClass,
$data
);
}
}
7 changes: 4 additions & 3 deletions SilMock/Google/Service/Directory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace SilMock\Google\Service;

use SilMock\Google\Service\Directory\Asps;
Expand All @@ -19,10 +20,10 @@ class Directory
* Sets the users and users_aliases properties to be instances of
* the corresponding mock classes.
*
* @param $client mixed - Ignored (normally it would be a Google_Client)
* @param $dbFile string (optional) - The path and file name of the database file
* @param mixed $client -- Ignored (normally it would be a Google_Client)
* @param string|null $dbFile -- (optional) The path and file name of the database file
*/
public function __construct($client, $dbFile=null)
public function __construct($client, $dbFile = null)
{
$this->asps = new Asps($dbFile);
$this->tokens = new Tokens($dbFile);
Expand Down
14 changes: 3 additions & 11 deletions SilMock/Google/Service/Directory/Asps.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
namespace SilMock\Google\Service\Directory;

use Google_Service_Directory_Asps;
use SilMock\Google\Service\DbClass;

class Asps
class Asps extends DbClass
{
/** @var string - The path (with file name) to the SQLite database. */
private $dbFile;

/** @var string - The 'type' field to use in the database. */
private $dataType = 'directory';

/** @var string - The 'class' field to use in the database */
private $dataClass = 'asps';

public function __construct($dbFile = null)
{
$this->dbFile = $dbFile;
parent::__construct($dbFile, 'directory', 'asps');
}

public function listAsps($userKey, $optParams = array())
Expand Down
66 changes: 51 additions & 15 deletions SilMock/Google/Service/Directory/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,64 @@

namespace SilMock\Google\Service\Directory;

use Google_Service_Directory_Token;
use Google_Service_Directory_Tokens;
use Google_Service_Exception;
use SilMock\Google\Service\DbClass;

class Tokens
class Tokens extends DbClass
{
/** @var string - The path (with file name) to the SQLite database. */
private $dbFile;

/** @var string - The 'type' field to use in the database. */
private $dataType = 'directory';

/** @var string - The 'class' field to use in the database */
private $dataClass = 'tokens';

public function __construct($dbFile = null)
{
$this->dbFile = $dbFile;
parent::__construct($dbFile, 'directory', 'tokens');
}

public function listTokens($userKey, $optParams = array())
public function listTokens($userKey, $optParams = []): Google_Service_Directory_Tokens
{
return new Google_Service_Directory_Tokens([ 'items' => [] ]);
}

/**
* @throws Google_Service_Exception
*/
public function delete($userKey, $clientId)
{
$this->assertIsValidUserKey($userKey);

foreach ($this->listTokensFor($userKey) as $recordId => $token) {
/** @var Google_Service_Directory_Token $token */
if ($token->clientId === $clientId) {
$this->removeToken($recordId);
return;
}
}
}

protected function assertIsValidUserKey(string $userId)
{
if (! $this->isValidEmailAddress($userId)) {
throw new Google_Service_Exception('Invalid userId: ' . $userId, 400);
}
}

/**
* Determine whether the given string is a valid email address.
*
* @param string $email The email address to check.
* @return bool Whether the string is a valid email address.
*/
protected function isValidEmailAddress(string $email): bool
{
return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false);
}

protected function listTokensFor(string $userId): array
{
return [];
}

protected function removeToken($recordId)
{
return new Google_Service_Directory_Tokens(array(
'items' => array(),
));
$this->deleteRecordById($recordId);
}
}
65 changes: 32 additions & 33 deletions SilMock/Google/Service/Directory/UsersAliasesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace SilMock\Google\Service\Directory;

use Exception;
use Google_Service_Directory_Alias as Alias;
use Google_Service_Directory_Aliases;
use SilMock\DataStore\Sqlite\SqliteUtils;
use SilMock\Google\Service\DbClass;
use SilMock\Google\Service\Directory;

class UsersAliasesResource
class UsersAliasesResource extends DbClass
{

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)
{
$this->_dbFile = $dbFile;
parent::__construct($dbFile, 'directory', 'users_alias');
}

/**
Expand All @@ -23,7 +22,7 @@ public function __construct($dbFile = null)
* @param string $userKey The email or immutable Id of the user
* @param string $alias The alias to be removed
* @return true|null depending on if an alias was deleted
* @throws \Exception with code 201407101645
* @throws Exception with code 201407101645
*/
public function delete($userKey, $alias)
{
Expand All @@ -34,18 +33,18 @@ public function delete($userKey, $alias)
}

// ensure that user exists in db
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
$dir = new Directory('anything', $this->dbFile);
$matchingUsers = $dir->users->get($userKey);

if ($matchingUsers === null) {
throw new \Exception("Account doesn't exist: " . $userKey, 201407101645);
throw new Exception("Account doesn't exist: " . $userKey, 201407101645);
}

// Get all the aliases for that user
$sqliteUtils = new SqliteUtils($this->_dbFile);
$sqliteUtils = new SqliteUtils($this->dbFile);
$aliases = $sqliteUtils->getAllRecordsByDataKey(
$this->_dataType,
$this->_dataClass,
$this->dataType,
$this->dataClass,
$key,
$userKey
);
Expand Down Expand Up @@ -73,7 +72,7 @@ public function delete($userKey, $alias)
* @param string $userKey The email or immutable Id of the user
* @param Alias $postBody The array/object with the data for that alias
* @return Alias - a real Google_Service_Directory_Alias instance
* @throws \Exception with code 201407110830 if a matching user is not found.
* @throws Exception with code 201407110830 if a matching user is not found.
*/
public function insert($userKey, $postBody)
{
Expand All @@ -84,11 +83,11 @@ public function insert($userKey, $postBody)
}

// ensure that user exists in db
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
$dir = new Directory('anything', $this->dbFile);
$matchingUsers = $dir->users->get($userKey);

if ($matchingUsers === null) {
throw new \Exception("Account doesn't exist: " . $userKey, 201407110830);
throw new Exception("Account doesn't exist: " . $userKey, 201407110830);
}

if ($postBody->$key === null) {
Expand All @@ -107,19 +106,19 @@ public function insert($userKey, $postBody)
public function insertAssumingUserExists($postBody)
{
$entryData = json_encode(get_object_vars($postBody));
$sqliteUtils = new SqliteUtils($this->_dbFile);
$sqliteUtils = new SqliteUtils($this->dbFile);
$sqliteUtils->recordData(
$this->_dataType,
$this->_dataClass,
$this->dataType,
$this->dataClass,
$entryData
);
$allAliases = $sqliteUtils->getData($this->_dataType, $this->_dataClass);
$allAliases = $sqliteUtils->getData($this->dataType, $this->dataClass);

if (! $allAliases) {
return null;
}

$newAlias = new \Google_Service_Directory_Alias();
$newAlias = new Alias();
ObjectUtils::initialize($newAlias, $postBody);

return $newAlias;
Expand All @@ -131,22 +130,22 @@ public function insertAssumingUserExists($postBody)
* instances for that user
*
* @param string $userKey - The Email or immutable Id of the user
* @return a real Google_Service_Directory_Aliases instance
* @throws \Exception with code 201407101420 if a matching user is not found.
* @return Google_Service_Directory_Aliases|null
* @throws Exception with code 201407101420 if a matching user is not found.
*/
public function listUsersAliases($userKey)
public function listUsersAliases($userKey): ?Google_Service_Directory_Aliases
{
// If the $userKey is not an email address, it must be an id
$key = 'primaryEmail';
if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) {
$key = 'id';
}
// ensure that user exists in db
$dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile);
$dir = new Directory('anything', $this->dbFile);
$matchingUsers = $dir->users->get($userKey);

if ($matchingUsers === null) {
throw new \Exception("Account doesn't exist: " . $userKey, 201407101420);
throw new Exception("Account doesn't exist: " . $userKey, 201407101420);
}

$foundAliases = $this->fetchAliasesByUser($key, $userKey);
Expand All @@ -161,14 +160,14 @@ public function listUsersAliases($userKey)
*
* @param string $keyType - "Email" or "Id"
* @param string $userKey - The Email or immutable Id of the user
* @return null|Google_Service_Directory_Aliases
* @return Google_Service_Directory_Aliases|null
*/
public function fetchAliasesByUser($keyType, $userKey)
public function fetchAliasesByUser($keyType, $userKey): ?Google_Service_Directory_Aliases
{
$sqliteUtils = new SqliteUtils($this->_dbFile);
$sqliteUtils = new SqliteUtils($this->dbFile);
$aliases = $sqliteUtils->getAllRecordsByDataKey(
$this->_dataType,
$this->_dataClass,
$this->dataType,
$this->dataClass,
$keyType,
$userKey
);
Expand All @@ -180,7 +179,7 @@ public function fetchAliasesByUser($keyType, $userKey)
$foundAliases = array();

foreach ($aliases as $nextAlias) {
$newAlias = new \Google_Service_Directory_Alias();
$newAlias = new Alias();
ObjectUtils::initialize($newAlias, json_decode($nextAlias['data'], true));

$foundAliases[] = $newAlias;
Expand Down
Loading

0 comments on commit 2feb7d3

Please sign in to comment.