SubmoduleCommand::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command;
22
23
use GitElephant\Repository;
24
25
/**
26
 * Submodule command generator
27
 *
28
 * @author Matteo Giachino <[email protected]>
29
 */
30
class SubmoduleCommand extends BaseCommand
31
{
32
    public const SUBMODULE_COMMAND = 'submodule';
33
    public const SUBMODULE_ADD_COMMAND = 'add';
34
    public const SUBMODULE_INIT_COMMAND = 'init';
35
    public const SUBMODULE_UPDATE_COMMAND = 'update';
36
    public const SUBMODULE_OPTION_FORCE = '--force';
37
    public const SUBMODULE_OPTION_INIT = '--init';
38
    public const SUBMODULE_OPTION_RECURSIVE = '--recursive';
39
40
    /**
41
     * constructor
42
     *
43
     * @param \GitElephant\Repository $repo The repository object this command
44
     *                                      will interact with
45
     */
46 1
    public function __construct(Repository $repo = null)
47
    {
48 1
        parent::__construct($repo);
49 1
    }
50
51
    /**
52
     * add a submodule
53
     *
54
     * @param string $gitUrl git url of the submodule
55
     * @param string $path   path to register the submodule to
56
     *
57
     * @throws \RuntimeException
58
     * @return string
59
     */
60 1
    public function add($gitUrl, $path = null): string
61
    {
62 1
        $this->clearAll();
63 1
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_ADD_COMMAND));
64 1
        $this->addCommandArgument($gitUrl);
65 1
        if (null !== $path) {
66
            $this->addCommandSubject($path);
67
        }
68
69 1
        return $this->getCommand();
70
    }
71
72
    /**
73
     * initialize a repository's submodules
74
     *
75
     * @param  string $path init only submodules at the specified path
76
     *
77
     * @return string
78
     */
79
    public function init($path = null): string
80
    {
81
        $this->clearAll();
82
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_INIT_COMMAND));
83
        if (null !== $path) {
84
            $this->addPath($path);
85
        }
86
87
        return $this->getCommand();
88
    }
89
90
    /**
91
     * Lists submodules
92
     *
93
     * @throws \RuntimeException
94
     * @return string the command
95
     */
96
    public function listSubmodules(): string
97
    {
98
        $this->clearAll();
99
        $this->addCommandName(self::SUBMODULE_COMMAND);
100
101
        return $this->getCommand();
102
    }
103
104
    /**
105
     * Lists submodules
106
     *
107
     * @deprecated This method uses an unconventional name but is being left in
108
     *             place to remain compatible with existing code relying on it.
109
     *             New code should be written to use listSubmodules().
110
     *
111
     * @throws \RuntimeException
112
     * @return string the command
113
     */
114
    public function lists(): string
115
    {
116
        return $this->listSubmodules();
117
    }
118
119
    /**
120
     * update a repository's submodules
121
     *
122
     * @param  bool   $recursive update recursively
123
     * @param  bool   $init      init before update
124
     * @param  bool   $force     force the checkout as part of update
125
     * @param  string $path      update only a specific submodule path
126
     *
127
     * @return string
128
     */
129
    public function update(
130
        bool $recursive = false,
131
        bool $init = false,
132
        bool $force = false,
133
        string $path = null
134
    ): string {
135
        $this->clearAll();
136
        $this->addCommandName(sprintf('%s %s', self::SUBMODULE_COMMAND, self::SUBMODULE_UPDATE_COMMAND));
137
        if ($recursive) {
138
            $this->addCommandArgument(self::SUBMODULE_OPTION_RECURSIVE);
139
        }
140
        if ($init) {
141
            $this->addCommandArgument(self::SUBMODULE_OPTION_INIT);
142
        }
143
        if ($force) {
144
            $this->addCommandArgument(self::SUBMODULE_OPTION_FORCE);
145
        }
146
        if ($path !== null) {
147
            $this->addPath($path);
148
        }
149
150
        return $this->getCommand();
151
    }
152
}
153