Passed
Push — master ( 717366...3e7db8 )
by nicolas
43s
created

PdoScriptCache::setProductUrl()   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 0
Metric Value
c 0
b 0
f 0
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 PdoScriptCache
7
 */
8
class PdoScriptCache 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 \PDO $connection
23
     */
24
    public function __construct(\PDO $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
        $request = $this->connection->prepare('SELECT our_value FROM adback_cache_table WHERE our_key = :key LIMIT 1');
225
        $request->execute([
226
            'key' => $key,
227
        ]);
228
229
        $data = $request->fetch();
230
        $request->closeCursor();
231
        if (is_array($data) && array_key_exists('our_value', $data)) {
232
            return $data['our_value'];
233
        }
234
235
        return null;
236
    }
237
238
    /**
239
     * @param string $key
240
     * @param string $value
241
     */
242
    protected function set($key, $value)
243
    {
244
        $this->clear($key);
245
        $request = $this->connection->prepare("INSERT INTO adback_cache_table (our_key, our_value) VALUES (:key, :value)");
246
        $request->execute([
247
            'key' => $key,
248
            'value' => $value,
249
        ]);
250
    }
251
252
    /**
253
     * @param string $key
254
     */
255
    protected function clear($key)
256
    {
257
        $request = $this->connection->prepare("DELETE FROM adback_cache_table WHERE our_key = :key");
258
        $request->execute([
259
            'key' => $key,
260
        ]);
261
    }
262
}
263