Utils::readJsonFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm;
7
8
use Symfony\Component\Filesystem\Filesystem;
9
use Slince\Crm\Exception\InvalidArgumentException;
10
11
class Utils
12
{
13
    /**
14
     * @var Filesystem
15
     */
16
    protected static $filesystem;
17
18
    /**
19
     * @return Filesystem
20
     */
21
    public static function getFilesystem()
22
    {
23
        if (is_null(static::$filesystem)) {
24
            static::$filesystem = new Filesystem();
25
        }
26
        return static::$filesystem ;
27
    }
28
29
    /**
30
     * read json file data
31
     * @param $file
32
     * @return mixed
33
     * @throws \Exception
34
     */
35
    public static function readJsonFile($file)
36
    {
37
        if (!is_file($file)) {
38
            throw new InvalidArgumentException(sprintf("File [%s] does not exists", $file));
39
        }
40
        $rawContent = @file_get_contents($file);
41
        $data = json_decode($rawContent, true);
42
        if (json_last_error()) {
43
            throw new InvalidArgumentException(sprintf("File [%s] must contain valid json, error: %s", $file, json_last_error_msg()));
44
        }
45
        return $data;
46
    }
47
}
48