Completed
Branch master (07905b)
by yuuki
01:56
created

CouchbaseServiceProvider::compiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.4285
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase;
14
15
use Illuminate\Cache\Repository;
16
use Illuminate\Queue\QueueManager;
17
use Illuminate\Support\ServiceProvider;
18
use Illuminate\Session\CacheBasedSessionHandler;
19
use Ytake\LaravelCouchbase\Database\Connectable;
20
use Ytake\LaravelCouchbase\Cache\LegacyCouchbaseStore;
21
use Ytake\LaravelCouchbase\Cache\MemcachedBucketStore;
22
use Ytake\LaravelCouchbase\Database\CouchbaseConnector;
23
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
24
25
/**
26
 * Class CouchbaseServiceProvider.
27
 *
28
 * @codeCoverageIgnore
29
 *
30
 * @author Yuuki Takezawa<[email protected]>
31
 */
32
class CouchbaseServiceProvider extends ServiceProvider
33
{
34
    /**
35
     * Bootstrap  application services.
36
     */
37
    public function boot()
38
    {
39
        $this->registerCouchbaseBucketCacheDriver();
40
        $this->registerMemcachedBucketCacheDriver();
41
        $this->registerCouchbaseQueueDriver();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function register()
48
    {
49
        $this->app->singleton(Connectable::class, function () {
50
            return new CouchbaseConnector();
51
        });
52
53
        $this->app->singleton('couchbase.memcached.connector', function () {
54
            return new MemcachedConnector();
55
        });
56
57
        // add couchbase session driver
58 View Code Duplication
        $this->app['session']->extend('couchbase', function ($app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
            $minutes = $app['config']['session.lifetime'];
60
61
            return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase'), $minutes);
62
        });
63
64
        // add couchbase session driver
65 View Code Duplication
        $this->app['session']->extend('couchbase-memcached', function ($app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            $minutes = $app['config']['session.lifetime'];
67
68
            return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase-memcached'), $minutes);
69
        });
70
71
        // add couchbase extension
72
        $this->app['db']->extend('couchbase', function ($config, $name) {
73
            return new CouchbaseConnection($config, $name);
74
        });
75
    }
76
77
    /**
78
     * register 'couchbase' cache driver.
79
     * for bucket type couchbase.
80
     */
81
    protected function registerCouchbaseBucketCacheDriver()
82
    {
83
        $this->app['cache']->extend('couchbase', function ($app, $config) {
84
            /** @var \CouchbaseCluster $cluster */
85
            $cluster = $app['db']->connection($config['driver'])->getCouchbase();
86
            $password = (isset($config['bucket_password'])) ? $config['bucket_password'] : '';
87
            return new Repository(
88
                new LegacyCouchbaseStore(
89
                    $cluster,
90
                    $config['bucket'],
91
                    $password,
92
                    $app['config']->get('cache.prefix'))
93
            );
94
        });
95
    }
96
97
    /**
98
     * register 'couchbase' cache driver.
99
     * for bucket type memcached.
100
     */
101
    protected function registerMemcachedBucketCacheDriver()
102
    {
103
        $this->app['cache']->extend('couchbase-memcached', function ($app, $config) {
104
            $prefix = $app['config']['cache.prefix'];
105
            $memcachedBucket = $this->app['couchbase.memcached.connector']->connect($config['servers']);
106
107
            return new Repository(
108
                new MemcachedBucketStore($memcachedBucket, $prefix, $config['servers'])
109
            );
110
        });
111
    }
112
113
    protected function registerCouchbaseQueueDriver()
114
    {
115
        /** @var QueueManager $queueManager */
116
        $queueManager = $this->app['queue'];
117
        $queueManager->addConnector('couchbase', function () {
118
            return new \Ytake\LaravelCouchbase\Queue\CouchbaseConnector($this->app['db']);
119
        });
120
    }
121
}
122