Completed
Pull Request — master (#44)
by Hiraku
02:30
created

Factory::getPostEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
use Composer\IO;
10
use Composer\Config as CConfig;
11
12
/**
13
 * cache manager for curl handler
14
 *
15
 * Singleton
16
 */
17
final class Factory
18
{
19
    /**
20
     * not need Authorization
21
     * @var array {
22
     *  'origin.example.com' => x
23
     * }
24
     */
25
    private static $connections = array();
26
27
    /**
28
     * need Authorization header
29
     * @var array {
30
     *  'origin.example.com' => x
31
     * }
32
     */
33
    private static $authConnections = array();
34
35
    /**
36
     * get cached curl handler
37
     * @param string $origin
38
     * @param bool $auth
39
     * @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...
40
     */
41 6
    public static function getConnection($origin, $auth = false)
42
    {
43 6
        if ($auth) {
44 1
            if (isset(self::$authConnections[$origin])) {
45 1
                return self::$authConnections[$origin];
46
            }
47
48 1
            return self::$authConnections[$origin] = curl_init();
49
        } else {
50 6
            if (isset(self::$connections[$origin])) {
51 5
                return self::$connections[$origin];
52
            }
53
54 2
            return self::$connections[$origin] = curl_init();
55
        }
56
    }
57
58
    /**
59
     * @param string $origin domain text
60
     * @param string $url
61
     * @param IO\IOInterface $io
62
     * @param CConfig $config
63
     * @param array $pluginConfig
64
     * @return Aspects\HttpGetRequest
65
     */
66 5
    public static function getHttpGetRequest($origin, $url, IO\IOInterface $io, CConfig $config, array $pluginConfig)
67
    {
68 5
        if (substr($origin, -10) === 'github.com') {
69
            $origin = 'github.com';
70
            $requestClass = 'GitHub';
71 5
        } elseif (in_array($origin, $config->get('github-domains') ?: array())) {
72
            $requestClass = 'GitHub';
73 5
        } elseif (in_array($origin, $config->get('gitlab-domains') ?: array())) {
74
            $requestClass = 'GitLab';
75
        } else {
76 5
            $requestClass = 'HttpGet';
77
        }
78 5
        $requestClass = __NAMESPACE__ . '\Aspects\\' . $requestClass . 'Request';
79 5
        $request = new $requestClass($origin, $url, $io);
80 5
        $request->verbose = $pluginConfig['verbose'];
81 5
        if ($pluginConfig['insecure']) {
82
            $request->curlOpts[CURLOPT_SSL_VERIFYPEER] = false;
83
        }
84 5
        if (!empty($pluginConfig['capath'])) {
85
            $request->curlOpts[CURLOPT_CAPATH] = $pluginConfig['capath'];
86
        }
87 5
        if (!empty($pluginConfig['userAgent'])) {
88
            $request->curlOpts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
89
        }
90 5
        return $request;
91
    }
92
93
    /**
94
     * @return Aspects\JoinPoint
95
     */
96 6
    public static function getPreEvent(Aspects\HttpGetRequest $req)
97
    {
98 6
        $pre = new Aspects\JoinPoint('pre-download', $req);
99 6
        $pre->attach(static::getAspectAuth());
100 6
        $pre->attach(new Aspects\AspectRedirect);
101 6
        $pre->attach(new Aspects\AspectProxy);
102 6
        return $pre;
103
    }
104
105
    /**
106
     * @return Aspects\JoinPoint
107
     */
108 6
    public static function getPostEvent(Aspects\HttpGetRequest $req)
109
    {
110 6
        $post = new Aspects\JoinPoint('post-download', $req);
111 6
        $post->attach(static::getAspectAuth());
112 6
        return $post;
113
    }
114
115
    /**
116
     * @return Aspects\AspectAuth (same instance)
117
     */
118 7
    public static function getAspectAuth()
119
    {
120 7
        static $auth;
121 7
        return $auth ?: $auth = new Aspects\AspectAuth;
122
    }
123
}
124