Completed
Push — develop ( d6d5e2...840c69 )
by
unknown
03:35
created

ConfigurationProvider::getApiCallsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class Configuration
5
 */
6
7
namespace HDNET\OnpageIntegration\Provider;
8
9
use HDNET\OnpageIntegration\Service\FileService;
10
use HDNET\OnpageIntegration\Exception\UnknownApiCallException;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
13
/**
14
 * Class Configuration
15
 */
16
class ConfigurationProvider extends AbstractProvider
17
{
18
    /**
19
     * @var \HDNET\OnpageIntegration\Service\FileService
20
     */
21
    protected $fileService;
22
23
    /**
24
     * Replace the authentication key with
25
     * api-key information
26
     *
27
     * @param string $apiCallKey
28
     * @return array
29
     * @throws \HDNET\Autoloader\Exception
30
     */
31
    public function getSingleConfiguration($apiCallKey)
32
    {
33
        $apiCallsArray    = $this->getApiCallsArray();
34
        $apiCallArrayKeys = $this->getApiCallArrayKeys($apiCallKey);
35
36
        return $this->findMatchingApiCall($apiCallsArray, $apiCallArrayKeys);
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getAllConfigurationData()
43
    {
44
        return $this->getApiCallsArray();
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    protected function getApiCallsArray()
51
    {
52
        $this->fileService = GeneralUtility::makeInstance(FileService::class);
53
        $apiCallData      = $this->fileService->readFile(
54
            PATH_site . 'typo3conf/ext/onpage_integration/Configuration/ApiCalls.json'
55
        );
56
57
        return json_decode($apiCallData, true);
58
    }
59
60
    /**
61
     * @param array $apiCalls
62
     * @param array $apiCallArrayKeys
63
     * @return array
64
     * @throws \HDNET\OnpageIntegration\Exception\UnknownApiCallException
65
     */
66
    protected function findMatchingApiCall(array $apiCalls, array $apiCallArrayKeys)
67
    {
68
        $apiCall = $apiCalls;
69
70
        foreach ($apiCallArrayKeys as $key) {
71
            if (!isset($apiCall[$key])) {
72
                throw new UnknownApiCallException('Unknown API Call.');
73
            } else {
74
                $apiCall = $apiCall[$key];
75
            }
76
        }
77
78
        return $apiCall;
79
    }
80
81
    /**
82
     * @param string $apiCallKey
83
     * @return array
84
     */
85
    protected function getApiCallArrayKeys($apiCallKey)
86
    {
87
        return explode('_', $apiCallKey);
88
    }
89
}
90