Completed
Pull Request — 2.x (#80)
by
unknown
02:00
created

ImapAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Auth\Adapter;
10
11
use Aura\Auth\Exception;
12
use Aura\Auth\Phpfunc;
13
14
/**
15
 *
16
 * Authenticate against an IMAP, POP3, or NNTP server.
17
 *
18
 * @package Aura.Auth
19
 *
20
 */
21
class ImapAdapter extends AbstractAdapter
22
{
23
    /**
24
     *
25
     * An imap_open() mailbox string; e.g., "{mail.example.com:143/imap/secure}"
26
     * or "{mail.example.com:110/pop3/secure}".
27
     *
28
     * @var string
29
     *
30
     */
31
    protected $mailbox;
32
33
    /**
34
     *
35
     * Options for the imap_open() call.
36
     *
37
     * @var int
38
     *
39
     */
40
    protected $options = 0;
41
42
    /**
43
     *
44
     * Try to connect this many times.
45
     *
46
     * @var int
47
     *
48
     */
49
    protected $retries = 1;
50
51
    /**
52
     *
53
     * Params for the imap_open() call.
54
     *
55
     * @var array|null
56
     *
57
     */
58
    protected $params;
59
60
    /**
61
     *
62
     * An object to intercept PHP calls.
63
     *
64
     * @var Phpfunc
65
     *
66
     */
67
    protected $phpfunc;
68
69
    /**
70
     *
71
     * Constructor.
72
     *
73
     * @param Phpfunc $phpfunc An object to intercept PHP calls.
74
     *
75
     * @param string $mailbox The imap_open() mailbox string.
76
     *
77
     * @param int $options Options for the imap_open() call.
78
     *
79
     * @param int $retries Try connecting this many times.
80
     *
81
     * @param array $params Params for the imap_open() call.
82
     *
83
     */
84 5
    public function __construct(
85
        Phpfunc $phpfunc,
86
        $mailbox,
87
        $options = 0,
88
        $retries = 1,
89
        array $params = null
90
    ) {
91 5
        $this->phpfunc = $phpfunc;
92 5
        $this->mailbox = $mailbox;
93 5
        $this->options = $options;
94 5
        $this->retries = $retries;
95 5
        $this->params = $params;
96 5
    }
97
98
    /**
99
     *
100
     * Verifies a set of credentials.
101
     *
102
     * @param array $input An array of credential data, including any data to
103
     * bind to the query.
104
     *
105
     * @return array An array of login data.
106
     *
107
     * @throws Exception\ConnectionFailed when the IMAP connection fails.
108
     *
109
     */
110 2
    public function login(array $input)
111
    {
112 2
        $this->checkInput($input);
113 2
        $username = $input['username'];
114 2
        $password = $input['password'];
115
116 2
        $conn = $this->phpfunc->imap_open(
0 ignored issues
show
Documentation Bug introduced by
The method imap_open does not exist on object<Aura\Auth\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117 2
            $this->mailbox,
118 2
            $username,
119 2
            $password,
120 2
            $this->options,
121 2
            $this->retries,
122 2
            $this->params
123 2
        );
124
125 2
        if (! $conn) {
126 1
            throw new Exception\ConnectionFailed($this->mailbox);
127
        }
128
129 1
        $this->phpfunc->imap_close($conn);
0 ignored issues
show
Documentation Bug introduced by
The method imap_close does not exist on object<Aura\Auth\Phpfunc>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
130 1
        return array($username, array());
131
    }
132
}
133