22
33namespace SilMock \Google \Service \Gmail \Resource ;
44
5+ use Google_Service_Exception ;
56use Google_Service_Gmail_ListForwardingAddressesResponse ;
6- use Webmozart \ Assert \ Assert ;
7+ use SilMock \ DataStore \ Sqlite \ SqliteUtils ;
78
89class UsersSettingsForwardingAddresses
910{
@@ -20,11 +21,68 @@ public function __construct($dbFile = null)
2021 {
2122 $ this ->dbFile = $ dbFile ;
2223 }
23-
24+
2425 public function listUsersSettingsForwardingAddresses ($ userId , $ optParams = array ())
2526 {
2627 return new Google_Service_Gmail_ListForwardingAddressesResponse (array (
2728 'forwardingAddresses ' => array (),
2829 ));
2930 }
31+
32+ protected function getSqliteUtils (): SqliteUtils
33+ {
34+ return new SqliteUtils ($ this ->dbFile );
35+ }
36+
37+ protected function assertIsValidUserId (string $ userId )
38+ {
39+ if (! $ this ->isValidEmailAddress ($ userId )) {
40+ throw new Google_Service_Exception ('Invalid userId: ' . $ userId , 400 );
41+ }
42+ }
43+
44+ protected function assertIsValidDelegateEmail ($ delegateEmail )
45+ {
46+ if (! $ this ->isValidEmailAddress ($ delegateEmail )) {
47+ throw new Google_Service_Exception ('Invalid delegate: ' . $ delegateEmail , 400 );
48+ }
49+ }
50+
51+ /**
52+ * Determine whether the given string is a valid email address.
53+ *
54+ * @param string $email The email address to check.
55+ * @return bool Whether the string is a valid email address.
56+ */
57+ protected function isValidEmailAddress (string $ email ): bool
58+ {
59+ return (filter_var ($ email , FILTER_VALIDATE_EMAIL ) !== false );
60+ }
61+
62+ /**
63+ * @throws Google_Service_Exception
64+ */
65+ public function delete ($ userId , $ forwardedAddress , $ optParams = array ())
66+ {
67+ $ this ->assertIsValidUserId ($ userId );
68+ $ this ->assertIsValidDelegateEmail ($ forwardedAddress );
69+
70+ foreach ($ this ->listForwardingAddressesFor ($ userId ) as $ recordId => $ forwardingAddress ) {
71+ if ($ forwardingAddress ->getForwardingEmail () === $ forwardedAddress ) {
72+ $ this ->removeForwardingAddress ($ recordId );
73+ return ;
74+ }
75+ }
76+ }
77+
78+ protected function listForwardingAddressesFor (string $ userId ): array
79+ {
80+ return [];
81+ }
82+
83+ protected function removeForwardingAddress ($ recordId )
84+ {
85+ $ sqliteUtils = $ this ->getSqliteUtils ();
86+ $ sqliteUtils ->deleteRecordById ($ recordId );
87+ }
3088}
0 commit comments