Completed
Push — develop ( 2a6d35...6e507e )
by Bastian
12s
created

MongoCredentialsProvider::fromInput()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 8.5906
cc 6
eloc 14
nc 6
nop 1
crap 6
1
<?php
2
/**
3
 * small helper to read our VCAP_SERVICES in cf
4
 */
5
6
namespace Graviton\ImportExport\Util;
7
8
use Symfony\Component\Console\Input\InputInterface;
9
use Flow\JSONPath\JSONPath;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class MongoCredentialsProvider
17
{
18
19
    /**
20
     * gets connection params from ENV if available
21
     *
22
     * @return array connection params
23
     */
24
    public static function getConnection()
25
    {
26
        // defaults
27
        $ret = ['uri' => null, 'db' => 'db'];
28
29
        if (isset($_ENV['VCAP_SERVICES'])) {
30
            $path = new JSONPath(json_decode($_ENV['VCAP_SERVICES'], true));
31
            $ret['uri'] = $path->find('$..mongodb[0].*.uri')->first();
32
            $ret['db'] = $path->find('$..mongodb[0].*.database')->first();
33
        }
34
35
        return $ret;
36
    }
37
38
    /**
39
     * gets connection params from input
40
     *
41
     * @param InputInterface $input input from user
42
     *
43
     * @return array
44
     */
45 4
    public static function fromInput(InputInterface $input)
46
    {
47 4
        $uri = $input->getOption('mongodb');
48 4
        $uriParts = parse_url($uri);
49
50 4
        $db = 'db';
51 4
        if (array_key_exists('path', $uriParts) && $uriParts['path'] !== '/') {
52 2
            $db = substr($uriParts['path'], 1);
53 2
        }
54
55 4
        if (array_key_exists('query', $uriParts)) {
56 1
            $queryParts = [];
57 1
            parse_str($uriParts['query'], $queryParts);
58 1
            if (array_key_exists('db', $queryParts) && !empty($queryParts['db'])) {
59 1
                $db = $queryParts['db'];
60 1
            }
61 1
        }
62
63
        return [
64 4
            'uri' => $uri,
65
            'db' => $db
66 4
        ];
67
    }
68
}
69