Completed
Pull Request — master (#136)
by Jaume
07:17
created

HookCopier   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 60
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A copyPreCommitHook() 0 4 1
A copyCommitMsgHook() 0 4 1
A copyPrePushHook() 0 4 1
A copyPrepareCommitMsgHook() 0 4 1
A hookExists() 0 4 1
A copyFile() 0 7 1
A createHooksDir() 0 5 1
A setPermissions() 0 5 1
A copyHookFile() 0 7 2
1
<?php
2
3
namespace PhpGitHooks\Module\Configuration\Infrastructure\Hook;
4
5
use Symfony\Component\Process\Process;
6
7
class HookCopier
8
{
9
    public const DEFAULT_GIT_HOOKS_DIR = '.git/hooks';
10
11
    protected $hooksDir       = self::DEFAULT_GIT_HOOKS_DIR;
12
    protected $sourceHooksDir = __DIR__ . '/../../../../Infrastructure/Hook';
13
14
    public function copyPreCommitHook(): void
15
    {
16
        $this->copyHookFile('pre-commit');
17
    }
18
19
    public function copyCommitMsgHook(): void
20
    {
21
        $this->copyHookFile('commit-msg');
22
    }
23
24
    public function copyPrePushHook(): void
25
    {
26
        $this->copyHookFile('pre-push');
27
    }
28
29
    public function copyPrepareCommitMsgHook(): void
30
    {
31
        $this->copyHookFile('prepare-commit-msg');
32
    }
33
34
    protected function hookExists(string $hookFile): bool
35
    {
36
        return \file_exists($this->hooksDir . "/$hookFile");
37
    }
38
39
    protected function copyFile(string $hookFile): void
40
    {
41
        $this->createHooksDir();
42
43
        $copy = new Process(['cp', $hookFile, $this->hooksDir]);
44
        $copy->run();
45
    }
46
47
    protected function createHooksDir(): void
48
    {
49
        $mkdir = new Process(['mkdir', '-p', $this->hooksDir]);
50
        $mkdir->run();
51
    }
52
53
    protected function setPermissions(string $hookFile): void
54
    {
55
        $permissions = new Process(['chmod', '775', $this->hooksDir . "/$hookFile"]);
56
        $permissions->run();
57
    }
58
59
    protected function copyHookFile(string $file): void
60
    {
61
        if (false === $this->hookExists($file)) {
62
            $this->copyFile($this->sourceHooksDir . "/$file");
63
            $this->setPermissions($file);
64
        }
65
    }
66
}
67