ServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Laravel;
4
5
use Psr\Http\Client\ClientInterface as HttpClientInterface;
6
use Psr\Http\Message\RequestFactoryInterface;
7
use Psr\Http\Message\UriFactoryInterface;
8
use Scriptotek\Alma\Client as AlmaClient;
9
use Scriptotek\Alma\Zones;
10
use Scriptotek\Sru\Client as SruClient;
11
12
class ServiceProvider extends \Illuminate\Support\ServiceProvider
13
{
14
    /**
15
     * Indicates if loading of the provider is deferred.
16
     *
17
     * @var bool
18
     */
19
    protected $defer = true;
20
21
    /**
22
     * Bootstrap the application events.
23
     *
24
     * @return void
25
     */
26
    public function boot()
27
    {
28
        $this->publishes([
29
            __DIR__ . '/../../config/alma.php' => config_path('alma.php'),
30
        ]);
31
    }
32
33
    /**
34
     * Register the service provider.
35
     *
36
     * @return void
37
     */
38
    public function register()
39
    {
40
        $this->mergeConfigFrom(
41
            __DIR__ . '/../../config/alma.php',
42
            'alma'
43
        );
44
45
        $this->app->singleton(AlmaClient::class, function ($app) {
46
47
            // Create Alma client
48
            $alma = new AlmaClient(
49
                $app['config']->get('alma.iz.key'),
50
                $app['config']->get('alma.region'),
51
                Zones::INSTITUTION,
52
                isset($app[HttpClientInterface::class]) ? $app[HttpClientInterface::class] : null,
53
                isset($app[RequestFactoryInterface::class]) ? $app[RequestFactoryInterface::class] : null,
54
                isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null
55
            );
56
57
            if ($app['config']->get('alma.iz.entrypoint')) {
58
                $alma->setEntryPoint($app['config']->get('alma.iz.entrypoint'));
59
            }
60
            $alma->setExtraHeaders($app['config']->get('alma.iz.headers', []));
61
62
            // Set network zone key, if any
63
            $alma->nz->setKey($app['config']->get('alma.nz.key'));
64
65
            if ($app['config']->get('alma.nz.entrypoint')) {
66
                $alma->nz->setEntryPoint($app['config']->get('alma.nz.entrypoint'));
67
            }
68
            $alma->nz->setExtraHeaders($app['config']->get('alma.nz.headers', []));
69
70
            // Optionally, attach SRU client for institution zone
71 View Code Duplication
            if ($app['config']->get('alma.iz.sru')) {
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...
72
                $alma->setSruClient(new SruClient(
73
                    $app['config']->get('alma.iz.sru'),
74
                    ['version' => '1.2', 'schema' => 'marcxml']
75
                ));
76
            }
77
78
            // Optionally, attach SRU client for network zone
79 View Code Duplication
            if ($app['config']->get('alma.nz.sru')) {
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...
80
                $alma->nz->setSruClient(new SruClient(
81
                    $app['config']->get('alma.nz.sru'),
82
                    ['version' => '1.2', 'schema' => 'marcxml']
83
                ));
84
            }
85
86
            return $alma;
87
        });
88
    }
89
90
    /**
91
     * Get the services provided by the provider.
92
     *
93
     * @return array
94
     */
95
    public function provides()
96
    {
97
        return [AlmaClient::class];
98
    }
99
}
100