1616namespace Phpfastcache \Drivers \Memcache ;
1717
1818use Phpfastcache \Config \ConfigurationOption ;
19+ use Phpfastcache \Exceptions \PhpfastcacheDriverException ;
1920use Phpfastcache \Exceptions \PhpfastcacheInvalidConfigurationException ;
2021use Phpfastcache \Exceptions \PhpfastcacheLogicException ;
2122
@@ -27,10 +28,10 @@ class Config extends ConfigurationOption
2728 * Multiple server can be added this way:
2829 * $cfg->setServers([
2930 * [
31+ * // If you use an UNIX socket set the host and port to null
3032 * 'host' => '127.0.0.1',
33+ * //'path' => 'path/to/unix/socket',
3134 * 'port' => 11211,
32- * 'saslUser' => false,
33- * 'saslPassword' => false,
3435 * ]
3536 * ]);
3637 */
@@ -40,49 +41,6 @@ class Config extends ConfigurationOption
4041
4142 protected int $ port = 11211 ;
4243
43- protected string $ saslUser = '' ;
44-
45- protected string $ saslPassword = '' ;
46-
47- /**
48- * @return string
49- */
50- public function getSaslUser (): string
51- {
52- return $ this ->saslUser ;
53- }
54-
55- /**
56- * @param string $saslUser
57- * @return self
58- * @throws PhpfastcacheLogicException
59- */
60- public function setSaslUser (string $ saslUser ): static
61- {
62- $ this ->enforceLockedProperty (__FUNCTION__ );
63- $ this ->saslUser = $ saslUser ;
64- return $ this ;
65- }
66-
67- /**
68- * @return string
69- */
70- public function getSaslPassword (): string
71- {
72- return $ this ->saslPassword ;
73- }
74-
75- /**
76- * @param string $saslPassword
77- * @return self
78- * @throws PhpfastcacheLogicException
79- */
80- public function setSaslPassword (string $ saslPassword ): static
81- {
82- $ this ->enforceLockedProperty (__FUNCTION__ );
83- $ this ->saslPassword = $ saslPassword ;
84- return $ this ;
85- }
8644
8745 /**
8846 * @return array
@@ -95,8 +53,6 @@ public function getServers(): array
9553 'host ' => $ this ->getHost (),
9654 'path ' => $ this ->getPath (),
9755 'port ' => $ this ->getPort (),
98- 'saslUser ' => $ this ->getSaslUser () ?: false ,
99- 'saslPassword ' => $ this ->getSaslPassword () ?: false ,
10056 ],
10157 ];
10258 }
@@ -109,23 +65,41 @@ public function getServers(): array
10965 * @return self
11066 * @throws PhpfastcacheInvalidConfigurationException
11167 * @throws PhpfastcacheLogicException
68+ *
69+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
70+ * @SuppressWarnings(PHPMD.NPathComplexity)
11271 */
11372 public function setServers (array $ servers ): static
11473 {
11574 $ this ->enforceLockedProperty (__FUNCTION__ );
11675 foreach ($ servers as $ server ) {
117- if ($ diff = array_diff ([ ' host ' , ' port ' , ' saslUser ' , 'saslPassword ' ], array_keys ( $ server) )) {
118- throw new PhpfastcacheInvalidConfigurationException ('Missing keys for memcached server: ' . implode ( ' , ' , $ diff ) );
76+ if (\array_key_exists ( ' saslUser ' , $ server ) || array_key_exists ( 'saslPassword ' , $ server )) {
77+ throw new PhpfastcacheInvalidConfigurationException ('Unlike Memcached, Memcache does not support SASL authentication ' );
11978 }
120- if ($ diff = array_diff (array_keys ($ server ), ['host ' , 'port ' , 'saslUser ' , 'saslPassword ' ])) {
79+
80+ if ($ diff = array_diff (array_keys ($ server ), ['host ' , 'port ' , 'path ' ])) {
12181 throw new PhpfastcacheInvalidConfigurationException ('Unknown keys for memcached server: ' . implode (', ' , $ diff ));
12282 }
123- if (!is_string ($ server ['host ' ])) {
124- throw new PhpfastcacheInvalidConfigurationException ('Host must be a valid string in "$server" configuration array ' );
83+
84+ if (!empty ($ server ['host ' ]) && !empty ($ server ['path ' ])) {
85+ throw new PhpfastcacheInvalidConfigurationException ('Host and path cannot be simultaneous defined. ' );
86+ }
87+
88+ if ((isset ($ server ['host ' ]) && !is_string ($ server ['host ' ])) || (empty ($ server ['path ' ]) && empty ($ server ['host ' ]))) {
89+ throw new PhpfastcacheInvalidConfigurationException ('Host must be a valid string in "$server" configuration array if path is not defined ' );
90+ }
91+
92+ if ((isset ($ server ['path ' ]) && !is_string ($ server ['path ' ])) || (empty ($ server ['host ' ]) && empty ($ server ['path ' ]))) {
93+ throw new PhpfastcacheInvalidConfigurationException ('Path must be a valid string in "$server" configuration array if host is not defined ' );
12594 }
126- if (!is_int ($ server ['port ' ])) {
95+
96+ if (!empty ($ server ['host ' ]) && (empty ($ server ['port ' ]) || !is_int ($ server ['port ' ])|| $ server ['port ' ] < 1 )) {
12797 throw new PhpfastcacheInvalidConfigurationException ('Port must be a valid integer in "$server" configuration array ' );
12898 }
99+
100+ if (!empty ($ server ['port ' ]) && !empty ($ server ['path ' ])) {
101+ throw new PhpfastcacheInvalidConfigurationException ('Port should not be defined along with path ' );
102+ }
129103 }
130104 $ this ->servers = $ servers ;
131105 return $ this ;
@@ -148,6 +122,7 @@ public function setHost(string $host): static
148122 {
149123 $ this ->enforceLockedProperty (__FUNCTION__ );
150124 $ this ->host = $ host ;
125+
151126 return $ this ;
152127 }
153128
0 commit comments