Completed
Push — master ( c894bb...4464ce )
by Yassir
11s
created

LoadBalancer::formatForwardRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DigitalOceanV2\Api;
13
14
use DigitalOceanV2\Entity\AbstractEntity;
15
use DigitalOceanV2\Entity\ForwardingRule as ForwardRuleEntity;
16
use DigitalOceanV2\Entity\HealthCheck as HealthCheckEntity;
17
use DigitalOceanV2\Entity\LoadBalancer as LoadBalancerEntity;
18
use DigitalOceanV2\Exception\HttpException;
19
20
/**
21
 * @author Jacob Holmes <[email protected]>
22
 */
23
class LoadBalancer extends AbstractApi
24
{
25
    /**
26
     * @return LoadBalancerEntity[]
27
     */
28
    public function getAll()
29
    {
30
        $loadBalancers = $this->adapter->get(sprintf('%s/load_balancers', $this->endpoint));
31
32
        $loadBalancers = json_decode($loadBalancers);
33
34
        $this->extractMeta($loadBalancers);
35
36
        return array_map(function ($key) {
37
            return new LoadBalancerEntity($key);
38
        }, $loadBalancers->load_balancers);
39
    }
40
41
    /**
42
     * @param string $id
43
     *
44
     * @throws HttpException
45
     *
46
     * @return LoadBalancerEntity
47
     */
48
    public function getById($id)
49
    {
50
        $loadBalancer = $this->adapter->get(sprintf('%s/load_balancers/%s', $this->endpoint, $id));
51
52
        $loadBalancer = json_decode($loadBalancer);
53
54
        return new LoadBalancerEntity($loadBalancer->load_balancer);
55
    }
56
57
    /**
58
     * @param string                      $name
59
     * @param string                      $region
60
     * @param array|ForwardRuleEntity[]   $forwardRules
61
     * @param string                      $algorithm
62
     * @param array|HealthCheckEntity[]   $healthCheck
63
     * @param array|StickySessionEntity[] $stickySessions
64
     * @param array                       $dropletIds
65
     * @param bool                        $httpsRedirect
66
     *
67
     * @throws HttpException
68
     *
69
     * @return LoadBalancerEntity
70
     */
71
    public function create(
72
        $name,
73
        $region,
74
        $forwardRules = null,
75
        $algorithm = 'round_robin',
76
        $healthCheck = [],
77
        $stickySessions = [],
78
        $dropletIds = [],
79
        $httpsRedirect = false
80
    ) {
81
        $data = [
82
            'name' => $name,
83
            'algorithm' => $algorithm,
84
            'region' => $region,
85
            'forwarding_rules' => $this->formatForwardRules($forwardRules),
0 ignored issues
show
Bug introduced by
It seems like $forwardRules defined by parameter $forwardRules on line 74 can also be of type null; however, DigitalOceanV2\Api\LoadB...r::formatForwardRules() does only seem to accept array|object<DigitalOcea...\Entity\AbstractEntity>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
            'health_check' => $this->formatConfigurationOptions($healthCheck),
87
            'sticky_sessions' => $this->formatConfigurationOptions($stickySessions),
88
            'droplet_ids' => $dropletIds,
89
            'redirect_http_to_https' => $httpsRedirect,
90
        ];
91
92
        $loadBalancer = $this->adapter->post(sprintf('%s/load_balancers', $this->endpoint), $data);
93
94
        $loadBalancer = json_decode($loadBalancer);
95
96
        return new LoadBalancerEntity($loadBalancer->load_balancer);
97
    }
98
99
    /**
100
     * @param string                   $id
101
     * @param array|LoadBalancerEntity $loadBalancerSpec
102
     *
103
     * @throws HttpException
104
     *
105
     * @return LoadBalancerEntity
106
     */
107
    public function update($id, $loadBalancerSpec)
108
    {
109
        $data = $this->formatConfigurationOptions($loadBalancerSpec);
110
111
        $loadBalancer = $this->adapter->put(sprintf('%s/load_balancers/%s', $this->endpoint, $id), $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->formatConfigurati...ions($loadBalancerSpec) on line 109 can also be of type object<DigitalOceanV2\Entity\AbstractEntity>; however, DigitalOceanV2\Adapter\AdapterInterface::put() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112
113
        $loadBalancer = json_decode($loadBalancer);
114
115
        return new LoadBalancerEntity($loadBalancer->load_balancer);
116
    }
117
118
    /**
119
     * @param string $id
120
     *
121
     * @throws HttpException
122
     */
123
    public function delete($id)
124
    {
125
        $this->adapter->delete(sprintf('%s/load_balancers/%s', $this->endpoint, $id));
126
    }
127
128
    /**
129
     * @param array|AbstractEntity $forwardRules
130
     *
131
     * @return array
132
     */
133
    private function formatForwardRules($forwardRules)
134
    {
135
        if (isset($forwardRules)) {
136
            return array_map(function ($rule) {
137
                return $this->formatConfigurationOptions($rule);
138
            }, $forwardRules);
139
        } else {
140
            return [
141
                (new ForwardRuleEntity())->setStandardHttpRules()->toArray(),
142
                (new ForwardRuleEntity())->setStandardHttpsRules()->toArray(),
143
            ];
144
        }
145
    }
146
147
    /**
148
     * @param array|AbstractEntity $config
149
     *
150
     * @return array|AbstractEntity
151
     */
152
    private function formatConfigurationOptions($config)
153
    {
154
        return $config instanceof AbstractEntity ? $config->toArray() : $config;
155
    }
156
}
157