StoryblokServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
eloc 15
c 7
b 0
f 2
dl 0
loc 21
rs 9.7666
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Riclep\Storyblok;
4
5
use Illuminate\Support\ServiceProvider;
6
use Riclep\Storyblok\Console\BlockMakeCommand;
7
use Riclep\Storyblok\Console\BlockSyncCommand;
8
use Riclep\Storyblok\Console\FolderMakeCommand;
9
use Riclep\Storyblok\Console\StubViewsCommand;
10
use Storyblok\Client;
11
12
class StoryblokServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17
    public function boot(): void
18
    {
19
		$this->loadRoutesFrom(__DIR__.'/routes/api.php');
20
21
		$this->loadViewsFrom(__DIR__.'/resources/views', 'laravel-storyblok');
22
23
        if ($this->app->runningInConsole()) {
24
			$this->publishes([
25
				__DIR__.'/../config/storyblok.php' => config_path('storyblok.php'),
26
				__DIR__ . '/../stubs/Page.stub' => app_path('Storyblok') . '/Page.php',
27
				__DIR__ . '/../stubs/Block.stub' => app_path('Storyblok') . '/Block.php',
28
				__DIR__ . '/../stubs/Asset.stub' => app_path('Storyblok') . '/Asset.php',
29
				__DIR__ . '/../stubs/Folder.stub' => app_path('Storyblok') . '/Folder.php',
30
			], 'storyblok');
31
        }
32
33
		$this->commands([
34
			BlockMakeCommand::class,
35
			BlockSyncCommand::class,
36
			FolderMakeCommand::class,
37
			StubViewsCommand::class
38
		]);
39
    }
40
41
    /**
42
     * Register the application services.
43
     */
44
    public function register(): void
45
    {
46
        // Automatically apply the package configuration
47
        $this->mergeConfigFrom(__DIR__.'/../config/storyblok.php', 'storyblok');
48
49
        // Register the main class to use with the facade
50
		$this->app->singleton('storyblok', function () {
51
			return new Storyblok;
52
		});
53
54
		////////////TODO should this be a middleware?
55
	    $storyblokRequest = request()->get('_storyblok_tk');
56
		if (!empty($storyblokRequest)) {
57
			$pre_token = $storyblokRequest['space_id'] . ':' . config('storyblok.api_preview_key') . ':' . $storyblokRequest['timestamp'];
58
			$token = sha1($pre_token);
59
			if ($token == $storyblokRequest['token'] && (int)$storyblokRequest['timestamp'] > strtotime('now') - 3600) {
60
				config(['storyblok.edit_mode' => true]);
61
				config(['storyblok.draft' => true]);
62
			}
63
		}
64
65
	    // register the Storyblok client, checking if we are in edit more of the dev requests draft content
66
	    $client = new Client(
67
			config('storyblok.draft') ? config('storyblok.api_preview_key') : config('storyblok.api_public_key'),
68
			"api.storyblok.com", "v2", config('storyblok.use_ssl'), config('storyblok.api_region')
69
	    );
70
71
	    // if we’re in Storyblok’s edit mode let’s save that in the config for easy access
72
	    $client->editMode(config('storyblok.draft'));
73
74
	    // This singleton allows to retrieve the driver set has default from the manager
75
	    $this->app->singleton('image-transformer.driver', function ($app) {
76
		    return $app['image-transformer']->driver();
77
	    });
78
79
		$this->app->instance('Storyblok\Client', $client);
80
    }
81
}
82