Passed
Push — master ( 716818...e06ec9 )
by Sebastian
02:13
created

IsEmpty::getFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
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 IsEmpty
23
 *
24
 * @package CaptainHook
25
 * @author  Felix Edelmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 5.4.0
28
 */
29
class IsEmpty implements Action
30
{
31
    /**
32
     * Executes the action
33
     *
34
     * @param  \CaptainHook\App\Config           $config
35
     * @param  \CaptainHook\App\Console\IO       $io
36
     * @param  \SebastianFeldmann\Git\Repository $repository
37
     * @param  \CaptainHook\App\Config\Action    $action
38
     * @return void
39
     * @throws \Exception
40
     */
41
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
42
    {
43
        $failedFiles = 0;
44
        foreach ($this->getFiles($action->getOptions()) as $file) {
45
            if (!$this->isEmpty($file)) {
46
                $io->write('- <error>FAIL</error> ' . $file, true);
47
                $failedFiles++;
48
            } else {
49
                $io->write('- <info>OK</info> ' . $file, true, IO::VERBOSE);
50
            }
51
        }
52
53
        if ($failedFiles > 0) {
54
            throw new ActionFailed('<error>Error: ' . $failedFiles . ' non-empty file(s)</error>');
55
        }
56
57
        $io->write('<info>All files are empty or don\'t exist</info>');
58
    }
59
60
    /**
61
     * Extract files list from the action configuration
62
     *
63
     * @param \CaptainHook\App\Config\Options $options
64
     * @return array
65
     * @throws \Exception
66
     */
67
    private function getFiles(Config\Options $options): array
68
    {
69
        $files = $options->get('files');
70
        if (!is_array($files)) {
0 ignored issues
show
introduced by
The condition is_array($files) is always false.
Loading history...
71
            throw new Exception('Missing option "files" for IsEmpty action');
72
        }
73
        return $files;
74
    }
75
76
    /**
77
     * Returns true when the file is empty or doesn't exist
78
     *
79
     * @param  string $file
80
     * @return bool
81
     */
82
    protected function isEmpty($file): bool
83
    {
84
        if (!file_exists($file)) {
85
            return true;
86
        }
87
88
        return filesize($file) === 0;
89
    }
90
}
91