Completed
Push — master ( ae5ea4...18538b )
by Sebastian
05:19
created

Hook::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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 CaptainHook\App\Console\Command;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Runner;
14
use SebastianFeldmann\Git\Repository;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class Hook
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/sebastianfeldmann/captainhook
24
 * @since   Class available since Release 0.9.0
25
 */
26
abstract class Hook extends Base
27
{
28
    /**
29
     * Hook to execute
30
     *
31
     * @var string
32
     */
33
    protected $hookName;
34
35
    /**
36
     * Path to the configuration file to use
37
     *
38
     * @var string
39
     */
40
    protected $configFile;
41
42
    /**
43
     * Path to the git repository to use.
44
     *
45
     * @var string
46
     */
47
    protected $repositoryPath;
48
49
    /**
50
     * Hook constructor
51
     *
52
     * @param string $configFile
53
     * @param string $repositoryPath
54
     */
55 5
    public function __construct(string $configFile, string $repositoryPath)
56
    {
57 5
        $this->configFile     = $configFile;
58 5
        $this->repositoryPath = $repositoryPath;
59 5
        parent::__construct();
60 5
    }
61
62
    /**
63
     * Configure the command
64
     */
65 5
    protected function configure()
66
    {
67 5
        $this->setName($this->hookName)
68 5
             ->setDescription('Run git ' . $this->hookName . ' hook.')
69 5
             ->setHelp('This command executes the ' . $this->hookName . ' hook.');
70 5
    }
71
72
    /**
73
     * Execute the command
74
     *
75
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
76
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
77
     * @return void
78
     * @throws \CaptainHook\App\Exception\InvalidHookName
79
     */
80 5
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82 5
        $io         = $this->getIO($input, $output);
83 5
        $config     = $this->getConfig($this->configFile, true);
84 5
        $repository = new Repository($this->repositoryPath);
85
86
        // handle hook specific bootstrap
87
        // for example load the commit message for validation
88 4
        $this->setup($input, $output, $config, $repository);
89
90
        // execute the hook and all its actions
91 4
        $hook = new Runner\Hook($io, $config, $repository);
92 4
        $hook->setHook($this->hookName);
93 4
        $hook->run();
94
95
        // handle hook specific post actions
96
        // for example writing a prepared commit message to disk
97 4
        $this->tearDown($input, $output, $config, $repository);
98 4
    }
99
100
    /**
101
     * Hook specific bootstrapping
102
     *
103
     * @param \Symfony\Component\Console\Input\InputInterface   $input
104
     * @param \Symfony\Component\Console\Output\OutputInterface $output
105
     * @param \CaptainHook\App\Config                           $config
106
     * @param \SebastianFeldmann\Git\Repository                 $repository
107
     */
108 3
    protected function setup(InputInterface $input, OutputInterface $output, Config $config, Repository $repository)
109
    {
110
        // do something fooish
111 3
    }
112
113
    /**
114
     * Post action after all the configured actions are executed
115
     *
116
     * @param \Symfony\Component\Console\Input\InputInterface   $input
117
     * @param \Symfony\Component\Console\Output\OutputInterface $output
118
     * @param \CaptainHook\App\Config                           $config
119
     * @param \SebastianFeldmann\Git\Repository                 $repository
120
     */
121 3
    protected function tearDown(InputInterface $input, OutputInterface $output, Config $config, Repository $repository)
1 ignored issue
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        // do some more foolish things.
124 3
    }
125
}
126