Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

Repository::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Repository;
13
14
use Webcreate\Conveyor\IO\IOInterface;
15
use Webcreate\Conveyor\Repository\Driver\DriverInterface;
16
17
class Repository
18
{
19
    protected $drivers = array();
20
21
    /** @var  DriverInterface */
22
    protected $driver;
23
    protected $type;
24
    protected $url;
25
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
26
    protected $cacheDir;
27
28
    /**
29
     * @param string           $type     name of a registered driver
30
     * @param string           $url
31
     * @param null|IOInterface $io
32
     * @param null|array       $drivers
33
     * @param null|string      $cacheDir
34
     */
35 1
    public function __construct($type, $url, IOInterface $io = null, array $drivers = null, $cacheDir = null)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
36
    {
37 1
        $this->drivers = $drivers ? $drivers : array(
38 1
            'svn' => 'Webcreate\\Conveyor\\Repository\\Driver\\SvnDriver',
39
            'git' => 'Webcreate\\Conveyor\\Repository\\Driver\\GitDriver',
40
        );
41
42 1
        $this->io   = $io;
43 1
        $this->type = $type;
44 1
        $this->url  = $url;
45 1
        $this->cacheDir = preg_replace('/^\~/', getenv('HOME'), $cacheDir);
46 1
    }
47
48
    /**
49
     * @return DriverInterface
50
     * @throws \RuntimeException
51
     */
52 1
    protected function getDriver()
53
    {
54 1
        if (isset($this->driver)) {
55
            return $this->driver;
56
        }
57
58 1
        if (isset($this->drivers[$this->type])) {
59 1
            $driver = $this->drivers[$this->type];
60
61 1
            if (is_string($driver)) {
62
                $this->driver = new $driver($this->url, $this->io);
63
            } else {
64 1
                $this->driver = $driver;
65
            }
66
67 1
            $this->driver->setCacheDir($this->cacheDir);
68 1
            $this->driver->initialize($this->url);
69
        }
70
71 1
        if (!$this->driver) {
72
            throw new \RuntimeException(sprintf('Driver for type \'%s\' not found', $this->type));
73
        }
74
75 1
        return $this->driver;
76
    }
77
78 1
    public function addDriver($type, DriverInterface $driver)
79
    {
80 1
        $this->drivers[$type] = $driver;
81 1
    }
82
83
    public function getType()
84
    {
85
        return $this->type;
86
    }
87
88
    public function getUrl()
89
    {
90
        return $this->url;
91
    }
92
93
    /**
94
     * @todo move this to webcreate/vcs library
95
     *
96
     * @return string
97
     */
98
    public function getMasterBranch()
99
    {
100
        $branch = 'unknown';
101
102
        switch ($this->getType()) {
103
            case "git":
104
                $branch = 'master';
105
                break;
106
            case "svn":
107
                $branch = 'trunk';
108
                break;
109
        }
110
111
        return $branch;
112
    }
113
114
    /**
115
     * @return Version[]
116
     */
117 1
    public function getVersions()
118
    {
119 1
        return $this->getDriver()->getVersions();
120
    }
121
122
    /**
123
     * @param string $name
124
     * @throws \InvalidArgumentException
125
     * @return Version
126
     */
127
    public function getVersion($name)
128
    {
129
        foreach ($this->getVersions() as $version) {
130
            if ($version->getName() === $name) {
131
                return $version;
132
            }
133
        }
134
135
        throw new \InvalidArgumentException(sprintf('Version \'%s\' not found', $name));
136
    }
137
138
    public function export(Version $version, $dest)
139
    {
140
        return $this->getDriver()->export($version, $dest);
141
    }
142
143
    /**
144
     * @param  Version                             $oldVersion
145
     * @param  Version                             $newVersion
146
     * @return \Webcreate\Vcs\Common\VcsFileInfo[]
147
     */
148
    public function diff(Version $oldVersion, Version $newVersion)
149
    {
150
        return $this->getDriver()->diff($oldVersion, $newVersion);
151
    }
152
153
    /**
154
     * @param Version $version1
155
     * @param Version $version2
156
     * @return int
157
     */
158
    public function versionCompare(Version $version1, Version $version2)
159
    {
160
        $build1 = $version1->getBuild();
161
        $build2 = $version2->getBuild();
162
163
        return $this->getDriver()->revisionCompare($build1, $build2);
164
    }
165
166
    public function changelog(Version $version1, Version $version2)
167
    {
168
        return $this->getDriver()->changelog($version1, $version2);
169
    }
170
}
171