Passed
Push — master ( 7d615b...d4db62 )
by Jean-Christophe
09:33
created

MessagesUpdates::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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