Swift_Transport_Esmtp_Auth_XOAuth2Authenticator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 0
cts 11
cp 0
wmc 4
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthKeyword() 0 4 1
A authenticate() 0 13 2
A constructXOAuth2Params() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * Handles XOAUTH2 authentication.
13
 *
14
 * Example:
15
 * <code>
16
 * $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
17
 *   ->setAuthMode('XOAUTH2')
18
 *   ->setUsername('YOUR_EMAIL_ADDRESS')
19
 *   ->setPassword('YOUR_ACCESS_TOKEN');
20
 * </code>
21
 *
22
 * @author xu.li<[email protected]>
23
 *
24
 * @see        https://developers.google.com/google-apps/gmail/xoauth2_protocol
25
 */
26
class Swift_Transport_Esmtp_Auth_XOAuth2Authenticator implements Swift_Transport_Esmtp_Authenticator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    /**
29
     * Get the name of the AUTH mechanism this Authenticator handles.
30
     *
31
     * @return string
32
     */
33
    public function getAuthKeyword()
34
    {
35
        return 'XOAUTH2';
36
    }
37
38
    /**
39
     * Try to authenticate the user with $email and $token.
40
     *
41
     * @param Swift_Transport_SmtpAgent $agent
42
     * @param string                    $email
43
     * @param string                    $token
44
     *
45
     * @return bool
46
     */
47
    public function authenticate(Swift_Transport_SmtpAgent $agent, $email, $token)
48
    {
49
        try {
50
            $param = $this->constructXOAuth2Params($email, $token);
51
            $agent->executeCommand('AUTH XOAUTH2 ' . $param . "\r\n", array(235));
52
53
            return true;
54
        } catch (Swift_TransportException $e) {
55
            $agent->executeCommand("RSET\r\n", array(250));
56
57
            return false;
58
        }
59
    }
60
61
    /**
62
     * Construct the auth parameter.
63
     *
64
     * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism
65
     */
66
    protected function constructXOAuth2Params($email, $token)
67
    {
68
        return base64_encode("user=$email\1auth=Bearer $token\1\1");
69
    }
70
}
71