Completed
Push — master ( af1a9d...ee1d8c )
by Hiraku
02:13
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
/**
10
 * cache manager for curl handler
11
 *
12
 * Singleton
13
 */
14
final class Factory
15
{
16
    /**
17
     * not need Authorization
18
     * @var array {
19
     *  'origin.example.com' => x
20
     * }
21
     */
22
    private static $connections = array();
23
24
    /**
25
     * need Authorization header
26
     * @var array {
27
     *  'origin.example.com' => x
28
     * }
29
     */
30
    private static $authConnections = array();
31
32
    /**
33
     * get cached curl handler
34
     * @param string $origin
35
     * @param bool $auth
36
     * @return resource<curl>
0 ignored issues
show
Documentation introduced by
The doc-type resource<curl> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
37
     */
38 1
    public static function getConnection($origin, $auth=false)
39
    {
40 1
        if ($auth) {
41 1
            if (isset(self::$authConnections[$origin])) {
42 1
                return self::$authConnections[$origin];
43
            }
44
45 1
            return self::$authConnections[$origin] = curl_init();
46
        } else {
47 1
            if (isset(self::$connections[$origin])) {
48 1
                return self::$connections[$origin];
49
            }
50
51 1
            return self::$connections[$origin] = curl_init();
52
        }
53
    }
54
55
    /**
56
     * @return Aspects\JoinPoint
57
     */
58 1
    public static function getPreEvent(Aspects\HttpGetRequest $req)
59
    {
60 1
        $pre = new Aspects\JoinPoint('pre-download', $req);
61 1
        $pre->attach(static::getAspectAuth());
62 1
        $pre->attach(new Aspects\AspectRedirect);
63 1
        $pre->attach(new Aspects\AspectProxy);
64 1
        return $pre;
65
    }
66
67
    /**
68
     * @return Aspects\JoinPoint
69
     */
70 1
    public static function getPostEvent(Aspects\HttpGetRequest $req)
71
    {
72 1
        $post = new Aspects\JoinPoint('post-download', $req);
73 1
        $post->attach(static::getAspectAuth());
74 1
        return $post;
75
    }
76
77
    /**
78
     * @return Aspects\AspectAuth (same instance)
79
     */
80 2
    public static function getAspectAuth()
81
    {
82 2
        static $auth;
83 2
        return $auth ?: $auth = new Aspects\AspectAuth;
84
    }
85
}
86