FileHandle::getFileInfo()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Maristela\Cli;
4
5
class FileHandle
6
{
7
    public function write(string $file, string $content, string $dir)
8
    {
9
        if (!is_dir($dir)) {
10
            mkdir($dir, 0755);
11
        }
12
13
        $fileName = $this->getComponentName($file);
14
15
        file_put_contents("{$dir}/{$fileName}.html", $content);
16
    }
17
18
    public function getMockData(string $file) : array
19
    {
20
        $fileInfo = $this->getFileInfo($file);
21
        $mockFile = "{$fileInfo['dirname']}/mock.json";
22
23
        if (!file_exists($mockFile)) {
24
            throw new \InvalidArgumentException('The mock.json must be at same level of php file.');
25
        }
26
27
        $data = json_decode(file_get_contents($mockFile), true);
28
29
        if (empty($data)) {
30
            throw new \InvalidArgumentException('Invalid mock.json!');
31
        }
32
33
        return $data;
34
    }
35
36
    public function getComponentName(string $file) : string
37
    {
38
        $fileInfo = $this->getFileInfo($file);
39
        return basename($fileInfo['dirname']);
40
    }
41
42
    protected function getFileInfo(string $file) : array
43
    {
44
        if (!file_exists($file)) {
45
            throw new \InvalidArgumentException("File: {$file} does not exists!");
46
        }
47
48
        $fileInfo = pathinfo($file);
49
50
        if ($fileInfo['extension'] !== 'php') {
51
            throw new \InvalidArgumentException('File need to be a php!');
52
        }
53
54
        return $fileInfo;
55
    }
56
}
57