Completed
Pull Request — 2.x (#89)
by
unknown
01:58
created

AuthFactory::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
nc 4
cc 3
eloc 10
nop 3
crap 3
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;
10
11
use Aura\Auth\Adapter;
12
use Aura\Auth\Service;
13
use Aura\Auth\Session;
14
use Aura\Auth\Session\SessionInterface;
15
use Aura\Auth\Session\SegmentInterface;
16
use Aura\Auth\Verifier;
17
use Aura\Auth\Adapter\AdapterInterface;
18
use PDO;
19
20
/**
21
 *
22
 * Factory for Auth package objects.
23
 *
24
 * @package Aura.Auth
25
 *
26
 */
27
28
class AuthFactory
29
{
30
    /**
31
     *
32
     * A session manager.
33
     *
34
     * @var SessionInterface
35
     *
36
     */
37
    protected $session;
38
39
    /**
40
     *
41
     * A session segment.
42
     *
43
     * @var SegmentInterface
44
     *
45
     */
46
    protected $segment;
47
48
    /**
49
     *
50
     * Constructor.
51
     *
52
     * @param array $cookie A copy of $_COOKIES.
53
     *
54
     * @param SessionInterface $session A session manager.
55
     *
56
     * @param SegmentInterface $segment A session segment.
57
     *
58
     */
59 10
    public function __construct(
60
        array $cookie,
61
        SessionInterface $session = null,
62
        SegmentInterface $segment = null
63
    ) {
64 10
        $this->session = $session;
65 10
        if (! $this->session) {
66 10
            $this->session = new Session\Session($cookie);
67 10
        }
68
69 10
        $this->segment = $segment;
70 10
        if (! $this->segment) {
71 10
            $this->segment = new Session\Segment;
72 10
        }
73 10
    }
74
75
    /**
76
     *
77
     * Returns a new authentication tracker.
78
     *
79
     * @return Auth
80
     *
81
     */
82 2
    public function newInstance()
83
    {
84 2
        return new Auth($this->segment);
85
    }
86
87
    /**
88
     *
89
     * Returns a new login service instance.
90
     *
91
     * @param AdapterInterface $adapter The adapter to use with the service.
92
     *
93
     * @return Service\LoginService
94
     *
95
     */
96 1
    public function newLoginService(AdapterInterface $adapter = null)
97
    {
98 1
        return new Service\LoginService(
99 1
            $this->fixAdapter($adapter),
100 1
            $this->session
101 1
        );
102
    }
103
104
    /**
105
     *
106
     * Returns a new logout service instance.
107
     *
108
     * @param AdapterInterface $adapter The adapter to use with the service.
109
     *
110
     * @return Service\LogoutService
111
     *
112
     */
113 1
    public function newLogoutService(AdapterInterface $adapter = null)
114
    {
115 1
        return new Service\LogoutService(
116 1
            $this->fixAdapter($adapter),
117 1
            $this->session
118 1
        );
119
    }
120
121
    /**
122
     *
123
     * Returns a new "resume session" service.
124
     *
125
     * @param AdapterInterface $adapter The adapter to use with the service, and
126
     * with the underlying logout service.
127
     *
128
     * @param int $idle_ttl The session idle time in seconds.
129
     *
130
     * @param int $expire_ttl The session expire time in seconds.
131
     *
132
     * @return Service\ResumeService
133
     *
134
     */
135 1
    public function newResumeService(
136
        AdapterInterface $adapter = null,
137
        $idle_ttl = 3600,               // 1 hour
138
        $expire_ttl = 86400             // 24 hours
139
    ) {
140
141 1
        $adapter = $this->fixAdapter($adapter);
142
143 1
        $timer = new Session\Timer(
144 1
            ini_get('session.gc_maxlifetime'),
145 1
            ini_get('session.cookie_lifetime'),
146 1
            $idle_ttl,
147
            $expire_ttl
148 1
        );
149
150 1
        $logout_service = new Service\LogoutService(
151 1
            $adapter,
152 1
            $this->session
153 1
        );
154
155 1
        return new Service\ResumeService(
156 1
            $adapter,
157 1
            $this->session,
158 1
            $timer,
159
            $logout_service
160 1
        );
161
    }
162
163
    /**
164
     *
165
     * Make sure we have an Adapter instance, even if only a NullAdapter.
166
     *
167
     * @param Adapterinterface $adapter Check to make sure this is an Adapter
168
     * instance.
169
     *
170
     * @return AdapterInterface
171
     *
172
     */
173 3
    protected function fixAdapter(AdapterInterface $adapter = null)
174
    {
175 3
        if ($adapter === null) {
176 3
            $adapter = new Adapter\NullAdapter;
177 3
        }
178 3
        return $adapter;
179
    }
180
181
    /**
182
     *
183
     * Returns a new PDO adapter.
184
     *
185
     * @param PDO $pdo A PDO connection.
186
     *
187
     * @param mixed $verifier_spec Specification to pick a verifier: if an
188
     * object, assume a VerifierInterface; otherwise, assume a PASSWORD_*
189
     * constant for a PasswordVerifier.
190
     *
191
     * @param array $cols Select these columns.
192
     *
193
     * @param string $from Select from this table (and joins).
194
     *
195
     * @param string $where WHERE conditions for the select.
196
     *
197
     * @return Adapter\PdoAdapter
198
     *
199
     */
200 2
    public function newPdoAdapter(
201
        PDO $pdo,
202
        $verifier_spec,
203
        array $cols,
204
        $from,
205
        $where = null
206
    ) {
207 2
        if (is_object($verifier_spec)) {
208 1
            $verifier = $verifier_spec;
209 1
        } else {
210 1
            $verifier = new Verifier\PasswordVerifier($verifier_spec);
211
        }
212
213 2
        return new Adapter\PdoAdapter(
214 2
            $pdo,
215 2
            $verifier,
216 2
            $cols,
217 2
            $from,
218
            $where
219 2
        );
220
    }
221
222
    /**
223
     *
224
     * Returns a new HtpasswdAdapter.
225
     *
226
     * @param string $file Path to the htpasswd file.
227
     *
228
     * @return Adapter\HtpasswdAdapter
229
     *
230
     */
231 1
    public function newHtpasswdAdapter($file)
232
    {
233 1
        $verifier = new Verifier\HtpasswdVerifier;
234 1
        return new Adapter\HtpasswdAdapter(
235 1
            $file,
236
            $verifier
237 1
        );
238
    }
239
240
    /**
241
     *
242
     * Returns a new ImapAdapter.
243
     *
244
     * @param string $mailbox An imap_open() mailbox string.
245
     *
246
     * @param int $options Options for the imap_open() call.
247
     *
248
     * @param int $retries Try to connect this many times.
249
     *
250
     * @param array $params Set these params after opening the connection.
251
     *
252
     * @return Adapter\ImapAdapter
253
     *
254
     */
255 1
    public function newImapAdapter(
256
        $mailbox,
257
        $options = 0,
258
        $retries = 1,
259
        array $params = null
260
    ) {
261 1
        return new Adapter\ImapAdapter(
262 1
            new Phpfunc,
263 1
            $mailbox,
264 1
            $options,
265 1
            $retries,
266
            $params
267 1
        );
268
    }
269
270
    /**
271
     *
272
     * Returns a new LdapAdapter.
273
     *
274
     * @param string $server An LDAP server string.
275
     *
276
     * @param string $dnformat A distinguished name format string for looking up
277
     * the username.
278
     *
279
     * @param array $options Use these connection options.
280
     *
281
     * @return Adapter\LdapAdapter
282
     *
283
     */
284 1
    public function newLdapAdapter(
285
        $server,
286
        $dnformat,
287
        array $options = array()
288
    ) {
289 1
        return new Adapter\LdapAdapter(
290 1
            new Phpfunc,
291 1
            $server,
292 1
            $dnformat,
293
            $options
294 1
        );
295
    }
296
}
297