SwivelLoader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 67.86%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 104
ccs 19
cts 28
cp 0.6786
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 4 1
A getConfig() 0 14 3
A setBucketIndex() 0 11 5
A getModel() 0 3 1
A getManager() 0 3 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelSwivel;
6
7
use LaravelSwivel\Entity\SwivelFeature;
8
9
/**
10
 * @package Webkod3r\LaravelSwivel
11
 * @author Pablo Molina <[email protected]>
12
 */
13
class SwivelLoader
14
{
15
    /**
16
     * Swivel config
17
     *
18
     * @var \Zumba\Swivel\Config
19
     */
20
    protected $config;
21
22
    /**
23
     * Configuration options
24
     *
25
     * @var array
26
     */
27
    protected $options;
28
29
    /**
30
     * Swivel manager
31
     *
32
     * @var \Zumba\Swivel\Manager
33
     */
34
    protected $manager;
35
36
    /**
37
     * SwivelLoader only creates the swivel manager whenever you try to use it.
38
     *
39
     * @param array $options Set of options to build the loader with
40
     */
41 2
    public function __construct(array $options)
42
    {
43 2
        $this->options = $options;
44 2
    }
45
46
    /**
47
     * Get the swivel config instance
48
     *
49
     * @return \Zumba\Swivel\Config
50
     */
51 2
    public function getConfig()
52
    {
53 2
        if (empty($this->config)) {
54 2
            $options = $this->options;
55 2
            $this->config = new \Zumba\Swivel\Config(
56 2
                $this->getModel()->getMapData(),
57 2
                $options['BucketIndex'],
58 2
                $options['Logger']
59
            );
60 2
            if (!empty($options['Metrics'])) {
61
                $this->config->setMetrics($options['Metrics']);
62
            }
63
        }
64 2
        return $this->config;
65
    }
66
67
    /**
68
     * Get the swivel manager instance
69
     *
70
     * @return \Zumba\Swivel\Manager
71
     */
72 2
    public function getManager()
73
    {
74 2
        return $this->manager ?: $this->load();
75
    }
76
77
    /**
78
     * Get the configured swivel model.
79
     * Falls back to the SwivelFeature model provided by the plugin if the app does not define one.
80
     *
81
     * @return SwivelFeature
82
     */
83 2
    protected function getModel()
84
    {
85 2
        return new $this->options['ModelAlias']();
86
    }
87
88
    /**
89
     * Create a Swivel Manager object and return it.
90
     *
91
     * @return \Zumba\Swivel\Manager
92
     */
93 2
    protected function load()
94
    {
95 2
        $this->manager = new \Zumba\Swivel\Manager($this->getConfig());
96 2
        return $this->manager;
97
    }
98
99
    /**
100
     * Used to set the bucket index before loading swivel.
101
     *
102
     * @param integer $index Number between 1 and 10
103
     * @return void
104
     * @throws \InvalidArgumentException if $index is not valid
105
     */
106
    public function setBucketIndex($index)
107
    {
108
        if (!is_numeric($index) || $index < 1 || $index > 10) {
0 ignored issues
show
introduced by
The condition is_numeric($index) is always true.
Loading history...
109
            throw new \InvalidArgumentException("SwivelLoader: $index is not a valid bucket index.");
110
        }
111
        if (empty($this->manager)) {
112
            $this->options['BucketIndex'] = $index;
113
        } else {
114
            $config = $this->getConfig();
115
            $config->setBucketIndex($index);
116
            $this->manager->setBucket($config->getBucket());
117
        }
118
    }
119
}
120