diff --git a/README.md b/README.md index 1af0bb9..f452caa 100644 --- a/README.md +++ b/README.md @@ -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' => '', + 'clientSecret' => '', + 'redirectUri' => '', + 'developerKey' => '', + 'refreshToken' => '' +); + +$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. diff --git a/factories/ContactFactory.php b/factories/ContactFactory.php index d7e47df..ecef94b 100644 --- a/factories/ContactFactory.php +++ b/factories/ContactFactory.php @@ -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'); @@ -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); @@ -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); @@ -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; @@ -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')); @@ -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')); @@ -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); diff --git a/helpers/GoogleHelper.php b/helpers/GoogleHelper.php index 123b12a..71256fa 100644 --- a/helpers/GoogleHelper.php +++ b/helpers/GoogleHelper.php @@ -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'); @@ -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();