Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

Hook   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 108
c 0
b 0
f 0
ccs 25
cts 25
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRepositoryPath() 0 4 1
A getRepositoryPath() 0 7 2
A setHook() 0 8 2
A doRun() 0 7 1
A createCommand() 0 9 1
A getHookCommand() 0 7 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\CaptainHook\Console\Application;
11
12
use SebastianFeldmann\CaptainHook\Hook\Util;
13
use SebastianFeldmann\CaptainHook\Console\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class Hook
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/captainhook
23
 * @since   Class available since Release 0.9.0
24
 */
25
class Hook extends ConfigHandler
26
{
27
    /**
28
     * Path to the git repository
29
     *
30
     * @var string
31
     */
32
    protected $repositoryPath;
33
34
    /**
35
     * Hook that gets executed.
36
     *
37
     * @var string
38
     */
39
    protected $hookToExecute;
40
41
    /**
42
     * Hook to command map
43
     *
44
     * @var array
45
     */
46
    protected $hookCommandMap = [
47
        'pre-commit' => 'PreCommit',
48
        'commit-msg' => 'CommitMsg',
49
        'pre-push'   => 'PrePush'
50
    ];
51
52
    /**
53
     * Repository path setter.
54
     *
55
     * @param string $git
56 1
     */
57
    public function setRepositoryPath(string $git)
58 1
    {
59 1
        $this->repositoryPath = $git;
60
    }
61
62
    /**
63
     * Get the git repository root path.
64 2
     *
65
     * @return string
66 2
     */
67 1
    public function getRepositoryPath() : string
68
    {
69 2
        if (empty($this->repositoryPath)) {
70
            $this->repositoryPath = getcwd();
71
        }
72
        return $this->repositoryPath;
73
    }
74
75
    /**
76
     * Set the hook to execute.
77
     *
78 2
     * @param  string $hook
79
     * @return \SebastianFeldmann\CaptainHook\Console\Application\Hook
80 2
     */
81 1
    public function setHook(string $hook)
82
    {
83 1
        if (!Util::isValid($hook)) {
84 1
            throw new \RuntimeException('Invalid hook name');
85
        }
86
        $this->hookToExecute = $hook;
87
        return $this;
88
    }
89
90
    /**
91
     * Execute hook.
92
     *
93
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
94 2
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
95
     * @return int
96 2
     */
97
    public function doRun(InputInterface $input, OutputInterface $output)
98 2
    {
99 1
        $input->setInteractive(false);
100
101
        $command = $this->createCommand();
102
        return $command->run($input, $output);
103
    }
104
105
    /**
106
     * Create the hook command.
107 2
     *
108
     * @return \SebastianFeldmann\CaptainHook\Console\Command\Hook
109
     */
110 2
    private function createCommand() : Command\Hook
111 1
    {
112 1
        /* @var \SebastianFeldmann\CaptainHook\Console\Command\Hook $command */
113
        $class   = '\\SebastianFeldmann\\CaptainHook\\Console\\Command\\Hook\\' . $this->getHookCommand();
114 1
        $command = new $class($this->getConfigFile(), $this->getRepositoryPath());
115
        $command->setHelperSet($this->getHelperSet());
116
117
        return $command;
118
    }
119
120
    /**
121
     * Get the command class name to execute.
122 2
     *
123
     * @return string
124 2
     */
125 1
    private function getHookCommand() : string
126
    {
127 1
        if (null === $this->hookToExecute) {
128
            throw new \RuntimeException('No hook to execute');
129
        }
130
        return $this->hookCommandMap[$this->hookToExecute];
131
    }
132
}
133