1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebPT\ZendCouchbaseModule; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
8
|
|
|
|
9
|
|
|
class AbstractCouchbaseBucketFactory implements AbstractFactoryInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Determine if we can create a service with name |
13
|
|
|
* |
14
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
15
|
|
|
* @param $name |
16
|
|
|
* @param $requestedName |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
1 |
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
20
|
|
|
{ |
21
|
1 |
|
return stripos($requestedName, 'couchbase.') === 0 && substr_count($requestedName, '.') === 2; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create service with name |
26
|
|
|
* |
27
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
28
|
|
|
* @param string $name |
29
|
|
|
* @param string $requestedName |
30
|
|
|
* @return \CouchbaseBucket |
31
|
|
|
* @throws \RuntimeException |
32
|
|
|
* @throws \Assert\AssertionFailedException |
33
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
34
|
|
|
*/ |
35
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
36
|
|
|
{ |
37
|
|
|
$components = explode('.', $requestedName); |
38
|
|
|
Assertion::count($components, 3); |
39
|
|
|
|
40
|
|
|
$moduleConfig = $this->getModuleConfig($serviceLocator); |
41
|
|
|
|
42
|
|
|
$clusterOptions = $this->getClusterOptions($moduleConfig, $components[1]); |
43
|
|
|
|
44
|
|
|
$bucketOptions = $this->getBucketOptions($moduleConfig, $components[2]); |
45
|
|
|
|
46
|
|
|
$cluster = $this->createCouchbaseCluster($clusterOptions); |
47
|
|
|
|
48
|
|
|
return $this->createCouchbaseBucket($cluster, $bucketOptions); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \CouchbaseCluster $cluster |
53
|
|
|
* @param BucketOptions $options |
54
|
|
|
* @return \CouchbaseBucket |
55
|
|
|
*/ |
56
|
|
|
public function createCouchbaseBucket(\CouchbaseCluster $cluster, BucketOptions $options) |
57
|
|
|
{ |
58
|
|
|
return $cluster->openBucket($options->getBucket(), $options->getPassword()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ClusterOptions $options |
63
|
|
|
* @return \CouchbaseCluster |
64
|
|
|
* @throws \RuntimeException |
65
|
|
|
*/ |
66
|
|
|
public function createCouchbaseCluster(ClusterOptions $options) |
67
|
|
|
{ |
68
|
|
|
if (class_exists('\Couchbase\Cluster', true)) { |
69
|
|
|
$class = '\Couchbase\Cluster'; |
70
|
|
|
return new $class($options->getDsn()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (class_exists('\CouchbaseCluster', true)) { |
74
|
|
|
return new \CouchbaseCluster($options->getDsn(), $options->getUsername(), $options->getPassword()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new \RuntimeException('Extension couchbase is required.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
82
|
|
|
* @return array |
83
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
84
|
|
|
* @throws \Assert\AssertionFailedException |
85
|
|
|
*/ |
86
|
3 |
|
public function getModuleConfig(ServiceLocatorInterface $serviceLocator) |
87
|
|
|
{ |
88
|
3 |
|
$config = $serviceLocator->get('config'); |
89
|
3 |
|
Assertion::isArray($config); |
90
|
|
|
|
91
|
3 |
|
Assertion::keyExists($config, 'webpt/zend-couchbase-module'); |
92
|
3 |
|
$moduleConfig = $config['webpt/zend-couchbase-module']; |
93
|
3 |
|
Assertion::isArray($moduleConfig); |
94
|
|
|
|
95
|
3 |
|
return $moduleConfig; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $moduleConfig |
100
|
|
|
* @param string $bucketConfigName |
101
|
|
|
* @return BucketOptions |
102
|
|
|
* @throws \Assert\AssertionFailedException |
103
|
|
|
*/ |
104
|
1 |
View Code Duplication |
public function getBucketOptions(array $moduleConfig, $bucketConfigName) |
|
|
|
|
105
|
|
|
{ |
106
|
1 |
|
Assertion::keyExists($moduleConfig, 'buckets'); |
107
|
|
|
|
108
|
1 |
|
$bucketsConfig = $moduleConfig['buckets']; |
109
|
1 |
|
Assertion::isArray($bucketsConfig); |
110
|
|
|
|
111
|
1 |
|
Assertion::keyExists($bucketsConfig, $bucketConfigName); |
112
|
1 |
|
$bucketConfig = $bucketsConfig[$bucketConfigName]; |
113
|
1 |
|
Assertion::isArray($bucketConfig); |
114
|
|
|
|
115
|
1 |
|
return new BucketOptions($bucketConfig); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $moduleConfig |
120
|
|
|
* @param string $clusterName |
121
|
|
|
* @return ClusterOptions |
122
|
|
|
* @throws \Assert\AssertionFailedException |
123
|
|
|
*/ |
124
|
1 |
View Code Duplication |
public function getClusterOptions(array $moduleConfig, $clusterName) |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
Assertion::keyExists($moduleConfig, 'clusters'); |
127
|
|
|
|
128
|
1 |
|
$clustersConfig = $moduleConfig['clusters']; |
129
|
1 |
|
Assertion::isArray($clustersConfig); |
130
|
|
|
|
131
|
1 |
|
Assertion::keyExists($clustersConfig, $clusterName); |
132
|
1 |
|
$clusterConfig = $clustersConfig[$clusterName]; |
133
|
1 |
|
Assertion::isArray($clusterConfig); |
134
|
|
|
|
135
|
1 |
|
return new ClusterOptions($clusterConfig); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.