Completed
Push — master ( be0f4b...3d6dee )
by Pablo
02:31
created

HookCopier::copyPrePushHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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(sprintf('%s%s', $this->hooksDir, $hookFile));
37
    }
38
39
    protected function copyFile(string $hookFile): void
40
    {
41
        $copy = new Process(sprintf("mkdir -p {$this->hooksDir} && cp %s %s", $hookFile, $this->hooksDir));
42
        $copy->run();
43
    }
44
45
    protected function setPermissions(string $hookFile): void
46
    {
47
        $permissions = new Process(sprintf('chmod 775 %s%s', $this->hooksDir, $hookFile));
48
        $permissions->run();
49
    }
50
51
    protected function copyHookFile(string $file): void
52
    {
53
        if (false === $this->hookExists($file)) {
54
            $this->copyFile(sprintf('%s/%s', $this->sourceHooksDir, $file));
55
            $this->setPermissions($file);
56
        }
57
    }
58
}
59