Completed
Push — master ( f7d81c...d64534 )
by Jimmy
06:11 queued 02:27
created

ClientServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Spinen\Formio\Providers;
4
5
use Carbon\Carbon;
6
use GuzzleHttp\Client as Guzzle;
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Contracts\Support\DeferrableProvider;
9
use Illuminate\Support\Facades\Cache;
10
use Illuminate\Support\Facades\Config;
11
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
12
use Spinen\Formio\Client as Formio;
13
14
/**
15
 * Class ClientServiceProvider
16
 *
17
 * Since this is deferred, it only needed to deal with code that has to do with the client.
18
 *
19
 * @package Spinen\Formio\Providers
20
 */
21
class ClientServiceProvider extends LaravelServiceProvider implements DeferrableProvider
22
{
23
    /**
24
     * Bootstrap services.
25
     *
26
     * @return void
27
     */
28
    public function boot()
29
    {
30
        //
31
    }
32
33
    /**
34
     * Register services.
35
     *
36
     * @return void
37
     */
38
    public function register()
39
    {
40
        $this->registerClient();
41
42
        $this->app->alias(Formio::class, 'Formio');
43
    }
44
45
    /**
46
     * Get the services provided by the provider.
47
     *
48
     * @return array
49
     */
50
    public function provides()
51
    {
52
        return [
53
            Formio::class,
54
        ];
55
    }
56
57
    /**
58
     * Register the client
59
     *
60
     * If the Formio id or roles are null, then assume sensible values via the API
61
     */
62
    protected function registerClient(): void
63
    {
64
        $this->app->bind(
65
            Formio::class,
66
            function (Application $app) {
67
                $formio = new Formio(Config::get('formio'), $app->make(Guzzle::class));
68
69
                $resourceIds = function () use ($formio) {
70
                    $id = $formio->login()
71
                                 ->request('form?name=user')[0]['_id'];
72
73
                    $formio->logout();
74
75
                    return $id;
76
                };
77
78
                // If the formio id is null, then get it or a cached value for the user resource
79
                if (empty(Config::get('formio.user.form'))) {
80
                    Config::set(
81
                        'formio.user.form',
82
                        Cache::remember(
83
                            'formio.id',
84
                            Carbon::now()
85
                                  ->addDay(),
86
                            $resourceIds
87
                        )
88
                    );
89
90
                    $formio->setConfigs(Config::get('formio'));
91
                }
92
93
                $roleIds = function () use ($formio) {
94
                    $roles = (array)$formio->login()
95
                                           ->request('role?title=Authenticated')[0]['_id'];
96
97
                    $formio->logout();
98
99
                    return $roles;
100
                };
101
102
                // If the user roles are null, then get it or a cached value for authenticated user
103
                if (empty(Config::get('formio.user.roles'))) {
104
                    Config::set(
105
                        'formio.user.roles',
106
                        Cache::remember(
107
                            'formio.user.roles',
108
                            Carbon::now()
109
                                  ->addDay(),
110
                            $roleIds
111
                        )
112
                    );
113
114
                    $formio->setConfigs(Config::get('formio'));
115
                }
116
117
                return $formio;
118
            }
119
        );
120
    }
121
}
122