Completed
Push — master ( 2a2836...3a2809 )
by Pieter
02:57
created

Repository::add()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Gitilicious\GitClient\Git;
4
5
use Gitilicious\GitClient\Cli\Input\LongFlag;
6
use Gitilicious\GitClient\Cli\Input\PlainArgument;
7
use Gitilicious\GitClient\Cli\Input\ShortFlag;
8
use Gitilicious\GitClient\Client;
9
use Gitilicious\GitClient\FileSystem\Directory;
10
use Gitilicious\GitClient\FileSystem\File;
11
12
class Repository
13
{
14
    private $client;
15
16
    private $directory;
17
18
    private $name;
19
20 10
    public function __construct(Client $client, Directory $directory, string $name)
21
    {
22 10
        $this->client    = $client;
23 10
        $this->directory = $directory;
24 10
        $this->name      = $name;
25 10
    }
26
27 4 View Code Duplication
    public static function createBare(Client $client, Directory $directory, string $name): Repository
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29 4
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
30
31 4
        $result = $client->run($repositoryDirectory, new PlainArgument('init'), new LongFlag('bare'));
32
33 4
        if (!$result->isSuccess()) {
34 1
            throw new Exception($result->getErrorMessage());
35
        }
36
37 3
        return new self($client, $directory, $name);
38
    }
39
40 8 View Code Duplication
    public static function create(Client $client, Directory $directory, string $name): Repository
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42 8
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
43
44 8
        $result = $client->run($repositoryDirectory, new PlainArgument('init'));
45
46 8
        if (!$result->isSuccess()) {
47 1
            throw new Exception($result->getErrorMessage());
48
        }
49
50 7
        return new self($client, $directory, $name);
51
    }
52
53 3
    public function clone(Directory $directory, string $name): Repository
54
    {
55 3
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);
56
57 3
        $result = $this->client->run(
58
            $repositoryDirectory,
59 3
            new PlainArgument('clone'),
60 3
            new PlainArgument($this->getPath()),
61 3
            new PlainArgument($repositoryDirectory->getPath())
62
        );
63
64 3
        if (!$result->isSuccess()) {
65 1
            throw new Exception($result->getErrorMessage());
66
        }
67
68 2
        return new self($this->client, $directory, $name);
69
    }
70
71 4
    public function getPath(): string
72
    {
73 4
        return $this->directory->getPath() . '/' . $this->name;
74
    }
75
76
    public function getBranches(): Branches
77
    {
78
        $result = $this->client->run($this->getPath(), 'branch');
79
80
        if (!$result->isSuccess()) {
81
            throw new Exception('Cannot list the branches.');
82
        }
83
84
        $branches = new Branches($this->client);
0 ignored issues
show
Unused Code introduced by
The call to Branches::__construct() has too many arguments starting with $this->client.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
86
        foreach ($result->getOutput() as $branch) {
0 ignored issues
show
Bug introduced by
The expression $result->getOutput() of type object<Gitilicious\GitClient\Cli\Output\Output> is not traversable.
Loading history...
87
            $branches->add(new Branch($branch));
88
        }
89
90
        return $branches;
91
    }
92
93
    public function add(File $file)
94
    {
95
        $this->client->run($this->directory, new PlainArgument('add'), new PlainArgument($file->getPath()));
96
    }
97
98
    public function commit(Author $author, string $message)
99
    {
100
        $this->client->run(
101
            $this->directory,
102
            new PlainArgument('commit'),
103
            new ShortFlag('m'),
104
            new PlainArgument($message),
105
            new LongFlag('author'),
106
            new PlainArgument($author->getFormatted())
107
        );
108
    }
109
110
    public function push()
111
    {
112
        $this->client->run($this->directory, new PlainArgument('push'));
113
    }
114
}
115