Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

MessagesUpdates::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\translation;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\utils\base\UArray;
7
8
/**
9
 * Store translation updates.
10
 * Ubiquity\translation$MessagesUpdates
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.0
15
 * @since Ubiquity 2.1.4
16
 *
17
 */
18
class MessagesUpdates {
19
	protected $locale;
20
	protected $domain;
21
	protected $key = "tmp/translations/";
22
	protected $toUpdate;
23
	protected $toAdd;
24
	protected $toDelete;
25
	protected $dirty;
26
	protected $newKeys;
27
28 1
	protected function getKey() {
29 1
		return md5 ( $this->locale . '.' . $this->domain );
30
	}
31
32 1
	public function __construct($locale, $domain) {
33 1
		$this->locale = $locale;
34 1
		$this->domain = $domain;
35 1
		$this->dirty = false;
36 1
	}
37
38 1
	public function load() {
39 1
		$key = $this->getKey ();
40 1
		if (CacheManager::$cache->exists ( $this->key . $key )) {
41 1
			$array = CacheManager::$cache->fetch ( $this->key . $key );
42
		}
43 1
		$this->newKeys = $array ['newKeys'] ?? [ ];
44 1
		$this->toAdd = $array ['toAdd'] ?? [ ];
45 1
		$this->toUpdate = $array ['toUpdate'] ?? [ ];
46 1
		$this->toDelete = $array ['toDelete'] ?? [ ];
47 1
	}
48
49 1
	public function exists() {
50 1
		$key = $this->getKey ();
51 1
		return CacheManager::$cache->exists ( $this->key . $key );
52
	}
53
54 1
	public function hasUpdates() {
55 1
		return sizeof ( $this->toUpdate ) > 0 || sizeof ( $this->toDelete ) > 0 || sizeof ( $this->toAdd ) > 0;
56
	}
57
58 1
	public function mergeMessages($messages, $beforeSave = false) {
59 1
		foreach ( $this->toDelete as $k ) {
60 1
			if (isset ( $messages [$k] )) {
61 1
				unset ( $messages [$k] );
62
			}
63
		}
64 1
		foreach ( $this->toUpdate as $k => $v ) {
65 1
			$messages [$k] = $v;
66
		}
67 1
		foreach ( $this->toAdd as $k => $v ) {
68 1
			if (isset ( $this->newKeys [$k] )) {
69 1
				if ($beforeSave) {
70
					$messages [$k] = $v;
71
				} else {
72 1
					$messages [$k] = [ $v,$this->newKeys [$k] ];
73
				}
74
			}
75
		}
76 1
		return $messages;
77
	}
78
79 1
	public function addToDelete($key) {
80 1
		$this->toDelete [] = $key;
81 1
		if (isset ( $this->toUpdate [$key] )) {
82
			unset ( $this->toUpdate [$key] );
83
		}
84 1
		$this->dirty = true;
85 1
	}
86
87 1
	public function updateValue($key, $value) {
88 1
		$this->toUpdate [$key] = $value;
89 1
		if (($index = array_search ( $key, $this->toDelete )) !== false) {
90
			unset ( $this->toDelete [$index] );
91
		}
92 1
		$this->dirty = true;
93 1
	}
94
95 1
	public function addValue($key, $value, $keyId = null) {
96 1
		$this->toAdd [$key] = $value;
97 1
		if (isset ( $keyId )) {
98 1
			if (($id = array_search ( $keyId, $this->newKeys )) !== false) {
99
				unset ( $this->toAdd [$id] );
100
				unset ( $this->newKeys [$id] );
101
			}
102 1
			$this->newKeys [$key] = $keyId;
103
		}
104
105 1
		$this->dirty = true;
106 1
	}
107
108
	public function removeAddValue($key) {
109
		if (isset ( $this->toAdd [$key] )) {
110
			unset ( $this->toAdd [$key] );
111
		}
112
	}
113
114 1
	public function replaceKey($key, $newKey, $value) {
115 1
		$this->addToDelete ( $key );
116 1
		$this->updateValue ( $newKey, $value );
117 1
	}
118
119
	public function removeNewKey($key) {
120
		if (($k = array_search ( $key, $this->newKeys )) !== false) {
121
			if (isset ( $this->toAdd [$k] )) {
122
				unset ( $this->toAdd [$k] );
123
			}
124
			unset ( $this->newKeys [$k] );
125
			$this->dirty = true;
126
		}
127
	}
128
129 1
	public function save() {
130 1
		if ($this->dirty) {
131 1
			$key = $this->getKey ();
132 1
			CacheManager::$cache->store ( $this->key . $key, 'return array' . UArray::asPhpArray ( [ 'newKeys' => $this->newKeys,'toAdd' => $this->toAdd,'toUpdate' => $this->toUpdate,'toDelete' => $this->toDelete ] ) . ';' );
133 1
			$this->dirty = false;
134 1
			return true;
135
		}
136
		return false;
137
	}
138
139 1
	public function delete() {
140 1
		$key = $this->getKey ();
141 1
		CacheManager::$cache->remove ( $this->key . $key );
142 1
	}
143
144
	public function isDirty() {
145
		return $this->dirty;
146
	}
147
148 1
	public function __toString() {
149 1
		$res = [ ];
150 1
		if (($nb = sizeof ( $this->toAdd )) > 0) {
151 1
			$res [] = '+' . $nb;
152
		}
153 1
		if (($nb = sizeof ( $this->toUpdate )) > 0) {
154 1
			$res [] = '±' . $nb;
155
		}
156 1
		if (($nb = sizeof ( $this->toDelete )) > 0) {
157 1
			$res [] = '-' . $nb;
158
		}
159 1
		return implode ( ', ', $res );
160
	}
161
}
162
163