Completed
Push — develop ( 5f7df1...9cb564 )
by Matteo
10s
created

SubmoduleCommand::lists()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
 * Submodule command generator
26
 *
27
 * @author Matteo Giachino <[email protected]>
28
 */
29
class SubmoduleCommand extends BaseCommand
30
{
31
    const SUBMODULE_COMMAND          = 'submodule';
32
    const SUBMODULE_ADD_COMMAND      = 'add';
33
    const SUBMODULE_INIT_COMMAND     = 'init';
34
    const SUBMODULE_UPDATE_COMMAND   = 'update';
35
    const SUBMODULE_OPTION_FORCE     = '--force';
36
    const SUBMODULE_OPTION_INIT      = '--init';
37
    const SUBMODULE_OPTION_RECURSIVE = '--recursive';
38
39
    /**
40
     * constructor
41
     *
42
     * @param \GitElephant\Repository $repo The repository object this command 
43
     *                                      will interact with
44
     */
45 1
    public function __construct(Repository $repo = null)
46
    {
47 1
        parent::__construct($repo);
48 1
    }
49
50
    /**
51
     * add a submodule
52
     *
53
     * @param string $gitUrl git url of the submodule
54
     * @param string $path   path to register the submodule to
55
     *
56
     * @throws \RuntimeException
57
     * @return string
58
     */
59 1
    public function add($gitUrl, $path = null)
60
    {
61 1
        $this->clearAll();
62 1
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_ADD_COMMAND));
63 1
        $this->addCommandArgument($gitUrl);
64 1
        if (null !== $path) {
65
            $this->addCommandSubject($path);
66
        }
67
68 1
        return $this->getCommand();
69
    }
70
71
    /**
72
     * initialize a repository's submodules
73
     *
74
     * @param  string $path init only submodules at the specified path
75
     *
76
     * @return string
77
     */
78
    public function init($path = null)
79
    {
80
        $this->clearAll();
81
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_INIT_COMMAND));
82
        if (null !== $path) {
83
            $this->addPath($path);
84
        }
85
86
        return $this->getCommand();
87
    }
88
89
    /**
90
     * Lists submodules
91
     *
92
     * @throws \RuntimeException
93
     * @return string the command
94
     */
95
    public function listSubmodules()
96
    {
97
        $this->clearAll();
98
        $this->addCommandName(self::SUBMODULE_COMMAND);
99
100
        return $this->getCommand();
101
    }
102
103
    /**
104
     * Lists submodules
105
     *
106
     * @deprecated This method uses an unconventional name but is being left in
107
     *             place to remain compatible with existing code relying on it.
108
     *             New code should be written to use listSubmodules().
109
     *
110
     * @throws \RuntimeException
111
     * @return string the command
112
     */
113
    public function lists()
114
    {
115
        return $this->listSubmodules();
116
    }
117
118
    /**
119
     * update a repository's submodules
120
     *
121
     * @param  bool   $recursive update recursively
122
     * @param  bool   $init      init before update
123
     * @param  bool   $force     force the checkout as part of update
124
     * @param  string $path      update only a specific submodule path
125
     *
126
     * @return string
127
     */
128
    public function update($recursive = false, $init = false, $force = false, $path = null)
129
    {
130
        $this->clearAll();
131
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_UPDATE_COMMAND));
132
        if ($recursive === true) {
133
            $this->addCommandArgument(self::SUBMODULE_OPTION_RECURSIVE);
134
        }
135
        if ($init === true) {
136
            $this->addCommandArgument(self::SUBMODULE_OPTION_INIT);
137
        }
138
        if ($force === true) {
139
            $this->addCommandArgument(self::SUBMODULE_OPTION_FORCE);
140
        }
141
        if ($path !== null) {
142
            $this->addPath($path);
143
        }
144
145
        return $this->getCommand();
146
    }
147
}
148