Passed
Push — master ( 80f541...43846d )
by Sebastian
03:16
created

FileChanged::getChangedFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Hook\Condition;
11
12
use CaptainHook\App\Console\IO;
13
use CaptainHook\App\Hook\Condition;
14
use SebastianFeldmann\Git\Repository;
15
16
/**
17
 * Class FileChange
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/captainhookphp/captainhook
22
 * @since   Class available since Release 4.2.0
23
 */
24
abstract class FileChanged implements Condition
25
{
26
    /**
27
     * @var string[]
28
     */
29
    protected $filesToWatch;
30
31
    /**
32
     * FileChange constructor.
33
     *
34
     * @param string[] $files
35
     */
36 5
    public function __construct(array $files)
37
    {
38 5
        $this->filesToWatch = $files;
39 5
    }
40
41
    /**
42
     * Evaluates a condition
43
     *
44
     * @param \CaptainHook\App\Console\IO       $io
45
     * @param \SebastianFeldmann\Git\Repository $repository
46
     * @return bool
47
     */
48
    abstract public function isTrue(IO $io, Repository $repository): bool;
49
50
    /**
51
     * Use 'diff-tree' to find the changed files after this merge or checkout
52
     *
53
     * In case of a checkout it is easy because the arguments 'previousHead' and 'newHead' exist.
54
     * In case of a merge determining this hashes is more difficult so we are using the 'ref-log'
55
     * to do it and using 'HEAD@{1}' as the last position before the merge and 'HEAD' as the
56
     * current position after the merge.
57
     *
58
     * @param \CaptainHook\App\Console\IO       $io
59
     * @param \SebastianFeldmann\Git\Repository $repository
60
     * @return array|string[]
61
     */
62 5
    protected function getChangedFiles(IO $io, Repository $repository)
63
    {
64 5
        $oldHash = $io->getArgument('previousHead', 'HEAD@{1}');
65 5
        $newHash = $io->getArgument('newHead', 'HEAD');
66
67 5
        return $repository->getDiffOperator()->getChangedFiles($oldHash, $newHash);
68
    }
69
}
70