Passed
Pull Request — master (#103)
by Sebastian
02:29
created

IsNotEmpty   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 5
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\File\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception\ActionFailed;
17
use CaptainHook\App\Hook\Action;
18
use Exception;
19
use SebastianFeldmann\Git\Repository;
20
21
/**
22
 * Class IsNotEmpty
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 5.4.1
28
 */
29
class IsNotEmpty extends Check
30
{
31
    /**
32
     * Actual action name for better error messages
33
     *
34
     * @var string
35
     */
36
    protected $actionName = 'IsNotEmpty';
37
38
    /**
39
     * Executes the action
40
     *
41
     * @param  \CaptainHook\App\Config           $config
42
     * @param  \CaptainHook\App\Console\IO       $io
43
     * @param  \SebastianFeldmann\Git\Repository $repository
44
     * @param  \CaptainHook\App\Config\Action    $action
45
     * @return void
46
     * @throws \Exception
47
     */
48
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
49
    {
50
        $failedFiles = 0;
51
        foreach ($this->getFiles($action->getOptions()) as $glob => $files) {
52
            if (empty($files) || $this->isAnyFileEmpty($files)) {
53
                $io->write('- <error>FAIL</error> ' . $glob, true);
54
                $failedFiles++;
55
            } else {
56
                $io->write('- <info>OK</info> ' . $glob, true, IO::VERBOSE);
57
            }
58
        }
59
60
        if ($failedFiles > 0) {
61
            throw new ActionFailed('<error>Error: ' . $failedFiles . ' empty file(s)</error>');
62
        }
63
64
        $io->write('<info>None of the files is empty</info>');
65
    }
66
}
67