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 |
||
| 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 |