Completed
Push — develop ( 4961cb...5f7df1 )
by Matteo
03:22
created

CatFileCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Object;
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\Object                  $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(Object $object, $treeish)
56
    {
57 2
        $this->clearAll();
58 2
        if ($treeish instanceof TreeishInterface) {
59 2
            $sha = $treeish->getSha();
60 2
        } else {
61 1
            $sha = $treeish;
62
        }
63 2
        $this->addCommandName(static::GIT_CAT_FILE);
64
        // pretty format
65 2
        $this->addCommandArgument('-p');
66 2
        $this->addCommandSubject($sha . ':' . $object->getFullPath());
67
68 2
        return $this->getCommand();
69
    }
70
71
    /**
72
     * output an object content given it's sha
73
     *
74
     * @param string $sha
75
     *
76
     * @throws \RuntimeException
77
     * @return string
78
     */
79
    public function contentBySha($sha)
80
    {
81
        $this->clearAll();
82
        $this->addCommandName(static::GIT_CAT_FILE);
83
        $this->addCommandArgument('-p');
84
        $this->addCommandSubject($sha);
85
86
        return $this->getCommand();
87
    }
88
}
89