Passed
Push — master ( d7f029...202469 )
by Jean-Christophe
15:08
created

MessagesUpdates::hasUpdates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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 1
	protected function getKey() {
28 1
		return md5 ( $this->locale . '.' . $this->domain );
29
	}
30
31 1
	public function __construct($locale, $domain) {
32 1
		$this->locale = $locale;
33 1
		$this->domain = $domain;
34 1
		$this->dirty = false;
35 1
	}
36
37 1
	public function load() {
38 1
		$key = $this->getKey ();
39 1
		if (CacheManager::$cache->exists ( $this->key . $key )) {
40 1
			$array = CacheManager::$cache->fetch ( $this->key . $key );
41
		}
42 1
		$this->newKeys = $array ['newKeys'] ?? [ ];
43 1
		$this->toAdd = $array ['toAdd'] ?? [ ];
44 1
		$this->toUpdate = $array ['toUpdate'] ?? [ ];
45 1
		$this->toDelete = $array ['toDelete'] ?? [ ];
46 1
	}
47
48 1
	public function exists() {
49 1
		$key = $this->getKey ();
50 1
		return CacheManager::$cache->exists ( $this->key . $key );
51
	}
52
53 1
	public function hasUpdates() {
54 1
		return sizeof ( $this->toUpdate ) > 0 || sizeof ( $this->toDelete ) > 0 || sizeof ( $this->toAdd ) > 0;
55
	}
56
57 1
	public function mergeMessages($messages, $beforeSave = false) {
58 1
		foreach ( $this->toDelete as $k ) {
59 1
			if (isset ( $messages [$k] )) {
60 1
				unset ( $messages [$k] );
61
			}
62
		}
63 1
		foreach ( $this->toUpdate as $k => $v ) {
64 1
			$messages [$k] = $v;
65
		}
66 1
		foreach ( $this->toAdd as $k => $v ) {
67 1
			if (isset ( $this->newKeys [$k] )) {
68 1
				if ($beforeSave) {
69
					$messages [$k] = $v;
70
				} else {
71 1
					$messages [$k] = [ $v,$this->newKeys [$k] ];
72
				}
73
			}
74
		}
75 1
		return $messages;
76
	}
77
78 1
	public function addToDelete($key) {
79 1
		$this->toDelete [] = $key;
80 1
		if (isset ( $this->toUpdate [$key] )) {
81
			unset ( $this->toUpdate [$key] );
82
		}
83 1
		$this->dirty = true;
84 1
	}
85
86 1
	public function updateValue($key, $value) {
87 1
		$this->toUpdate [$key] = $value;
88 1
		if (($index = array_search ( $key, $this->toDelete )) !== false) {
89
			unset ( $this->toDelete [$index] );
90
		}
91 1
		$this->dirty = true;
92 1
	}
93
94 1
	public function addValue($key, $value, $keyId = null) {
95 1
		$this->toAdd [$key] = $value;
96 1
		if (isset ( $keyId )) {
97 1
			if (($id = array_search ( $keyId, $this->newKeys )) !== false) {
98
				unset ( $this->toAdd [$id] );
99
				unset ( $this->newKeys [$id] );
100
			}
101 1
			$this->newKeys [$key] = $keyId;
102
		}
103
104 1
		$this->dirty = true;
105 1
	}
106
107
	public function removeAddValue($key) {
108
		if (isset ( $this->toAdd [$key] )) {
109
			unset ( $this->toAdd [$key] );
110
		}
111
	}
112
113 1
	public function replaceKey($key, $newKey, $value) {
114 1
		$this->addToDelete ( $key );
115 1
		$this->updateValue ( $newKey, $value );
116 1
	}
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 1
	public function save() {
129 1
		if ($this->dirty) {
130 1
			$key = $this->getKey ();
131 1
			CacheManager::$cache->store ( $this->key . $key, [ 'newKeys' => $this->newKeys,'toAdd' => $this->toAdd,'toUpdate' => $this->toUpdate,'toDelete' => $this->toDelete ] );
0 ignored issues
show
Bug introduced by
array('newKeys' => $this...te' => $this->toDelete) of type array is incompatible with the type string expected by parameter $code of Ubiquity\cache\system\AbstractDataCache::store(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
			CacheManager::$cache->store ( $this->key . $key, /** @scrutinizer ignore-type */ [ 'newKeys' => $this->newKeys,'toAdd' => $this->toAdd,'toUpdate' => $this->toUpdate,'toDelete' => $this->toDelete ] );
Loading history...
132 1
			$this->dirty = false;
133 1
			return true;
134
		}
135
		return false;
136
	}
137
138 1
	public function delete() {
139 1
		$key = $this->getKey ();
140 1
		CacheManager::$cache->remove ( $this->key . $key );
141 1
	}
142
143
	public function isDirty() {
144
		return $this->dirty;
145
	}
146
147 1
	public function __toString() {
148 1
		$res = [ ];
149 1
		if (($nb = \count ( $this->toAdd )) > 0) {
150 1
			$res [] = '+' . $nb;
151
		}
152 1
		if (($nb = \count ( $this->toUpdate )) > 0) {
153 1
			$res [] = '±' . $nb;
154
		}
155 1
		if (($nb = \count ( $this->toDelete )) > 0) {
156 1
			$res [] = '-' . $nb;
157
		}
158 1
		return \implode ( ', ', $res );
159
	}
160
}
161
162