FileHandle   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 50
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMockData() 0 16 3
A getComponentName() 0 4 1
A getFileInfo() 0 13 3
A write() 0 9 2
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