Passed
Push — master ( ff83e3...5f4a5c )
by Ehsan
03:54
created

FileUtility   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B jsonFileToArray() 0 18 5
1
<?php
2
3
namespace Botonomous\utility;
4
5
/**
6
 * Class FileUtility.
7
 */
8
class FileUtility extends AbstractUtility
9
{
10
    /**
11
     * Validate and convert a (JSON) file content to a PHP array.
12
     *
13
     * @param   $filePath
14
     *
15
     * @throws \Exception
16
     *
17
     * @return array|mixed
18
     */
19 23
    public function jsonFileToArray($filePath)
20
    {
21 23
        if (empty($filePath)) {
22 1
            throw new \Exception('File path is empty');
23
        }
24
25 22
        if (!file_exists($filePath) || !is_file($filePath)) {
26 1
            throw new \Exception('File does not exist or is not a file');
27
        }
28
29 21
        if (pathinfo($filePath, PATHINFO_EXTENSION) !== 'json') {
30 1
            throw new \Exception('File is not a json file');
31
        }
32
33 20
        $content = file_get_contents($filePath);
34
35 20
        return (new StringUtility())->jsonToArray($content);
36
    }
37
}
38