Completed
Push — master ( 6a4e8f...29d5bf )
by
unknown
25:35 queued 21:57
created

ServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 62.96%

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 129
ccs 34
cts 54
cp 0.6296
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerContentfulRelated() 0 16 1
A register() 0 13 2
A provides() 0 12 1
A registerCommands() 0 39 1
A boot() 0 7 1
1
<?php
2
3
namespace Distilleries\Contentful;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
7
class ServiceProvider extends BaseServiceProvider
8
{
9
    /**
10
     * Package Laravel specific internal name.
11
     *
12
     * @var string
13
     */
14
    protected $package = 'contentful';
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function provides(): array
20
    {
21
        return [
22
            'command.contentful.model',
23
            'command.contentful.migration',
24
            'command.contentful.sync',
25
            'command.contentful.sync-data',
26
            'command.contentful.sync-flatten',
27
            'command.contentful.sync-locales',
28
            'command.contentful.import-clean',
29
            'command.contentful.import-publish',
30
            'contentful.rich-text.parser'
31
        ];
32
    }
33
34
    /**
35
     * Bootstrap any application services.
36
     *
37
     * @return void
38
     */
39 36
    public function boot()
40
    {
41 36
        $this->publishes([
42 36
            __DIR__ . '/../../config/config.php' => base_path('config/' . $this->package . '.php'),
43 36
        ], 'config');
44
45 36
        $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations/');
46
    }
47
48
    /**
49
     * Register any application services.
50
     *
51
     * @return void
52
     */
53 36
    public function register()
54
    {
55 36
        $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', $this->package);
56
57 36
        $this->app->bind(Api\DeliveryApi::class, Api\Delivery\Cached::class);
58 36
        $this->app->bind(Api\ManagementApi::class, Api\Management\Api::class);
59 36
        $this->app->bind(Api\SyncApi::class, Api\Sync\Api::class);
60 36
        $this->app->bind(Api\UploadApi::class, Api\Upload\Api::class);
61
62 36
        $this->registerContentfulRelated();
63
64 36
        if ($this->app->runningInConsole()) {
65 36
            $this->registerCommands();
66
        }
67
    }
68
69
    /**
70
     * Register Artisan commands.
71
     *
72
     * @return void
73
     */
74 36
    private function registerCommands()
75
    {
76
        $this->app->singleton('command.contentful.model', function () {
77
            return new Commands\Generators\Models(app(Api\ManagementApi::class));
78 36
        });
79
        $this->app->singleton('command.contentful.migration', function () {
80
            return new Commands\Generators\Migrations(app(Api\ManagementApi::class));
81 36
        });
82
        $this->app->singleton('command.contentful.sync', function () {
83
            return new Commands\Sync\Sync;
84 36
        });
85
        $this->app->singleton('command.contentful.sync-switch', function () {
86
            return new Commands\Sync\SyncSwitch;
87 36
        });
88
        $this->app->singleton('command.contentful.sync-data', function () {
89
            return new Commands\Sync\SyncData(app(Api\SyncApi::class));
90 36
        });
91
        $this->app->singleton('command.contentful.sync-flatten', function () {
92
            return new Commands\Sync\SyncFlatten;
93 36
        });
94
        $this->app->singleton('command.contentful.sync-locales', function () {
95
            return new Commands\Sync\SyncLocales(app(Api\ManagementApi::class));
96 36
        });
97
        $this->app->singleton('command.contentful.import-clean', function () {
98
            return new Commands\Import\ImportClean(app(Api\ManagementApi::class));
99 36
        });
100
        $this->app->singleton('command.contentful.import-publish', function () {
101
            return new Commands\Import\ImportPublish(app(Api\ManagementApi::class));
102 36
        });
103
104 36
        $this->commands('command.contentful.model');
105 36
        $this->commands('command.contentful.migration');
106 36
        $this->commands('command.contentful.sync');
107 36
        $this->commands('command.contentful.sync-switch');
108 36
        $this->commands('command.contentful.sync-data');
109 36
        $this->commands('command.contentful.sync-flatten');
110 36
        $this->commands('command.contentful.sync-locales');
111 36
        $this->commands('command.contentful.import-clean');
112 36
        $this->commands('command.contentful.import-publish');
113
    }
114
115
    /**
116
     * Bind utilities in IoC.
117
     *
118
     * @return void
119
     */
120 36
    private function registerContentfulRelated()
121
    {
122
        $this->app->singleton('contentful.rich-text.parser', function () {
123
            $spaceId = config('contentful.space_id');
124
            $environment = config('contentful.environment');
125
126
            $client = new \Contentful\Delivery\Client(config('contentful.tokens.delivery.live'), $spaceId, $environment);
127
            $linkResolver = new \Contentful\Delivery\LinkResolver(
128
                $client,
129
                new \Contentful\Delivery\ResourcePool\Extended(
130
                    $client,
131
                    new \Cache\Adapter\Void\VoidCachePool
132
                )
133
            );
134
135
            return new \Contentful\RichText\Parser($linkResolver);
136 36
        });
137
    }
138
}
139