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
![]() |
|||
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 |