Completed
Push — master ( acf9c2...00722f )
by Aimeos
10:24
created

APC::dn()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 3
nop 4
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 * @package MW
8
 * @subpackage Translation
9
 */
10
11
12
namespace Aimeos\MW\Translation\Decorator;
13
14
15
/**
16
 * APC caching decorator for translation classes.
17
 *
18
 * @package MW
19
 * @subpackage Translation
20
 */
21
class APC
22
	extends \Aimeos\MW\Translation\Decorator\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MW\Translation\Decorator\Iface
24
{
25
	private $enable = false;
26
	private $prefix;
27
28
29
	/**
30
	 * Initializes the decorator.
31
	 *
32
	 * @param \Aimeos\MW\Translation\Iface $object Translation object or decorator
33
	 * @param string $prefix Prefix for keys to distinguish several instances
34
	 */
35
	public function __construct( \Aimeos\MW\Translation\Iface $object, $prefix = '' )
36
	{
37
		parent::__construct( $object );
38
39
		if( function_exists( 'apcu_store' ) === true )
40
		{
41
			$this->enable = true;
42
			$this->prefix = $prefix;
43
		}
44
	}
45
46
47
	/**
48
	 * Returns the translated string.
49
	 *
50
	 * @param string $domain Translation domain
51
	 * @param string $string String to be translated
52
	 * @return string The translated string
53
	 */
54
	public function dt( $domain, $string )
55
	{
56
		if( $this->enable === false ) {
57
			return parent::dt( $domain, $string );
58
		}
59
60
		$key = $this->prefix . $domain . '|' . $this->getLocale() . '|' . $string;
61
62
		// regular cache
63
		$success = false;
64
		$value = apcu_fetch( $key, $success );
65
66
		if( $success === true ) {
67
			return $value;
68
		}
69
70
		// not cached
71
		$value = parent::dt( $domain, $string );
72
73
		apcu_store( $key, $value );
74
75
		return $value;
76
	}
77
78
79
	/**
80
	 * Returns the translated string by the given plural and quantity.
81
	 *
82
	 * @param string $domain Translation domain
83
	 * @param string $singular String in singular form
84
	 * @param string $plural String in plural form
85
	 * @param integer $number Quantity to chose the correct plural form for languages with plural forms
86
	 * @return string Returns the translated singular or plural form of the string depending on the given number.
87
	 */
88
	public function dn( $domain, $singular, $plural, $number )
89
	{
90
		if( $this->enable === false ) {
91
			return parent::dn( $domain, $singular, $plural, $number );
92
		}
93
94
		$locale = $this->getLocale();
95
		$index = $this->getPluralIndex( $number, $locale );
96
		$key = $this->prefix . $domain . '|' . $locale . '|' . $singular . '|' . $index;
97
98
		// regular cache
99
		$success = false;
100
		$value = apcu_fetch( $key, $success );
101
102
		if( $success === true ) {
103
			return $value;
104
		}
105
106
		// not cached
107
		$value = parent::dn( $domain, $singular, $plural, $number );
108
109
		apcu_store( $key, $value );
110
111
		return $value;
112
	}
113
}
114