TmdbServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @package php-tmdb\laravel
4
 * @author Mark Redeman <[email protected]>
5
 * @copyright (c) 2014, Mark Redeman
6
 */
7
namespace Tmdb\Laravel;
8
9
use Illuminate\Support\ServiceProvider;
10
use Tmdb\Laravel\TmdbServiceProviderLaravel4;
11
use Tmdb\Laravel\TmdbServiceProviderLaravel5;
12
use Tmdb\ApiToken;
13
use Tmdb\Client;
14
15
class TmdbServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Indicates if loading of the provider is deferred.
19
     *
20
     * @var bool
21
     */
22
    protected $defer = false;
23
24
    /**
25
     * Actual provider
26
     *
27
     * @var \Illuminate\Support\ServiceProvider
28
     */
29
    protected $provider;
30
31
    /**
32
     * Construct the TMDB service provider
33
     */
34
    public function __construct()
35
    {
36
        // Call the parent constructor with all provided arguments
37
        $arguments = func_get_args();
38
        call_user_func_array(
39
            [$this, 'parent::' . __FUNCTION__],
40
            $arguments
41
        );
42
43
        $this->registerProvider();
44
    }
45
46
    /**
47
     * Bootstrap the application events.
48
     *
49
     * @return void
50
     */
51
    public function boot()
52
    {
53
        return $this->provider->boot();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Support\ServiceProvider as the method boot() does only exist in the following sub-classes of Illuminate\Support\ServiceProvider: Tmdb\Laravel\TmdbServiceProvider, Tmdb\Laravel\TmdbServiceProviderLaravel4, Tmdb\Laravel\TmdbServiceProviderLaravel5. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
54
    }
55
56
    /**
57
     * Register the service provider.
58
     *
59
     * @return void
60
     */
61
    public function register()
62
    {
63
        // Configure any bindings that are version dependent
64
        $this->provider->register();
65
66
        // Let the IoC container be able to make a Symfony event dispatcher
67
        $this->app->bind(
68
            'Symfony\Component\EventDispatcher\EventDispatcherInterface',
69
            'Symfony\Component\EventDispatcher\EventDispatcher'
70
        );
71
72
        // Setup default configurations for the Tmdb Client
73
        $this->app->singleton('Tmdb\Client', function() {
74
            $config = $this->provider->config();
0 ignored issues
show
Bug introduced by
The method config() does not exist on Illuminate\Support\ServiceProvider. Did you maybe mean mergeConfigFrom()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75
            $options = $config['options'];
76
77
            // Use an Event Dispatcher that uses the Laravel event dispatcher
78
            $options['event_dispatcher'] = $this->app->make('Tmdb\Laravel\Adapters\EventDispatcherAdapter');
79
80
            // Register the client using the key and options from config
81
            $token = new ApiToken($config['api_key']);
82
            return new Client($token, $options);
83
        });
84
85
        // bind the configuration (used by the image helper)
86
        $this->app->bind('Tmdb\Model\Configuration', function() {
87
            $configuration = $this->app->make('Tmdb\Repository\ConfigurationRepository');
88
            return $configuration->load();
89
        });
90
    }
91
92
    /**
93
     * Register the ServiceProvider according to Laravel version
94
     *
95
     * @return \Tmdb\Laravel\Provider\ProviderInterface
96
     */
97
    private function registerProvider()
98
    {
99
        $app = $this->app;
100
101
        // Pick the correct service provider for the current verison of Laravel
102
        $this->provider = (version_compare($app::VERSION, '5.0', '<'))
103
            ? new TmdbServiceProviderLaravel4($app)
104
            : new TmdbServiceProviderLaravel5($app);
105
    }
106
107
    /**
108
     * Get the services provided by the provider.
109
     *
110
     * @return array
111
     */
112
    public function provides()
113
    {
114
        return array('tmdb');
115
    }
116
}
117