Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

FileUtility::jsonFileToArray()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 1
crap 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 15
    public function jsonFileToArray($filePath)
20
    {
21 15
        if (empty($filePath)) {
22 1
            throw new \Exception('File path is empty');
23
        }
24
25 14
        if (!file_exists($filePath) || !is_file($filePath)) {
26 1
            throw new \Exception('File does not exist or is not a file');
27
        }
28
29 13
        if (pathinfo($filePath, PATHINFO_EXTENSION) !== 'json') {
30 1
            throw new \Exception('File is not a json file');
31
        }
32
33 12
        $content = file_get_contents($filePath);
34
35 12
        return (new StringUtility())->jsonToArray($content);
36
    }
37
}
38