DataService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 107
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getApiResult() 0 12 4
A getAllResults() 0 16 3
A makeApiCall() 0 5 1
A getApiCall() 0 7 1
1
<?php
2
/**
3
 * Class DataService
4
 */
5
6
namespace HDNET\OnpageIntegration\Service;
7
8
use HDNET\OnpageIntegration\Exception\ApiErrorException;
9
use HDNET\OnpageIntegration\Provider\AuthenticationProvider;
10
use HDNET\OnpageIntegration\Provider\ConfigurationProvider;
11
12
/**
13
 * Class DataService
14
 */
15
class DataService extends AbstractService
16
{
17
18
    /**
19
     * @var \HDNET\OnpageIntegration\Provider\ConfigurationProvider
20
     */
21
    protected $configurationProvider;
22
23
    /**
24
     * @var \HDNET\OnpageIntegration\Service\ArrayService
25
     */
26
    protected $arrayService;
27
28
    /**
29
     * @var \HDNET\OnpageIntegration\Provider\AuthenticationProvider
30
     */
31
    protected $authenticationProvider;
32
33
    /**
34
     * @var \HDNET\OnpageIntegration\Service\ApiCallService
35
     */
36
    protected $apiCallService;
37
38
    /**
39
     * DataService constructor.
40
     *
41
     * @param ConfigurationProvider $configurationProvider
42
     * @param AuthenticationProvider $authenticationProvider
43
     * @param ArrayService $arrayService
44
     * @param ApiCallService $apiCallService
45
     */
46
    public function __construct(
47
        ConfigurationProvider $configurationProvider,
48
        AuthenticationProvider $authenticationProvider,
49
        ArrayService $arrayService,
50
        ApiCallService $apiCallService
51
    ) {
52
        $this->configurationProvider = $configurationProvider;
53
        $this->authenticationProvider = $authenticationProvider;
54
        $this->arrayService = $arrayService;
55
        $this->apiCallService = $apiCallService;
56
    }
57
58
    /**
59
     * @param string $key
60
     *
61
     * @return array
62
     * @throws ApiErrorException
63
     */
64
    public function getApiResult($key)
65
    {
66
        $apiCall = $this->getApiCall($key);
67
        $result = $this->makeApiCall($apiCall);
68
        $result = json_decode($result, true);
69
70
        if (!isset($result['status']) || $result['status'] != 'success' || !isset($result['result'])) {
71
            throw new ApiErrorException('There has been a negative result for your request.');
72
        }
73
74
        return $result['result'];
75
    }
76
77
    /**
78
     * @return array
79
     * @throws ApiErrorException
80
     */
81
    public function getAllResults()
82
    {
83
        $results = [];
84
        $configData = $this->configurationProvider->getAllConfigurationData();
85
        $keys = $this->arrayService->findByContainedKey($configData, 'authentication');
86
87
        foreach ($keys as $key) {
88
            try {
89
                $results[$key] = $this->getApiResult($key);
90
            } catch (ApiErrorException $e) {
91
                continue;
92
            }
93
        }
94
95
        return $results;
96
    }
97
98
    /**
99
     * @param array $apiCall
100
     *
101
     * @return string
102
     */
103
    protected function makeApiCall(array $apiCall)
104
    {
105
        $json = json_encode($apiCall);
106
        return $this->apiCallService->makeCall($json);
107
    }
108
109
    /**
110
     * @param string $apiCallKey
111
     *
112
     * @return array
113
     */
114
    protected function getApiCall($apiCallKey)
115
    {
116
        $authenticationData = $this->authenticationProvider->get();
117
        $configurationData = $this->configurationProvider->getSingleConfiguration($apiCallKey);
118
119
        return $this->arrayService->replaceRecursiveByKey($configurationData, $authenticationData, 'authentication');
120
    }
121
}
122