Completed
Push — develop ( b29d0d...9c2be0 )
by
unknown
16s queued 11s
created

CatFileCommand::contentBySha()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Objects\NodeObject;
23
use \GitElephant\Objects\TreeishInterface;
24
use \GitElephant\Repository;
25
26
/**
27
 * cat-file command generator
28
 *
29
 * @author Matteo Giachino <[email protected]>
30
 */
31
class CatFileCommand extends BaseCommand
32
{
33
    const GIT_CAT_FILE = 'cat-file';
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
     * command to show content of a Object at a given Treeish point
48
     *
49
     * @param \GitElephant\Objects\NodeObject              $object  a Object instance
50
     * @param \GitElephant\Objects\TreeishInterface|string $treeish an object with TreeishInterface interface
51
     *
52
     * @throws \RuntimeException
53
     * @return string
54
     */
55 2
    public function content(NodeObject $object, $treeish): string
56
    {
57 2
        $this->clearAll();
58 2
        $sha = $treeish instanceof TreeishInterface ? $treeish->getSha() : $treeish;
59 2
        $this->addCommandName(static::GIT_CAT_FILE);
60
        // pretty format
61 2
        $this->addCommandArgument('-p');
62 2
        $this->addCommandSubject($sha . ':' . $object->getFullPath());
63
64 2
        return $this->getCommand();
65
    }
66
67
    /**
68
     * output an object content given it's sha
69
     *
70
     * @param string $sha
71
     *
72
     * @throws \RuntimeException
73
     * @return string
74
     */
75
    public function contentBySha($sha): string
76
    {
77
        $this->clearAll();
78
        $this->addCommandName(static::GIT_CAT_FILE);
79
        $this->addCommandArgument('-p');
80
        $this->addCommandSubject($sha);
81
82
        return $this->getCommand();
83
    }
84
}
85