Passed
Push — master ( 1031f0...a89f83 )
by Guillaume
02:57
created

ScriptUrlQuery::execute()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 26
cts 26
cp 1
rs 5.3846
cc 8
eloc 24
nc 9
nop 0
crap 8
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://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_domain', $scriptUrlFacade) && null != $scriptUrlFacade['message_domain']) {
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_domain', $scriptUrlFacade) && null != $scriptUrlFacade['autopromo_banner_domain']) {
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
}
76