JsonFile::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Modera\UpgradeBundle\Json;
4
5
/**
6
 * @author    Sergei Vizel <[email protected]>
7
 * @copyright 2014 Modera Foundation
8
 */
9
class JsonFile
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @param string $path
18
     */
19
    public function __construct($path)
20
    {
21
        $this->path = $path;
22
    }
23
24
    /**
25
     * Reads json file.
26
     *
27
     * @throws \RuntimeException
28
     *
29
     * @return mixed
30
     */
31
    public function read()
32
    {
33
        $json = @file_get_contents($this->path);
34
35
        if (false === $json) {
36
            throw new \RuntimeException(
37
                'Could not read '.$this->path
38
            );
39
        }
40
41
        $arr = json_decode($json, true);
42
43
        $error = null;
44
        switch (json_last_error()) {
45
            case JSON_ERROR_NONE:
46
                break;
47
            case JSON_ERROR_DEPTH:
48
                $error = 'Maximum stack depth exceeded.';
49
                break;
50
            case JSON_ERROR_STATE_MISMATCH:
51
                $error = 'Underflow or the modes mismatch.';
52
                break;
53
            case JSON_ERROR_CTRL_CHAR:
54
                $error = 'Unexpected control character found.';
55
                break;
56
            case JSON_ERROR_SYNTAX:
57
                $error = 'Syntax error, malformed JSON.';
58
                break;
59
            case JSON_ERROR_UTF8:
60
                $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
61
                break;
62
            default:
63
                $error = 'Unknown error';
64
                break;
65
        }
66
67
        if ($error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68
            throw new \RuntimeException(
69
                $error.' Path: '.$this->path
70
            );
71
        }
72
73
        return $arr;
74
    }
75
76
    /**
77
     * Writes json file.
78
     *
79
     * @param array $hash    writes hash into json file
80
     * @param int   $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
81
     *
82
     * @throws \RuntimeException
83
     */
84
    public function write(array $hash, $options = 448)
85
    {
86
        if (!file_exists(dirname($this->path))) {
87
            throw new \RuntimeException(
88
                'Could not write '.$this->path
89
            );
90
        }
91
92
        $encode = json_encode($hash, $options);
93
        file_put_contents($this->path, $encode.($options & JSON_PRETTY_PRINT ? "\n" : ''));
94
    }
95
}
96