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

AspectAuth::before()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 7.551
cc 7
eloc 12
nc 4
nop 1
crap 7
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 10
    public function update(SplSubject $ev)
19
    {
20 10
        $name = (string)$ev;
21 10
        if ('post-download' === $name) {
22 8
            $this->after($ev->refResponse());
23 7
        }
24 9
    }
25
26 8
    private function after(HttpGetResponse $res)
27
    {
28 8
        if (CURLE_OK !== $res->errno) {
29 1
            throw new Downloader\TransportException("$res->error:$res->errno");
30
        }
31
32 7
        if (in_array($res->info['http_code'], array(401, 403, 404))) {
33 4
            $res->setNeedAuth();
34 4
            return;
35
        }
36 4
    }
37
}
38