Completed
Push — master ( f89a6d...41603f )
by Bjørn
02:37
created

writeMissingAndChangedFiles()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 8.4444
cc 8
nc 14
nop 1
crap 72
1
<?php
2
3
namespace HtaccessCapabilityTester;
4
5
class SimpleTestFileLineUpper implements TestFilesLineUpperInterface
6
{
7
8
    /**
9
     * Write missing and changed files.
10
     *
11
     * @param  array  $files   The files that needs to be there
12
     *
13
     * @return  void
14
     */
15
    private function writeMissingAndChangedFiles($files)
16
    {
17
        foreach ($files as $file) {
18
            $success = true;
19
            list($filename, $content) = $file;
20
            $dir = dirname($filename);
21
            if (!is_dir($dir)) {
22
                if (!mkdir($dir, 0777, true)) {
23
                    // TODO: Use custom exception
24
                    throw new \Exception('Failed creating dir: ' . $dir);
25
                }
26
            }
27
            if (file_exists($filename)) {
28
                // file already exists, now check if content is the same
29
                $existingContent = file_get_contents($filename);
30
                if (($existingContent === false) || ($content != $existingContent)) {
31
                    $success = file_put_contents($filename, $content);
32
                }
33
            } else {
34
                $success = file_put_contents($filename, $content);
35
            }
36
            if (!$success) {
37
                // TODO: Use custom exception
38
                throw new \Exception('Failed creating file: ' . $filename);
39
            }
40
        }
41
    }
42
43
    /**
44
     * Remove unused files.
45
     *
46
     * @param  array  $files   The files that needs to be there (others will be removed)
47
     *
48
     * @return  void
49
     */
50
    private function removeUnusedFiles($files)
51
    {
52
        $dirs = [];
53
        foreach ($files as $file) {
54
            list($filename, $content) = $file;
55
            $dir = dirname($filename);
56
            if (!isset($dirs[$dir])) {
57
                $dirs[$dir] = [];
58
            }
59
            $dirs[$dir][] = basename($filename);
60
        }
61
62
        foreach ($dirs as $dir => $filesSupposedToBeInDir) {
63
            $fileIterator = new \FilesystemIterator($dir);
64
            while ($fileIterator->valid()) {
65
                $filename = $fileIterator->getFilename();
66
                if (!in_array($filename, $filesSupposedToBeInDir)) {
67
                    unlink($dir . '/' . $filename);
68
                }
69
                $fileIterator->next();
70
            }
71
        }
72
    }
73
74
    /**
75
     * Line-up test files.
76
     *
77
     * This method should make sure that the files passed in are there and are up-to-date.
78
     * - If a file is missing, it should be created.
79
     * - If a file has changed content, it should be updated
80
     * - If the directory contains a file/dir that should not be there, it should be removed
81
     *
82
     * @param  array  $files   The files that needs to be there
83
     *
84
     * @return  void
85
     */
86
    public function lineUp($files)
87
    {
88
        // 1. Put missing files / changed files
89
        $this->writeMissingAndChangedFiles($files);
90
91
        // 2. Remove unused files
92
        $this->removeUnusedFiles($files);
93
    }
94
}
95