Completed
Push — master ( 11f33f...14005b )
by Tim
10:31 queued 07:40
created

DataService::getAllResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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