Passed
Pull Request — master (#56)
by Guy
10:03
created

DriverReflection::getSshUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker;
4
5
use Composer\Repository\VcsRepository;
6
use ReflectionObject;
7
use RuntimeException;
8
9
class DriverReflection
10
{
11
    public static function getDriverWithoutException(VcsRepository $repo)
12
    {
13
        $reflectedRepo = new ReflectionObject($repo);
14
        $reflectedDrivers = $reflectedRepo->getProperty('drivers');
15
        $reflectedDrivers->setAccessible(true);
16
        $drivers = $reflectedDrivers->getValue($repo);
17
        if (isset($drivers[$repo->type])) {
0 ignored issues
show
Bug introduced by
The property type is declared protected in Composer\Repository\VcsRepository and cannot be accessed from this context.
Loading history...
18
            $class = $drivers[$repo->type];
19
            $driver = new $class($repo->repoConfig, $repo->io, $repo->config);
0 ignored issues
show
Bug introduced by
The property repoConfig is declared protected in Composer\Repository\VcsRepository and cannot be accessed from this context.
Loading history...
Bug introduced by
The property io is declared protected in Composer\Repository\VcsRepository and cannot be accessed from this context.
Loading history...
Bug introduced by
The property config is declared protected in Composer\Repository\VcsRepository and cannot be accessed from this context.
Loading history...
20
            try {
21
                $driver->initialize();
22
            } catch (RuntimeException $e) {
23
                // no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh
24
                // but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now.
25
            }
26
            return $driver;
27
        }
28
29
        foreach ($drivers as $driver) {
30
            if ($driver::supports($repo->io, $repo->config, $repo->url)) {
0 ignored issues
show
Bug introduced by
The property url is declared protected in Composer\Repository\VcsRepository and cannot be accessed from this context.
Loading history...
31
                $driver = new $driver($repo->repoConfig, $repo->io, $repo->config);
32
                try {
33
                    $driver->initialize();
34
                } catch (RuntimeException $e) {
35
                    // no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh
36
                    // but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now.
37
                }
38
                return $driver;
39
            }
40
        }
41
42
        foreach ($drivers as $driver) {
43
            if ($driver::supports($repo->io, $repo->config, $repo->url, true)) {
44
                $driver = new $driver($repo->repoConfig, $repo->io, $repo->config);
45
                try {
46
                    $driver->initialize();
47
                } catch (RuntimeException $e) {
48
                    // no-op - this is probably caused due to insufficient permissions when trying to create /var/www/.ssh
49
                    // but since we're just getting the driver as a shortcut to getting the repository name, we can ignore this for now.
50
                }
51
                return $driver;
52
            }
53
        }
54
    }
55
56
    public static function getSshUrl($driver)
57
    {
58
        $reflectedDriver = new ReflectionObject($driver);
59
        if ($reflectedDriver->hasMethod('generateSshUrl')) {
60
            $reflectedMethod = $reflectedDriver->getMethod('generateSshUrl');
61
            $reflectedMethod->setAccessible(true);
62
            return $reflectedMethod->invoke($driver);
63
        }
64
        return null;
65
    }
66
}
67