Skip to content

Commit

Permalink
Merge pull request #38 from Dezzy666/config-file-override
Browse files Browse the repository at this point in the history
Config file override
  • Loading branch information
DivineOmega authored May 1, 2017
2 parents 83bb510 + 5f3026d commit 7016963
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@ Also, take a look at the following files for basic examples of how to retrieve c

* `test.php`
* `test_individual.php`

## Config file override

Each method has optional argument for config file override. It is useful when you want to use work with multiple Google accounts at the same time.

```php
$customConfig = (object) array(
'clientID' => '<clientId which you get according to setup above>',
'clientSecret' => '<clientSecret which you get according to setup above>',
'redirectUri' => '<your redirect uri>',
'developerKey' => '<developer key>',
'refreshToken' => '<refresh token specific for google account>'
);

$contacts = ContactFactory::getAll($customConfig);
```

You have to define all variables as the original config is completely ignored. To be more precise, it doesn't have to exist at all.
24 changes: 12 additions & 12 deletions factories/ContactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

abstract class ContactFactory
{
public static function getAll()
public static function getAll($customConfig = NULL)
{
$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);

$req = new \Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full?max-results=10000&updated-min=2007-03-16T00:00:00');

Expand Down Expand Up @@ -74,9 +74,9 @@ public static function getAll()
return $contactsArray;
}

public static function getBySelfURL($selfURL)
public static function getBySelfURL($selfURL, $customConfig = NULL)
{
$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);

$req = new \Google_Http_Request($selfURL);

Expand Down Expand Up @@ -137,9 +137,9 @@ public static function getBySelfURL($selfURL)
return new Contact($contactDetails);
}

public static function submitUpdates(Contact $updatedContact)
public static function submitUpdates(Contact $updatedContact, $customConfig = NULL)
{
$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);

$req = new \Google_Http_Request($updatedContact->selfURL);

Expand Down Expand Up @@ -215,7 +215,7 @@ public static function submitUpdates(Contact $updatedContact)
return new Contact($contactDetails);
}

public static function create($name, $phoneNumber, $emailAddress)
public static function create($name, $phoneNumber, $emailAddress, $customConfig = NULL)
{
$doc = new \DOMDocument();
$doc->formatOutput = true;
Expand All @@ -239,7 +239,7 @@ public static function create($name, $phoneNumber, $emailAddress)

$xmlToSend = $doc->saveXML();

$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);

$req = new \Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full');
$req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));
Expand Down Expand Up @@ -287,9 +287,9 @@ public static function create($name, $phoneNumber, $emailAddress)
return new Contact($contactDetails);
}

public static function delete(Contact $toDelete)
public static function delete(Contact $toDelete, $customConfig = NULL)
{
$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);

$req = new \Google_Http_Request($toDelete->editURL);
$req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));
Expand All @@ -298,9 +298,9 @@ public static function delete(Contact $toDelete)
$client->getAuth()->authenticatedRequest($req);
}

public static function getPhoto($photoURL)
public static function getPhoto($photoURL, $customConfig = NULL)
{
$client = GoogleHelper::getClient();
$client = GoogleHelper::getClient($customConfig);
$req = new \Google_Http_Request($photoURL);
$req->setRequestMethod('GET');
$val = $client->getAuth()->authenticatedRequest($req);
Expand Down
7 changes: 4 additions & 3 deletions helpers/GoogleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static function initConfig(
self::$_config->refreshToken = $refreshToken;
}

private static function loadConfig()
private static function loadConfig($customConfig = NULL)
{
self::$_config = $customConfig;
if (NULL === self::$_config) {
$configPath = __DIR__.'/../../../../.config.json';
if(!file_exists($configPath)) throw new \Exception('Not found config.json');
Expand All @@ -33,9 +34,9 @@ private static function loadConfig()
return self::$_config;
}

public static function getClient()
public static function getClient($customConfig = NULL)
{
$config = self::loadConfig();
$config = self::loadConfig($customConfig);

$client = new \Google_Client();

Expand Down

0 comments on commit 7016963

Please sign in to comment.