Passed
Push — master ( 3e7db8...bd69b1 )
by nicolas
44s
created

MysqliScriptCache::setProductScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dekalee\AdbackAnalytics\Driver;
4
5
/**
6
 * Class MysqliScriptCache
7
 */
8
class MysqliScriptCache implements ScriptCacheInterface
9
{
10
    protected $connection;
11
12
    const ADBACK_ANALYTICS_SCRIPT = 'adback_analytics_script';
13
    const ADBACK_ANALYTICS_URL = 'adback_analytics_url';
14
    const ADBACK_MESSAGE_SCRIPT = 'adback_message_script';
15
    const ADBACK_MESSAGE_URL = 'adback_message_url';
16
    const ADBACK_AUTOPROMO_BANNER_SCRIPT = 'adback_autopromo_banner_script';
17
    const ADBACK_AUTOPROMO_BANNER_URL = 'adback_autopromo_banner_url';
18
    const ADBACK_PRODUCT_SCRIPT = 'adback_product_script';
19
    const ADBACK_PRODUCT_URL = 'adback_product_url';
20
21
    /**
22
     * @param mixed $connection Mysqli Connection
23
     */
24
    public function __construct($connection)
25
    {
26
        $this->connection = $connection;
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    public function isAnalyticsConfigured()
33
    {
34
        return null != $this->get(self::ADBACK_ANALYTICS_SCRIPT);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->get(self::ADBACK_ANALYTICS_SCRIPT) of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
35
    }
36
37
    /**
38
     * @param string $domain
39
     */
40
    public function setAnalyticsUrl($domain)
41
    {
42
        $this->set(self::ADBACK_ANALYTICS_URL, $domain);
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getAnalyticsUrl()
49
    {
50
        return $this->get(self::ADBACK_ANALYTICS_URL);
51
    }
52
53
    /**
54
     * @param string $script
55
     */
56
    public function setAnalyticsScript($script)
57
    {
58
        $this->set(self::ADBACK_ANALYTICS_SCRIPT, $script);
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getAnalyticsScript()
65
    {
66
        return $this->get(self::ADBACK_ANALYTICS_SCRIPT);
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isMessageConfigured()
73
    {
74
        return null != $this->get(self::ADBACK_MESSAGE_SCRIPT);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->get(self::ADBACK_MESSAGE_SCRIPT) of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isAutopromoBannerConfigured()
81
    {
82
        return null != $this->get(self::ADBACK_AUTOPROMO_BANNER_SCRIPT);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->get(self::ADBACK_AUTOPROMO_BANNER_SCRIPT) of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
83
    }
84
85
    /**
86
     * @param string $domain
87
     */
88
    public function setMessageUrl($domain)
89
    {
90
        $this->set(self::ADBACK_MESSAGE_URL, $domain);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getMessageUrl()
97
    {
98
        return $this->get(self::ADBACK_MESSAGE_URL);
99
    }
100
101
    /**
102
     * @param string $script
103
     */
104
    public function setMessageScript($script)
105
    {
106
        $this->set(self::ADBACK_MESSAGE_SCRIPT, $script);
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getMessageScript()
113
    {
114
        return $this->get(self::ADBACK_MESSAGE_SCRIPT);
115
    }
116
117
    /**
118
     * @param string $domain
119
     */
120
    public function setAutopromoBannerUrl($domain)
121
    {
122
        $this->set(self::ADBACK_AUTOPROMO_BANNER_URL, $domain);
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getAutopromoBannerUrl()
129
    {
130
        return $this->get(self::ADBACK_AUTOPROMO_BANNER_URL);
131
    }
132
133
    /**
134
     * @param string $domain
135
     */
136
    public function setProductUrl($domain)
137
    {
138
        $this->set(self::ADBACK_PRODUCT_URL, $domain);
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getProductUrl()
145
    {
146
        return $this->get(self::ADBACK_PRODUCT_URL);
147
    }
148
149
    /**
150
     * @param string $script
151
     */
152
    public function setAutopromoBannerScript($script)
153
    {
154
        $this->set(self::ADBACK_AUTOPROMO_BANNER_SCRIPT, $script);
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getAutopromoBannerScript()
161
    {
162
        return $this->get(self::ADBACK_AUTOPROMO_BANNER_SCRIPT);
163
    }
164
165
    /**
166
     * @param string $script
167
     */
168
    public function setProductScript($script)
169
    {
170
        $this->set(self::ADBACK_PRODUCT_SCRIPT, $script);
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getProductScript()
177
    {
178
        return $this->get(self::ADBACK_PRODUCT_SCRIPT);
179
    }
180
181
    /**
182
     * Clear analytics data
183
     */
184
    public function clearAnalyticsData()
185
    {
186
        $this->clear(self::ADBACK_ANALYTICS_SCRIPT);
187
        $this->clear(self::ADBACK_ANALYTICS_URL);
188
    }
189
190
    /**
191
     * Clear message data
192
     */
193
    public function clearMessageData()
194
    {
195
        $this->clear(self::ADBACK_MESSAGE_SCRIPT);
196
        $this->clear(self::ADBACK_MESSAGE_URL);
197
    }
198
199
    /**
200
     * Clear autopromo banner data
201
     */
202
    public function clearAutopromoBannerData()
203
    {
204
        $this->clear(self::ADBACK_AUTOPROMO_BANNER_SCRIPT);
205
        $this->clear(self::ADBACK_AUTOPROMO_BANNER_URL);
206
    }
207
208
    /**
209
     * Clear autopromo banner data
210
     */
211
    public function clearProductData()
212
    {
213
        $this->clear(self::ADBACK_PRODUCT_SCRIPT);
214
        $this->clear(self::ADBACK_PRODUCT_URL);
215
    }
216
217
    /**
218
     * @param string $key
219
     *
220
     * @return string|null
221
     */
222
    protected function get($key)
223
    {
224
        $value = null;
225
        $request = $this->connection->prepare('SELECT our_value FROM adback_cache_table WHERE our_key = ? LIMIT 1');
226
        $request->bind_param('s', $key);
227
228
        $request->execute();
229
        $request->bind_result($value);
230
        $request->fetch();
231
        $request->free_result();
232
233
        return $value;
234
    }
235
236
    /**
237
     * @param string $key
238
     * @param string $value
239
     */
240 View Code Duplication
    protected function set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        $this->clear($key);
243
        $request = $this->connection->prepare("INSERT INTO adback_cache_table (our_key, our_value) VALUES (?, ?)");
244
        $request->bind_param('ss', $key, $value);
245
        $request->execute();
246
    }
247
248
    /**
249
     * @param string $key
250
     */
251
    protected function clear($key)
252
    {
253
        $request = $this->connection->prepare("DELETE FROM adback_cache_table WHERE our_key = ?");
254
        $request->bind_param('s', $key);
255
        $request->execute();
256
    }
257
}
258