commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / vendor / civicrm / civicrm-cxn-rpc / src / CxnStore / JsonFileCxnStore.php
1 <?php
2
3 /*
4 * This file is part of the civicrm-cxn-rpc package.
5 *
6 * Copyright (c) CiviCRM LLC <info@civicrm.org>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this package.
10 */
11
12 namespace Civi\Cxn\Rpc\CxnStore;
13
14 /**
15 * Class JsonFileCxnStore
16 *
17 * This is a very simple implementation. DO NOT USE IN PRODUCTION. It is not multithread safe.
18 *
19 * @package Civi\Cxn\Rpc\CxnStore
20 */
21 class JsonFileCxnStore implements CxnStoreInterface {
22
23 private $file;
24
25 private $cache;
26
27 public function __construct($file) {
28 $this->file = $file;
29 }
30
31 public function getCache() {
32 if (!$this->cache) {
33 $this->cache = $this->load();
34 }
35 return $this->cache;
36 }
37
38 public function getAll() {
39 return $this->getCache();
40 }
41
42 public function getByCxnId($cxnId) {
43 $cache = $this->getCache();
44 return isset($cache[$cxnId]) ? $cache[$cxnId] : NULL;
45 }
46
47 public function getByAppId($appId) {
48 $cache = $this->getCache();
49 foreach ($cache as $cxn) {
50 if ($cxn['appId'] == $appId) {
51 return $cxn;
52 }
53 }
54 return NULL;
55 }
56
57 public function add($cxn) {
58 $data = $this->load();
59 $data[$cxn['cxnId']] = $cxn;
60 $this->save($data);
61 }
62
63 public function remove($cxnId) {
64 $data = $this->load();
65 if (isset($data[$cxnId])) {
66 unset($data[$cxnId]);
67 $this->save($data);
68 }
69 }
70
71 /**
72 * @return mixed
73 */
74 public function load() {
75 return json_decode(file_get_contents($this->file), TRUE);
76 }
77
78 public function save($data) {
79 file_put_contents($this->file, json_encode($data));
80 }
81
82 }