Completed
Pull Request — develop (#148)
by
unknown
05:08 queued 03:19
created

CloneCommand::cloneUrl()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.3188

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 16
cts 19
cp 0.8421
rs 8.0555
c 0
b 0
f 0
cc 9
nc 26
nop 5
crap 9.3188
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant\Command;
21
22
use GitElephant\Repository;
23
24
/**
25
 * CloneCommand generator
26
 *
27
 * @author Matteo Giachino <[email protected]>
28
 * @author Kirk Madera <[email protected]>
29
 */
30
class CloneCommand extends BaseCommand
31
{
32
    const GIT_CLONE_COMMAND = 'clone';
33
34
    /**
35
     * constructor
36
     *
37
     * @param \GitElephant\Repository $repo The repository object this command
38
     *                                      will interact with
39
     */
40 3
    public function __construct(Repository $repo = null)
41
    {
42 3
        parent::__construct($repo);
43 3
    }
44
45
    /**
46
     * Command to clone a repository
47
     *
48
     * @param string      $url           repository url
49
     * @param string      $to            where to clone the repo
50
     * @param string|null $repoReference Repo reference to clone. Required if performing a shallow clone.
51
     * @param int|null    $depth         Depth of commits to clone
52
     * @param bool        $recursive     Whether to recursively clone submodules.
53
     *
54
     * @throws \RuntimeException
55
     * @return string command
56
     */
57 3
    public function cloneUrl(string $url, string $to = null, string $repoReference = null, int $depth = null, bool $recursive = false)
58
    {
59 3
        $this->clearAll();
60 3
        $this->addCommandName(static::GIT_CLONE_COMMAND);
61 3
        $this->addCommandSubject($url);
62 3
        if (null !== $to) {
63 3
            $this->addCommandSubject2($to);
64
        }
65
66 3
        $binaryVersion = '2.0.0.0';
67
68 3
        if (null !== $repoReference) {
69
            // git documentation says the --branch was added in 2.0.0, but it exists undocumented at least back to 1.8.3.1
70 2
            if (version_compare($binaryVersion, '1.8.3.1', '<')) {
71
                throw new \RuntimeException(
72
                    'Please upgrade to git v1.8.3.1 or newer to support cloning a specific branch.'
73
                );
74
            }
75 2
            $this->addCommandArgument('--branch=' . $repoReference);
76
        }
77
78 3
        if (null !== $depth) {
79 2
            $this->addCommandArgument('--depth=' . $depth);
80
            // shallow-submodules is a nice to have feature. Just ignoring if git version not high enough
81
            // It would be nice if this had a logger injected for us to log notices
82 2
            if (version_compare($binaryVersion, '2.9.0', '>=') && $recursive && 1 == $depth) {
83
                $this->addCommandArgument('--shallow-submodules');
84
            }
85
        }
86
87 3
        if ($recursive) {
88 1
            $this->addCommandArgument('--recursive');
89
        }
90
91 3
        return $this->getCommand();
92
    }
93
}
94