Completed
Push — master ( 6a13c0...e84ecd )
by Hiraku
02:02
created

Factory::getAspectAuth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
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
    private static $instance = null;
17 2
    public static function getInstance()
18
    {
19 2
        return self::$instance ?: self::$instance = new self;
20
    }
21
22 1
    private function __construct()
23
    {
24
        // do nothing
25 1
    }
26
27
    /**
28
     * don't need Authorization
29
     * @var array {
30
     *  'origin.example.com' => x
31
     * }
32
     */
33
    private $connections = array();
0 ignored issues
show
Unused Code introduced by
The property $connections is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
    /**
36
     * need Authorization header
37
     * @var array {
38
     *  'origin.example.com' => x
39
     * }
40
     */
41
    private $authConnections = array();
0 ignored issues
show
Unused Code introduced by
The property $authConnections is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
    /**
44
     * get cached curl handler
45
     * @param string $origin
46
     * @param bool $auth
47
     * @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...
48
     */
49 1
    public static function getConnection($origin, $auth=false)
50
    {
51 1
        $instance = self::getInstance();
52 1
        if ($auth) {
53 1
            if (isset($instance->authConnections[$origin])) {
54 1
                return $instance->authConnections[$origin];
55
            }
56
57 1
            return $instance->authConnections[$origin] = curl_init();
58
        } else {
59 1
            if (isset($instance->connections[$origin])) {
60 1
                return $instance->connections[$origin];
61
            }
62
63 1
            return $instance->connections[$origin] = curl_init();
64
        }
65
    }
66
67
    /**
68
     * @return Aspects\JoinPoint
69
     */
70 1
    public static function getPreEvent(Aspects\HttpGetRequest $req)
71
    {
72 1
        $pre = new Aspects\JoinPoint('pre-download', $req);
73 1
        $pre->attach(static::getAspectAuth());
74 1
        $pre->attach(new Aspects\AspectRedirect);
75 1
        $pre->attach(new Aspects\AspectProxy);
76 1
        return $pre;
77
    }
78
79
    /**
80
     * @return Aspects\JoinPoint
81
     */
82 1
    public static function getPostEvent(Aspects\HttpGetRequest $req)
83
    {
84 1
        $post = new Aspects\JoinPoint('post-download', $req);
85 1
        $post->attach(static::getAspectAuth());
86 1
        return $post;
87
    }
88
89
    /**
90
     * @return Aspects\AspectAuth (same instance)
91
     */
92 2
    public static function getAspectAuth()
93
    {
94 2
        static $auth;
95 2
        return $auth ?: $auth = new Aspects\AspectAuth;
96
    }
97
}
98