Completed
Push — 2.x ( ed04a7...fe437f )
by Cy
01:45
created

rebindHorizonRedisFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel\Horizon;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Redis\Factory as RedisFactory;
7
use Illuminate\Support\ServiceProvider;
8
use Laravel\Horizon\Connectors\RedisConnector as HorizonRedisConnector;
9
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
10
use Monospice\LaravelRedisSentinel\Horizon\HorizonServiceBindings;
11
use Monospice\LaravelRedisSentinel\Manager\VersionedManagerFactory;
12
use Monospice\LaravelRedisSentinel\RedisSentinelServiceProvider;
13
14
/**
15
 * Configures the application to use Redis Sentinel connections for Laravel
16
 * Horizon.
17
 *
18
 * For applications that use Sentinel connections only for the queue API and
19
 * Horizon, we can register this service provider without the package's main
20
 * service provider.
21
 *
22
 * @category Package
23
 * @package  Monospice\LaravelRedisSentinel
24
 * @author   Cy Rossignol <[email protected]>
25
 * @license  See LICENSE file
26
 * @link     https://github.com/monospice/laravel-redis-sentinel-drivers
27
 */
28
class HorizonServiceProvider extends ServiceProvider
29
{
30
    /**
31
     * Loads the package's configuration and provides configuration values.
32
     *
33
     * @var ConfigurationLoader
34
     */
35
    protected $config;
36
37
    /**
38
     * Create a new service provider instance.
39
     *
40
     * @param Container           $app    The current Laravel/Lumen application
41
     * instance.
42
     * @param ConfigurationLoader $config Loads the package's configuration and
0 ignored issues
show
Documentation introduced by
Should the type for parameter $config not be null|ConfigurationLoader?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
43
     * provides configuration values.
44
     */
45
    public function __construct(
46
        Container $app,
47
        ConfigurationLoader $config = null
48
    ) {
49
        parent::__construct($app);
0 ignored issues
show
Compatibility introduced by
$app of type object<Illuminate\Contracts\Container\Container> is not a sub-type of object<Illuminate\Contra...Foundation\Application>. It seems like you assume a child interface of the interface Illuminate\Contracts\Container\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
51
        if ($config === null) {
52
            $config = ConfigurationLoader::load($app);
0 ignored issues
show
Documentation introduced by
$app is of type object<Illuminate\Contracts\Container\Container>, but the function expects a object<Illuminate\Founda...avel\Lumen\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
        }
54
55
        $this->config = $config;
56
    }
57
58
    /**
59
     * Set up any additional components needed for Horizon.
60
     *
61
     * @return void
62
     */
63
    public function boot()
64
    {
65
        if (! $this->config->shouldIntegrateHorizon) {
66
            return;
67
        }
68
69
        $this->addHorizonSentinelQueueConnector();
70
    }
71
72
    /**
73
     * Configure the package's services for use with Laravel Horizon.
74
     *
75
     * @return void
76
     */
77
    public function register()
78
    {
79
        if (! $this->config->shouldIntegrateHorizon) {
80
            return;
81
        }
82
83
        $this->config->loadHorizonConfiguration();
84
85
        $this->registerServices();
86
87
        if ($this->shouldRebindHorizonRedisFactory()) {
88
            $this->rebindHorizonRedisFactory();
89
        }
90
    }
91
92
    /**
93
     * Register the core Redis Sentinel connection manager if not already bound
94
     * (for using this service provider by itself).
95
     *
96
     * @return void
97
     */
98
    protected function registerServices()
99
    {
100
        $this->app->bindIf('redis-sentinel', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
            return VersionedManagerFactory::make($this->config);
102
        }, true);
103
104
        $this->app->bindIf('redis-sentinel.manager', function ($app) {
105
            return $app->make('redis-sentinel')->getVersionedManager();
106
        }, true);
107
    }
108
109
    /**
110
     * Determine whether the package needs to override the Redis service
111
     * injected into Horizon classes with the Sentinel service.
112
     *
113
     * @return bool True if configured as such and the package doesn't already
114
     * override the application's Redis API.
115
     */
116
    protected function shouldRebindHorizonRedisFactory()
117
    {
118
        // If we're using this package for Horizon only, we only register this
119
        // service provider, so nothing overrides Laravel's standard Redis API:
120
        if (! $this->app->bound(RedisSentinelServiceProvider::class)) {
121
            return true;
122
        }
123
124
        // If we're already overriding Laravel's standard Redis API, we don't
125
        // need to rebind the "redis" service for Horizon.
126
        return ! $this->config->shouldOverrideLaravelRedisApi;
127
    }
128
129
    /**
130
     * Add contextual bindings for Horizon's services that inject the package's
131
     * Redis Sentinel manager.
132
     *
133
     * @return void
134
     */
135
    protected function rebindHorizonRedisFactory()
136
    {
137
        // Although not all of the classes that Horizon registers need an
138
        // instance of the Redis service, we'll set up contextual bindings
139
        // for any declared so we don't need to update this package in the
140
        // future every time Horizon adds or removes one:
141
        foreach ((new HorizonServiceBindings()) as $serviceClass) {
142
            $this->app->when($serviceClass)
143
                ->needs(RedisFactory::class)
144
                ->give(function () {
145
                    return $this->app->make('redis-sentinel.manager');
146
                });
147
        }
148
    }
149
150
    /**
151
     * Add "redis-sentinel" as an available queue connection driver option to
152
     * the Laravel queue manager using Horizon's modified Redis connector.
153
     *
154
     * @return void
155
     */
156 View Code Duplication
    protected function addHorizonSentinelQueueConnector()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
157
    {
158
        $this->app->make('queue')->extend('redis-sentinel', function () {
159
            $redis = $this->app->make('redis-sentinel.manager');
160
161
            return new HorizonRedisConnector($redis);
162
        });
163
    }
164
}
165