|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dekalee\AdbackAnalytics\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Dekalee\AdbackAnalytics\Client\Client; |
|
6
|
|
|
use Dekalee\AdbackAnalytics\Driver\ScriptCacheInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ScriptUrlQuery |
|
10
|
|
|
*/ |
|
11
|
|
|
class ScriptUrlQuery |
|
12
|
|
|
{ |
|
13
|
|
|
protected $cache; |
|
14
|
|
|
protected $token; |
|
15
|
|
|
protected $apiUrl; |
|
16
|
|
|
protected $client; |
|
17
|
|
|
protected $scriptUrl; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Client $client |
|
21
|
|
|
* @param ScriptCacheInterface $cache |
|
22
|
|
|
* @param string $token |
|
23
|
|
|
* @param string $apiUrl |
|
24
|
|
|
* @param string $scriptUrl |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function __construct(Client $client, ScriptCacheInterface $cache, $token, $apiUrl = 'https://www.adback.co/api', $scriptUrl = 'script/me') |
|
27
|
|
|
{ |
|
28
|
5 |
|
$this->cache = $cache; |
|
29
|
5 |
|
$this->token = $token; |
|
30
|
5 |
|
$this->apiUrl = $apiUrl; |
|
31
|
5 |
|
$this->client = $client; |
|
32
|
5 |
|
$this->scriptUrl = $scriptUrl; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the script info and store them |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function execute() |
|
39
|
|
|
{ |
|
40
|
4 |
|
$url = sprintf('%s/%s?_format=json&access_token=%s', |
|
41
|
4 |
|
$this->apiUrl, |
|
42
|
4 |
|
$this->scriptUrl, |
|
43
|
4 |
|
$this->token |
|
44
|
4 |
|
); |
|
45
|
|
|
|
|
46
|
4 |
|
$response = $this->client->get($url); |
|
47
|
|
|
|
|
48
|
4 |
|
if (200 !== $response->getStatusCode()) { |
|
49
|
1 |
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
$scriptUrlFacade = json_decode($response->getBody(), true); |
|
53
|
|
|
|
|
54
|
3 |
|
if (array_key_exists('analytics_domain', $scriptUrlFacade) && null != $scriptUrlFacade['analytics_domain']) { |
|
55
|
2 |
|
$this->cache->setAnalyticsUrl($scriptUrlFacade['analytics_domain']); |
|
56
|
2 |
|
$this->cache->setAnalyticsScript($scriptUrlFacade['analytics_script']); |
|
57
|
2 |
|
} else { |
|
58
|
1 |
|
$this->cache->clearAnalyticsData(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
if (array_key_exists('message_script', $scriptUrlFacade) && null != $scriptUrlFacade['message_script']) { |
|
62
|
2 |
|
$this->cache->setMessageUrl($scriptUrlFacade['message_domain']); |
|
63
|
2 |
|
$this->cache->setMessageScript($scriptUrlFacade['message_script']); |
|
64
|
2 |
|
} else { |
|
65
|
1 |
|
$this->cache->clearMessageData(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
if (array_key_exists('autopromo_banner_script', $scriptUrlFacade) && null != $scriptUrlFacade['autopromo_banner_script']) { |
|
69
|
1 |
|
$this->cache->setAutopromoBannerUrl($scriptUrlFacade['autopromo_banner_domain']); |
|
70
|
1 |
|
$this->cache->setAutopromoBannerScript($scriptUrlFacade['autopromo_banner_script']); |
|
71
|
1 |
|
} else { |
|
72
|
2 |
|
$this->cache->clearAutopromoBannerData(); |
|
73
|
|
|
} |
|
74
|
3 |
|
|
|
75
|
|
|
if (array_key_exists('product_script', $scriptUrlFacade) && null != $scriptUrlFacade['product_script']) { |
|
76
|
|
|
$this->cache->setProductUrl($scriptUrlFacade['product_domain']); |
|
77
|
|
|
$this->cache->setProductScript($scriptUrlFacade['product_script']); |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->cache->clearProductData(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|