22
33namespace SilMock \Google \Service \Directory ;
44
5+ use Exception ;
6+ use Google_Service_Directory_Alias as Alias ;
7+ use Google_Service_Directory_Aliases ;
58use SilMock \DataStore \Sqlite \SqliteUtils ;
9+ use SilMock \Google \Service \DbClass ;
10+ use SilMock \Google \Service \Directory ;
611
7- class UsersAliasesResource
12+ class UsersAliasesResource extends DbClass
813{
9-
10- private $ _dbFile ; // string for the path (with file name) for the Sqlite database
11- private $ _dataType = 'directory ' ; // string to put in the 'type' field in the database
12- private $ _dataClass = 'users_alias ' ; // string to put in the 'class' field in the database
13-
14-
1514 public function __construct ($ dbFile = null )
1615 {
17- $ this -> _dbFile = $ dbFile ;
16+ parent :: __construct ( $ dbFile , ' directory ' , ' users_alias ' ) ;
1817 }
1918
2019 /**
@@ -23,7 +22,7 @@ public function __construct($dbFile = null)
2322 * @param string $userKey The email or immutable Id of the user
2423 * @param string $alias The alias to be removed
2524 * @return true|null depending on if an alias was deleted
26- * @throws \ Exception with code 201407101645
25+ * @throws Exception with code 201407101645
2726 */
2827 public function delete ($ userKey , $ alias )
2928 {
@@ -34,18 +33,18 @@ public function delete($userKey, $alias)
3433 }
3534
3635 // ensure that user exists in db
37- $ dir = new \ SilMock \ Google \ Service \ Directory ('anything ' , $ this ->_dbFile );
36+ $ dir = new Directory ('anything ' , $ this ->dbFile );
3837 $ matchingUsers = $ dir ->users ->get ($ userKey );
3938
4039 if ($ matchingUsers === null ) {
41- throw new \ Exception ("Account doesn't exist: " . $ userKey , 201407101645 );
40+ throw new Exception ("Account doesn't exist: " . $ userKey , 201407101645 );
4241 }
4342
4443 // Get all the aliases for that user
45- $ sqliteUtils = new SqliteUtils ($ this ->_dbFile );
44+ $ sqliteUtils = new SqliteUtils ($ this ->dbFile );
4645 $ aliases = $ sqliteUtils ->getAllRecordsByDataKey (
47- $ this ->_dataType ,
48- $ this ->_dataClass ,
46+ $ this ->dataType ,
47+ $ this ->dataClass ,
4948 $ key ,
5049 $ userKey
5150 );
@@ -73,7 +72,7 @@ public function delete($userKey, $alias)
7372 * @param string $userKey The email or immutable Id of the user
7473 * @param Alias $postBody The array/object with the data for that alias
7574 * @return Alias - a real Google_Service_Directory_Alias instance
76- * @throws \ Exception with code 201407110830 if a matching user is not found.
75+ * @throws Exception with code 201407110830 if a matching user is not found.
7776 */
7877 public function insert ($ userKey , $ postBody )
7978 {
@@ -84,11 +83,11 @@ public function insert($userKey, $postBody)
8483 }
8584
8685 // ensure that user exists in db
87- $ dir = new \ SilMock \ Google \ Service \ Directory ('anything ' , $ this ->_dbFile );
86+ $ dir = new Directory ('anything ' , $ this ->dbFile );
8887 $ matchingUsers = $ dir ->users ->get ($ userKey );
8988
9089 if ($ matchingUsers === null ) {
91- throw new \ Exception ("Account doesn't exist: " . $ userKey , 201407110830 );
90+ throw new Exception ("Account doesn't exist: " . $ userKey , 201407110830 );
9291 }
9392
9493 if ($ postBody ->$ key === null ) {
@@ -107,19 +106,19 @@ public function insert($userKey, $postBody)
107106 public function insertAssumingUserExists ($ postBody )
108107 {
109108 $ entryData = json_encode (get_object_vars ($ postBody ));
110- $ sqliteUtils = new SqliteUtils ($ this ->_dbFile );
109+ $ sqliteUtils = new SqliteUtils ($ this ->dbFile );
111110 $ sqliteUtils ->recordData (
112- $ this ->_dataType ,
113- $ this ->_dataClass ,
111+ $ this ->dataType ,
112+ $ this ->dataClass ,
114113 $ entryData
115114 );
116- $ allAliases = $ sqliteUtils ->getData ($ this ->_dataType , $ this ->_dataClass );
115+ $ allAliases = $ sqliteUtils ->getData ($ this ->dataType , $ this ->dataClass );
117116
118117 if (! $ allAliases ) {
119118 return null ;
120119 }
121120
122- $ newAlias = new \ Google_Service_Directory_Alias ();
121+ $ newAlias = new Alias ();
123122 ObjectUtils::initialize ($ newAlias , $ postBody );
124123
125124 return $ newAlias ;
@@ -131,22 +130,22 @@ public function insertAssumingUserExists($postBody)
131130 * instances for that user
132131 *
133132 * @param string $userKey - The Email or immutable Id of the user
134- * @return a real Google_Service_Directory_Aliases instance
135- * @throws \ Exception with code 201407101420 if a matching user is not found.
133+ * @return Google_Service_Directory_Aliases|null
134+ * @throws Exception with code 201407101420 if a matching user is not found.
136135 */
137- public function listUsersAliases ($ userKey )
136+ public function listUsersAliases ($ userKey ): ? Google_Service_Directory_Aliases
138137 {
139138 // If the $userKey is not an email address, it must be an id
140139 $ key = 'primaryEmail ' ;
141140 if (! filter_var ($ userKey , FILTER_VALIDATE_EMAIL )) {
142141 $ key = 'id ' ;
143142 }
144143 // ensure that user exists in db
145- $ dir = new \ SilMock \ Google \ Service \ Directory ('anything ' , $ this ->_dbFile );
144+ $ dir = new Directory ('anything ' , $ this ->dbFile );
146145 $ matchingUsers = $ dir ->users ->get ($ userKey );
147146
148147 if ($ matchingUsers === null ) {
149- throw new \ Exception ("Account doesn't exist: " . $ userKey , 201407101420 );
148+ throw new Exception ("Account doesn't exist: " . $ userKey , 201407101420 );
150149 }
151150
152151 $ foundAliases = $ this ->fetchAliasesByUser ($ key , $ userKey );
@@ -161,14 +160,14 @@ public function listUsersAliases($userKey)
161160 *
162161 * @param string $keyType - "Email" or "Id"
163162 * @param string $userKey - The Email or immutable Id of the user
164- * @return null| Google_Service_Directory_Aliases
163+ * @return Google_Service_Directory_Aliases|null
165164 */
166- public function fetchAliasesByUser ($ keyType , $ userKey )
165+ public function fetchAliasesByUser ($ keyType , $ userKey ): ? Google_Service_Directory_Aliases
167166 {
168- $ sqliteUtils = new SqliteUtils ($ this ->_dbFile );
167+ $ sqliteUtils = new SqliteUtils ($ this ->dbFile );
169168 $ aliases = $ sqliteUtils ->getAllRecordsByDataKey (
170- $ this ->_dataType ,
171- $ this ->_dataClass ,
169+ $ this ->dataType ,
170+ $ this ->dataClass ,
172171 $ keyType ,
173172 $ userKey
174173 );
@@ -180,7 +179,7 @@ public function fetchAliasesByUser($keyType, $userKey)
180179 $ foundAliases = array ();
181180
182181 foreach ($ aliases as $ nextAlias ) {
183- $ newAlias = new \ Google_Service_Directory_Alias ();
182+ $ newAlias = new Alias ();
184183 ObjectUtils::initialize ($ newAlias , json_decode ($ nextAlias ['data ' ], true ));
185184
186185 $ foundAliases [] = $ newAlias ;
0 commit comments