MvCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Objects\NodeObject;
24
use GitElephant\Repository;
25
26
/**
27
 * Class MvCommand
28
 *
29
 * @package GitElephant\Command
30
 */
31
class MvCommand extends BaseCommand
32
{
33
    public const MV_COMMAND = 'mv';
34
35
    /**
36
     * constructor
37
     *
38
     * @param \GitElephant\Repository $repo The repository object this command
39
     *                                      will interact with
40
     */
41 2
    public function __construct(Repository $repo = null)
42
    {
43 2
        parent::__construct($repo);
44 2
    }
45
46
    /**
47
     * @param string|NodeObject $source source name
48
     * @param string            $target dest name
49
     *
50
     * @throws \RuntimeException
51
     * @throws \InvalidArgumentException
52
     * @return string
53
     */
54 2
    public function rename($source, $target): string
55
    {
56 2
        if ($source instanceof NodeObject) {
57 1
            if (!$source->isBlob()) {
58
                throw new \InvalidArgumentException("The given object is not a blob, it couldn't be renamed");
59
            }
60 1
            $sourceName = $source->getFullPath();
61
        } else {
62 2
            $sourceName = $source;
63
        }
64 2
        $this->clearAll();
65 2
        $this->addCommandName(self::MV_COMMAND);
66
        // Skip move or rename actions which would lead to an error condition
67 2
        $this->addCommandArgument('-k');
68 2
        $this->addCommandSubject($sourceName);
69 2
        $this->addCommandSubject2($target);
70
71 2
        return $this->getCommand();
72
    }
73
}
74