Completed
Pull Request — master (#68)
by Hiraku
03:10
created

AspectAuth::before()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
crap 3
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo\Aspects;
8
9
use SplObserver;
10
use SplSubject;
11
use Composer\Downloader;
12
13
/**
14
 * Authentication aspects.
15
 */
16
class AspectAuth implements SplObserver
17
{
18 13
    public function update(SplSubject $ev)
19
    {
20 13
        $name = (string)$ev;
21 13
        if ('pre-download' === $name) {
22 11
            return $this->before($ev->refRequest());
23
        }
24 8
        if ('post-download' === $name) {
25 8
            $this->after($ev->refResponse());
26 7
        }
27 7
    }
28
29 11
    private function before(HttpGetRequest $req)
30
    {
31 11
        if (!$req->username || !$req->password) {
32 9
            $req->username = $req->password = null;
33 9
        }
34
    }
35
36 3
    private function after(HttpGetResponse $res)
37 1
    {
38
        if (CURLE_OK !== $res->errno) {
39 1
            throw new Downloader\TransportException("$res->error:$res->errno");
40 1
        }
41
42
        if (in_array($res->info['http_code'], array(401, 403, 404))) {
43 2
            $res->setNeedAuth();
44 1
            return;
45
        }
46 1
    }
47
}
48