Issues (3627)

CoreBundle/Helper/PRedisConnectionHelper.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright   2020 Mautic Contributors. All rights reserved
7
 * @author      Mautic
8
 *
9
 * @link        https://mautic.org
10
 *
11
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Mautic\CoreBundle\Helper;
15
16
/**
17
 * Helper functions for simpler operations with arrays.
18
 */
19
class PRedisConnectionHelper
20
{
21
    /**
22
     * Transform the redis url config key into an array if needed
23
     * If possible also resolve the configured redis hostname to multiple IP addresses.
24
     *
25
     * @param mixed $configuredUrls a string or an array of redis endpoints to connect to
26
     */
27
    public static function getRedisEndpoints($configuredUrls): iterable
28
    {
29
        if (is_iterable($configuredUrls)) {
30
            // assume arrays are already in the correct format
31
            return $configuredUrls;
32
        } else {
33
            $parsed = parse_url($configuredUrls);
34
            if (!$parsed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsed of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
                return [$configuredUrls];
36
            }
37
38
            // resolve hostnames ahead of time to support dns records with multiple ip addresses
39
            // we need to provide each one to predis separately or it will just use a single one
40
            $resolvedArray = gethostbynamel($parsed['host']);
41
            if (!$resolvedArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $resolvedArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
                return [$configuredUrls];
43
            } else {
44
                // this will return an array of associative arrays which is supported by Predis
45
                return array_map(function ($i) use ($parsed) {
46
                    $parsed['host'] = $i;
47
48
                    return $parsed;
49
                }, $resolvedArray);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Transform the redis mautic config to an options array consumable by PRedis.
56
     *
57
     * @param array $redisConfiguration mautic's redis configuration
58
     */
59
    public static function makeRedisOptions(array $redisConfiguration, string $prefix = ''): array
60
    {
61
        $redisOptions = [];
62
63
        if ($prefix) {
64
            $redisOptions['prefix'] = $prefix;
65
        }
66
67
        if (!empty($redisConfiguration['replication'])) {
68
            $redisOptions['replication'] = $redisConfiguration['replication'];
69
            $redisOptions['service']     = $redisConfiguration['service'] ?? 'mymaster';
70
        }
71
72
        if (!empty($redisConfiguration['password'])) {
73
            $redisOptions['parameters'] = ['password' => $redisConfiguration['password']];
74
        }
75
76
        return $redisOptions;
77
    }
78
}
79