Completed
Pull Request — master (#20)
by yuuki
07:30
created

CouchbaseServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\Support\ServiceProvider;
17
use Illuminate\Session\CacheBasedSessionHandler;
18
use Ytake\LaravelCouchbase\Database\Connectable;
19
use Ytake\LaravelCouchbase\Cache\CouchbaseStore;
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
 * @author Yuuki Takezawa<[email protected]>
29
 */
30
class CouchbaseServiceProvider extends ServiceProvider
31
{
32
    /**
33
     * Bootstrap  application services.
34
     */
35
    public function boot()
36
    {
37
        $this->registerCouchbaseBucketCacheDriver();
38
        $this->registerMemcachedBucketCacheDriver();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function register()
45
    {
46
        $this->app->singleton(Connectable::class, function () {
47
            return new CouchbaseConnector();
48
        });
49
50
        $this->app->singleton('couchbase.memcached.connector', function () {
51
            return new MemcachedConnector();
52
        });
53
54
        // add couchbase session driver
55 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...
56
            $minutes = $app['config']['session.lifetime'];
57
58
            return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase'), $minutes);
59
        });
60
61
        // add couchbase session driver
62 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...
63
            $minutes = $app['config']['session.lifetime'];
64
65
            return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase-memcached'), $minutes);
66
        });
67
68
        // add couchbase extension
69
        $this->app['db']->extend('couchbase', function ($config) {
70
            /* @var \CouchbaseCluster $cluster */
71
            return new CouchbaseConnection($config);
72
        });
73
    }
74
75
    /**
76
     * register 'couchbase' cache driver.
77
     * for bucket type couchbase.
78
     */
79
    protected function registerCouchbaseBucketCacheDriver()
80
    {
81
        $this->app['cache']->extend('couchbase', function ($app, $config) {
82
            /** @var \CouchbaseCluster $cluster */
83
            $cluster = $app['db']->connection($config['driver'])->getCouchbase();
84
            $password = (isset($config['bucket_password'])) ? $config['bucket_password'] : '';
85
            if (floatval($this->app->version()) <= 5.1) {
86
                return new Repository(
87
                    new LegacyCouchbaseStore(
88
                        $cluster,
89
                        $config['bucket'],
90
                        $password,
91
                        $app['config']->get('cache.prefix'))
92
                );
93
            }
94
95
            return new Repository(
96
                new CouchbaseStore(
97
                    $cluster,
98
                    $config['bucket'],
99
                    $password,
100
                    $app['config']->get('cache.prefix')
101
                )
102
            );
103
        });
104
    }
105
106
    /**
107
     * register 'couchbase' cache driver.
108
     * for bucket type memcached.
109
     */
110
    protected function registerMemcachedBucketCacheDriver()
111
    {
112
        $this->app['cache']->extend('couchbase-memcached', function ($app, $config) {
113
            $prefix = $app['config']['cache.prefix'];
114
            $memcachedBucket = $this->app['couchbase.memcached.connector']->connect($config['servers']);
115
116
            return new Repository(
117
                new MemcachedBucketStore($memcachedBucket, $prefix, $config['servers'])
118
            );
119
        });
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     *
125
     * @codeCoverageIgnore
126
     */
127
    public static function compiles()
128
    {
129
        return [
130
            base_path().'/vendor/ytake/laravel-couchbase/src/Cache/CouchbaseStore.php',
131
            base_path().'/vendor/ytake/laravel-couchbase/src/Cache/MemcachedBucketStore.php',
132
            base_path().'/vendor/ytake/laravel-couchbase/src/Database/CouchbaseConnection.php',
133
            base_path().'/vendor/ytake/laravel-couchbase/src/Database/CouchbaseConnector.php',
134
            base_path().'/vendor/ytake/laravel-couchbase/src/Exceptions/FlushException.php',
135
            base_path().'/vendor/ytake/laravel-couchbase/src/Exceptions/NotSupportedException.php',
136
            base_path().'/vendor/ytake/laravel-couchbase/src/Query/Grammer.php',
137
            base_path().'/vendor/ytake/laravel-couchbase/src/Query/Processor.php',
138
            base_path().'/vendor/ytake/laravel-couchbase/src/CouchbaseServiceProvider.php',
139
        ];
140
    }
141
}
142