Completed
Pull Request — master (#70)
by
unknown
01:22
created

TmdbServiceProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package php-tmdb\laravel
5
 * @author Mark Redeman <[email protected]>
6
 * @copyright (c) 2014, Mark Redeman
7
 */
8
9
namespace Tmdb\Laravel;
10
11
use Arr;
12
use Illuminate\Contracts\Foundation\Application;
13
use Illuminate\Support\ServiceProvider;
14
use Tmdb\ApiToken;
15
use Tmdb\Client;
16
use Tmdb\Laravel\Cache\DoctrineCacheBridge;
17
use Tmdb\Laravel\EventDispatcher\EventDispatcherBridge;
18
use Tmdb\Model\Configuration;
19
use Tmdb\Repository\ConfigurationRepository;
20
21
class TmdbServiceProvider extends ServiceProvider
22
{
23
    protected const CONFIG_PATH = __DIR__ . '/../config/tmdb.php';
24
25
    /**
26
     * Bootstrap the application events.
27
     *
28
     * @return void
29
     */
30
    public function boot()
31
    {
32
        $this->publishes([
33
            self::CONFIG_PATH => config_path('tmdb.php'),
34
        ], 'config');
35
    }
36
37
    /**
38
     * Register the service provider.
39
     *
40
     * @return void
41
     */
42
    public function register()
43
    {
44
        $this->mergeConfigFrom(self::CONFIG_PATH, 'tmdb');
45
46
        // Let the IoC container be able to make a Symfony event dispatcher
47
        $this->app->bindIf(
48
            'Symfony\Component\EventDispatcher\EventDispatcherInterface',
49
            'Symfony\Component\EventDispatcher\EventDispatcher'
50
        );
51
52
        $this->app->singleton(Client::class, function (Application $app) {
53
            $options = config('tmdb.options');
54
55
            if (!Arr::has($options, 'cache.handler')) {
56
                $repository = app('cache')->store(config('tmdb.cache_store'));
57
58
                if (!empty(config('tmdb.cache_tag'))) {
59
                    $repository = $repository->tags(config('tmdb.cache_tag'));
60
                }
61
62
                Arr::set($options, 'cache.handler', new DoctrineCacheBridge($repository));
63
            }
64
65
            if (!Arr::has($options, 'event_dispatcher')) {
66
                Arr::set($options, 'event_dispatcher', $app->make(EventDispatcherBridge::class));
67
            }
68
69
            $token = new ApiToken(config('tmdb.api_key'));
70
            return new Client($token, $options);
71
        });
72
73
        // bind the configuration (used by the image helper)
74
        $this->app->bind(Configuration::class, function () {
75
            $configuration = $this->app->make(ConfigurationRepository::class);
76
            return $configuration->load();
77
        });
78
    }
79
80
    /**
81
     * Get the services provided by the provider.
82
     *
83
     * @return array
84
     */
85
    public function provides()
86
    {
87
        return [
88
            Client::class,
89
        ];
90
    }
91
}
92