OfType::isTrue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 2
b 0
f 1
nc 2
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Condition\FileChanged;
13
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Git;
16
use CaptainHook\App\Hook\Condition;
17
use CaptainHook\App\Hook\Constrained;
18
use CaptainHook\App\Hook\FileList;
19
use CaptainHook\App\Hook\Restriction;
20
use CaptainHook\App\Hooks;
21
use SebastianFeldmann\Git\Repository;
22
23
/**
24
 * Class OfType
25
 *
26
 * Example configuration:
27
 *
28
 * "action": "some-action"
29
 * "conditions": [
30
 *   {"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\OfType",
31
 *    "args": [
32
 *      "php"
33
 *    ]}
34
 * ]
35
 *
36
 * @package CaptainHook
37
 * @author  Sebastian Feldmann <[email protected]>
38
 * @link    https://github.com/captainhook-git/captainhook
39
 * @since   Class available since Release 5.0.0
40
 */
41
class OfType implements Condition, Constrained
42
{
43
    /**
44
     * File type to check e.g. 'php' or 'html'
45
     *
46
     * @var string
47
     */
48
    private string $suffix;
49
50
    /**
51
     * OfType constructor
52
     *
53
     * @param string $type
54
     */
55 4
    public function __construct(string $type)
56
    {
57 4
        $this->suffix = $type;
58
    }
59
60
    /**
61
     * Return the hook restriction information
62
     *
63
     * @return \CaptainHook\App\Hook\Restriction
64
     */
65 1
    public static function getRestriction(): Restriction
66
    {
67 1
        return Restriction::fromArray([Hooks::PRE_PUSH, Hooks::POST_CHECKOUT, Hooks::POST_MERGE, Hooks::POST_REWRITE]);
68
    }
69
70
    /**
71
     * Evaluates the condition
72
     *
73
     * @param  \CaptainHook\App\Console\IO       $io
74
     * @param  \SebastianFeldmann\Git\Repository $repository
75
     * @return bool
76
     */
77 4
    public function isTrue(IO $io, Repository $repository): bool
78
    {
79 4
        $files = FileList::filterByType(
80 4
            Git\ChangedFiles::getChangedFiles($io, $repository, ['A', 'C', 'M', 'R']),
81 4
            ['of-type' => $this->suffix]
82 4
        );
83
84 4
        if (count($files) > 0) {
85 1
            return true;
86
        }
87 3
        return false;
88
    }
89
}
90