Passed
Push — master ( f1e3b7...717366 )
by nicolas
53s
created

ScriptUrlQuery::execute()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 44
ccs 31
cts 31
cp 1
rs 4.8196
c 3
b 0
f 1
cc 10
eloc 29
nc 17
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
75 3
        if (array_key_exists('product_script', $scriptUrlFacade) && null != $scriptUrlFacade['product_script']) {
76 1
            $this->cache->setProductUrl($scriptUrlFacade['product_domain']);
77 1
            $this->cache->setProductScript($scriptUrlFacade['product_script']);
78 1
        } else {
79 2
            $this->cache->clearProductData();
80
        }
81 3
    }
82
}
83