Passed
Pull Request — master (#16)
by Chris
01:43
created

SwiftypeCredentials   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
c 1
b 0
f 0
dl 0
loc 205
rs 10
wmc 19

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setMessage() 0 3 1
A getMessage() 0 3 1
A setAPIKey() 0 3 1
A getLogger() 0 7 2
A getDomainID() 0 3 1
A setEnabled() 0 3 1
B __construct() 0 57 6
A getEngineSlug() 0 3 1
A isEnabled() 0 3 1
A setDomainID() 0 3 1
A disable() 0 9 1
A getAPIKey() 0 3 1
A setEngineSlug() 0 3 1
1
<?php
2
3
namespace Ichaber\SSSwiftype\Service;
4
5
use Psr\Log\LoggerInterface;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\Debug;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
/**
13
 * Credit: [Bernard Hamlin](https://github.com/blueo) and [Mojmir Fendek](https://github.com/mfendeksilverstripe)
14
 *
15
 * Class SwiftypeCredentials
16
 *
17
 * @package Ichaber\SSSwiftype\Service
18
 */
19
class SwiftypeCredentials
20
{
21
    use Extensible;
22
    use Injectable;
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
29
    /**
30
     * @var bool
31
     */
32
    private $enabled = false;
33
34
    /**
35
     * @var string|null
36
     */
37
    private $engineSlug;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $domainID;
43
44
    /**
45
     * @var string|null
46
     */
47
    private $apiKey;
48
49
    /**
50
     * @var string|null
51
     */
52
    private $message;
53
54
    /**
55
     * @return bool
56
     */
57
    public function isEnabled(): bool
58
    {
59
        return $this->enabled;
60
    }
61
62
    /**
63
     * @param bool|null $enabled
64
     */
65
    public function setEnabled(?bool $enabled): void
66
    {
67
        $this->enabled = (bool) $enabled;
68
    }
69
70
    /**
71
     * @return string|null
72
     */
73
    public function getEngineSlug(): ?string
74
    {
75
        return $this->engineSlug;
76
    }
77
78
    /**
79
     * @param string|null $engineSlug
80
     */
81
    public function setEngineSlug(?string $engineSlug): void
82
    {
83
        $this->engineSlug = $engineSlug;
84
    }
85
86
    /**
87
     * @return string|null
88
     */
89
    public function getDomainID(): ?string
90
    {
91
        return $this->domainID;
92
    }
93
94
    /**
95
     * @param string|null $domainID
96
     */
97
    public function setDomainID(?string $domainID): void
98
    {
99
        $this->domainID = $domainID;
100
    }
101
102
    /**
103
     * @return string|null
104
     */
105
    public function getAPIKey(): ?string
106
    {
107
        return $this->apiKey;
108
    }
109
110
    /**
111
     * @param string|null $apiKey
112
     */
113
    public function setAPIKey(?string $apiKey): void
114
    {
115
        $this->apiKey = $apiKey;
116
    }
117
118
    /**
119
     * @return string|null
120
     */
121
    public function getMessage(): ?string
122
    {
123
        return $this->message;
124
    }
125
126
    /**
127
     * @param string $message
128
     */
129
    public function setMessage(string $message): void
130
    {
131
        $this->message = $message;
132
    }
133
134
    /**
135
     * Gets the Swiftype credentials
136
     *
137
     * @param mixed|null $additionalData If set, we assume that you want to populate your Credentials through extension
138
     * @return void
139
     */
140
    public function __construct($additionalData = null)
141
    {
142
        if ($additionalData !== null) {
143
            // You've supplied the class with $additionalData, so, please populate your Credentials data through the
144
            // populateCredentials extensions point. It will have access to your $additionalData also
145
            $this->invokeWithExtensions('populateCredentials', $additionalData);
146
        } else {
147
            // Default functionality is to grab Credentials from SiteConfig
148
            /** @var SiteConfig $config */
149
            $config = SiteConfig::current_site_config();
150
            
151
            Debug::dump($config->SwiftypeEnabled);
152
            Debug::dump($config->SwiftypeEngineSlug);
153
            Debug::dump($config->SwiftypeDomainID);
154
            Debug::dump($config->SwiftypeAPIKey);
155
156
            // You might want to implement this via Environment variables or something. Just make sure SiteConfig has
157
            // access to that variable, and return it here
158
            $this->setEnabled((bool) $config->relField('SwiftypeEnabled'));
159
160
            // If you have multiple Engines per site (maybe you use Fluent with a different Engine on each Locale), then
161
            // this provides some basic ability to have different credentials returned based on the application state
162
            $this->setEngineSlug($config->relField('SwiftypeEngineSlug'));
163
            $this->setDomainID($config->relField('SwiftypeDomainID'));
164
            $this->setAPIKey($config->relField('SwiftypeAPIKey'));
165
        }
166
167
        if (!$this->isEnabled()) {
168
            $this->disable(
169
                'Swiftype is disabled. It can be enabled under Settings > Swiftype Search'
170
            );
171
172
            return;
173
        }
174
175
        if (!$this->getEngineSlug()) {
176
            $this->disable(
177
                'Swiftype Engine Slug value has not been set. Settings > Swiftype Search > Swiftype Engine Slug'
178
            );
179
180
            return;
181
        }
182
183
        if (!$this->getDomainID()) {
184
            $this->disable(
185
                'Swiftype Domain ID has not been set. Settings > Swiftype Search > Swiftype Domain ID'
186
            );
187
188
            return;
189
        }
190
191
        if (!$this->getAPIKey()) {
192
            $this->disable(
193
                'Swiftype API Key has not been set. Settings > Swiftype Search > Swiftype Production API Key'
194
            );
195
196
            return;
197
        }
198
    }
199
200
    /**
201
     * @param string $message
202
     */
203
    protected function disable(string $message): void
204
    {
205
        $trace = debug_backtrace();
206
207
        // array_shift for adding context (for RaygunHandler) by using the last item on the stack trace.
208
        $this->getLogger()->warning($message, array_shift($trace));
209
210
        $this->setMessage($message);
211
        $this->setEnabled(false);
212
    }
213
214
    /**
215
     * @return LoggerInterface
216
     */
217
    protected function getLogger(): LoggerInterface
218
    {
219
        if (!$this->logger) {
220
            $this->logger = Injector::inst()->get(LoggerInterface::class);
221
        }
222
223
        return $this->logger;
224
    }
225
}
226