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

Any   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 6
dl 0
loc 30
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isTrue() 0 3 1
A didAnyFileChange() 0 8 3
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\FileChanged;
11
12
use CaptainHook\App\Console\IO;
13
use CaptainHook\App\Hook\Condition\FileChanged;
14
use SebastianFeldmann\Git\Repository;
15
16
/**
17
 * Class Any
18
 *
19
 * The FileChange condition is applicable for `post-merge` and `post-checkout` hooks.
20
 * For example it can be used to trigger an automatic composer install if the composer.json
21
 * or composer.lock file is changed during a checkout or merge.
22
 *
23
 * Example configuration:
24
 *
25
 * "action": "composer install"
26
 * "conditions": [
27
 *   {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChange\\Any",
28
 *    "args": [
29
 *      [
30
 *        "composer.json",
31
 *        "composer.lock"
32
 *      ]
33
 *    ]}
34
 * ]
35
 *
36
 * @package CaptainHook
37
 * @author  Sebastian Feldmann <[email protected]>
38
 * @link    https://github.com/captainhookphp/captainhook
39
 * @since   Class available since Release 4.2.0
40
 */
41
class Any extends FileChanged
42
{
43
    /**
44
     * Evaluates the condition
45
     *
46
     * @param  \CaptainHook\App\Console\IO       $io
47
     * @param  \SebastianFeldmann\Git\Repository $repository
48
     * @return bool
49
     */
50 3
    public function isTrue(IO $io, Repository $repository) : bool
51
    {
52 3
        return $this->didAnyFileChange($this->getChangedFiles($io, $repository));
53
    }
54
55
    /**
56
     * Check if any of the configured files was changed within the applied change set
57
     *
58
     * Important: If no files are configured this condition is always false.
59
     *
60
     * @param  array $changedFiles
61
     * @return bool
62
     */
63 3
    private function didAnyFileChange(array $changedFiles) : bool
64
    {
65 3
        foreach ($this->filesToWatch as $file) {
66 3
            if (in_array($file, $changedFiles)) {
67 2
                return true;
68
            }
69
        }
70 1
        return false;
71
    }
72
}
73