Passed
Push — master ( d73925...4edc68 )
by Timothy
08:08
created

AbstractCouchbaseBucketFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 20.16 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.51%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 26
loc 129
ccs 25
cts 42
cp 0.5951
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 0 4 2
A createServiceWithName() 0 15 1
A createCouchbaseBucket() 0 4 1
A createCouchbaseCluster() 0 13 3
A getModuleConfig() 0 11 1
A getBucketOptions() 13 13 1
A getClusterOptions() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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