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

VersionedManagerFactory::makeInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Monospice\LaravelRedisSentinel\Manager;
4
5
use Illuminate\Support\Arr;
6
use Monospice\LaravelRedisSentinel\Configuration\Loader as ConfigurationLoader;
7
use Monospice\LaravelRedisSentinel\RedisSentinelManager;
8
9
/**
10
 * Creates instances of the core Redis Sentinel connection manager for the
11
 * current version of the framework.
12
 *
13
 * @category Package
14
 * @package  Monospice\LaravelRedisSentinel
15
 * @author   Cy Rossignol <[email protected]>
16
 * @license  See LICENSE file
17
 * @link     https://github.com/monospice/laravel-redis-sentinel-drivers
18
 */
19
class VersionedManagerFactory
20
{
21
    /**
22
     * Detects the application version and provides configuration values.
23
     *
24
     * @var ConfigurationLoader
25
     */
26
    protected $config;
27
28
    /**
29
     * Create a factory using the provided configuration.
30
     *
31
     * @param ConfigurationLoader $config Detects the application version and
32
     * provides configuration values.
33
     */
34
    public function __construct(ConfigurationLoader $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * Create an instance of the package's core Redis Sentinel service.
41
     *
42
     * @param ConfigurationLoader $config Detects the application version and
43
     * provides configuration values.
44
     *
45
     * @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured
46
     * Redis Sentinel connection manager.
47
     */
48
    public static function make(ConfigurationLoader $config)
49
    {
50
        return (new static($config))->makeInstance();
51
    }
52
53
    /**
54
     * Create an instance of the package's core Redis Sentinel service.
55
     *
56
     * @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured
57
     * Redis Sentinel connection manager.
58
     */
59
    public function makeInstance()
60
    {
61
        $class = $this->getVersionedRedisSentinelManagerClass();
62
        $config = $this->config->get('database.redis-sentinel', [ ]);
63
        $driver = Arr::pull($config, 'client', 'predis');
64
65
        return new RedisSentinelManager(new $class($driver, $config));
66
    }
67
68
    /**
69
     * Get the fully-qualified class name of the RedisSentinelManager class
70
     * for the current version of Laravel or Lumen.
71
     *
72
     * @return string The class name of the appropriate RedisSentinelManager
73
     * with its namespace.
74
     */
75
    protected function getVersionedRedisSentinelManagerClass()
76
    {
77
        $appVersion = $this->config->getApplicationVersion();
78
79
        if ($this->config->isLumen) {
80
            $frameworkVersion = '5.4';
81
        } else {
82
            $frameworkVersion = '5.4.20';
83
        }
84
85
        if (version_compare($appVersion, $frameworkVersion, 'lt')) {
86
            return Laravel540RedisSentinelManager::class;
87
        }
88
89
        return Laravel5420RedisSentinelManager::class;
90
    }
91
}
92