Completed
Push — master ( eccdf7...f1de52 )
by Mario
02:37
created

JSONStorage   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 118
Duplicated Lines 12.71 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 15
loc 118
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * JsonStorage
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @category  Payment
8
 * @package   KlarnaAPI
9
 * @author    MS Dev <[email protected]>
10
 * @copyright 2012 Klarna AB (http://klarna.com)
11
 * @license   http://opensource.org/licenses/BSD-2-Clause BSD-2
12
 * @link      https://developers.klarna.com/
13
 */
14
15
/**
16
 * Include the {@link PCStorage} interface.
17
 */
18
require_once 'storage.intf.php';
19
20
/**
21
 * JSON storage class for KlarnaPClass
22
 *
23
 * This class is an JSON implementation of the PCStorage interface.
24
 *
25
 * @category  Payment
26
 * @package   KlarnaAPI
27
 * @author    MS Dev <[email protected]>
28
 * @copyright 2012 Klarna AB (http://klarna.com)
29
 * @license   http://opensource.org/licenses/BSD-2-Clause BSD-2
30
 * @link      https://developers.klarna.com/
31
 */
32
33
class JSONStorage extends PCStorage
34
{
35
36
37
    /**
38
     * return the name of the storage type
39
     *
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return "json";
45
    }
46
47
    /**
48
     * Checks if the file is writeable, readable or if the directory is.
49
     *
50
     * @param string $jsonFile json file that holds the pclasses
51
     *
52
     * @throws error
53
     * @return void
54
     */
55
    protected function checkURI($jsonFile)
56
    {
57
        //If file doesn't exist, check the directory.
58
        if (!file_exists($jsonFile)) {
59
            $jsonFile = dirname($jsonFile);
60
        }
61
62
        if (!is_writable($jsonFile)) {
63
            throw new Klarna_FileNotWritableException($jsonFile);
64
        }
65
66
        if (!is_readable($jsonFile)) {
67
            throw new Klarna_FileNotReadableException($jsonFile);
68
        }
69
    }
70
71
    /**
72
     * Clear the pclasses
73
     *
74
     * @param string $uri uri to file to clear
75
     *
76
     * @throws KlarnaException
77
     * @return void
78
     */
79
    public function clear($uri)
80
    {
81
        $this->checkURI($uri);
82
        unset($this->pclasses);
83
        if (file_exists($uri)) {
84
            unlink($uri);
85
        }
86
    }
87
88
    /**
89
     * Load pclasses from file
90
     *
91
     * @param string $uri uri to file to load
92
     *
93
     * @throws KlarnaException
94
     * @return void
95
     */
96
    public function load($uri)
97
    {
98
        $this->checkURI($uri);
99
        if (!file_exists($uri)) {
100
            //Do not fail, if file doesn't exist.
101
            return;
102
        }
103
        $arr = json_decode(file_get_contents($uri), true);
104
105
        if (count($arr) == 0) {
106
            return;
107
        }
108
109
        foreach ($arr as $pclasses) {
110
            if (count($pclasses) == 0) {
111
                continue;
112
            }
113
            foreach ($pclasses as $pclass) {
114
                $this->addPClass(new KlarnaPClass($pclass));
115
            }
116
        }
117
    }
118
119
    /**
120
     * Save pclasses to file
121
     *
122
     * @param string $uri uri to file to save
123
     *
124
     * @throws KlarnaException
125
     * @return void
126
     */
127
    public function save($uri)
128
    {
129
        try {
130
            $this->checkURI($uri);
131
132
            $output = array();
133
            foreach ($this->pclasses as $eid => $pclasses) {
134
                foreach ($pclasses as $pclass) {
135
                    if (!isset($output[$eid])) {
136
                        $output[$eid] = array();
137
                    }
138
                    $output[$eid][] = $pclass->toArray();
139
                }
140
            }
141
            if (count($this->pclasses) > 0) {
142
                file_put_contents($uri, json_encode($output));
143
            } else {
144
                file_put_contents($uri, "");
145
            }
146
        } catch(Exception $e) {
147
            throw new KlarnaException($e->getMessage());
148
        }
149
    }
150
}
151