| 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')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.