Completed
Push — master ( 9be902...75f49e )
by Daniel
02:41
created

Compare::getConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 15
rs 9.4286
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\info_compare;
30
31
/**
32
 * Description of compare
33
 *
34
 * @author Transformer-
35
 */
36
class Compare
37
{
38
39
    use \danielgp\common_lib\CommonCode,
40
        \danielgp\info_compare\ConfigurationCompare,
41
        \danielgp\info_compare\OutputFormBuilder,
42
        \danielgp\info_compare\CompareTable;
43
44
    private $config;
45
    private $localConfiguration;
46
    private $serverConfiguration;
47
48
    public function __construct()
49
    {
50
        $this->getConfiguration();
51
        echo $this->setHeaderHtml();
52
        $rqst         = new \Symfony\Component\HttpFoundation\Request;
53
        $superGlobals = $rqst->createFromGlobals();
54
        $this->prepareForOutputForm(['SuperGlobals' => $superGlobals]);
55
        if (!is_null($superGlobals->get('Label'))) {
56
            $this->processInfos(['sGlobals' => $superGlobals]);
57
            echo $this->setFormCurlInfos(['SuperGlobals' => $superGlobals]);
58
            echo $this->setFormInfos(['SuperGlobals' => $superGlobals]);
59
        }
60
        echo $this->setFooterHtml();
61
    }
62
63
    private function getConfiguration()
64
    {
65
        $strdConfig = $this->configuredDeployedInformators();
66
        foreach ($strdConfig['informators'] as $key => $value) {
67
            $this->config['Servers'][] = [
68
                'name' => $key,
69
                'url'  => $value,
70
            ];
71
        }
72
        $haystack                                         = array_keys($strdConfig['informators']);
73
        $this->config['Defaults']['Label']                = $strdConfig['default']['label'];
74
        $this->config['Defaults']['localConfig']          = array_search($strdConfig['default']['source'], $haystack);
75
        $this->config['Defaults']['serverConfig']         = array_search($strdConfig['default']['target'], $haystack);
76
        $this->config['Defaults']['displayOnlyDifferent'] = $strdConfig['default']['typeOfResults'];
77
    }
78
79
    private function prepareForOutputForm($inArray)
80
    {
81
        $urlToGetLbl = $this->config['Servers'][$this->config['Defaults']['localConfig']]['url']
82
                . '?Label=---' . urlencode(' List of known labels');
83
        $knownLabels = $this->getContentFromUrlThroughCurlAsArrayIfJson($urlToGetLbl)['response'];
84
        echo $this->setOutputForm([
85
            'Defaults'     => $this->config['Defaults'],
86
            'KnownLabels'  => $knownLabels,
87
            'Servers'      => $this->config['Servers'],
88
            'SuperGlobals' => $inArray['SuperGlobals'],
89
        ]);
90
    }
91
92
    private function processInfos($inArray)
93
    {
94
        if (!is_null($inArray['sGlobals']->get('localConfig')) && !is_null($inArray['sGlobals']->get('serverConfig'))) {
95
            $urlArguments              = '?Label=' . urlencode($inArray['sGlobals']->get('Label'));
96
            $source                    = $this->config['Servers'][$inArray['sGlobals']->get('localConfig')]['url']
97
                    . $urlArguments;
98
            $this->localConfiguration  = $this->getContentFromUrlThroughCurlAsArrayIfJson($source);
99
            $destination               = $this->config['Servers'][$inArray['sGlobals']->get('serverConfig')]['url']
100
                    . $urlArguments;
101
            $this->serverConfiguration = $this->getContentFromUrlThroughCurlAsArrayIfJson($destination);
102
        } else {
103
            $this->localConfiguration  = ['response' => '', 'info' => ''];
104
            $this->serverConfiguration = ['response' => '', 'info' => ''];
105
        }
106
    }
107
108
    private function setFooterHtml()
109
    {
110
        $footerToInject = [
111
            '</div><!-- from main Tabber -->',
112
            '<div class="resetOnly author">&copy; 2015 Daniel Popiniuc</div>',
113
            '<hr/><div class="disclaimer">The developer cannot be liable of any data input or results, ',
114
            'included but not limited to any implication of these (anywhere and whomever there might be these)!',
115
            '</div>',
116
        ];
117
        return $this->setFooterCommon($footerToInject);
118
    }
119
120
    private function setFormCurlInfos($inArray)
121
    {
122
        return '<div class="tabbertab" id="tabCurl" title="CURL infos">'
123
                . $this->displayTableFromMultiLevelArray([
124
                    'source'       => $this->localConfiguration['info'],
125
                    'destination'  => $this->serverConfiguration['info'],
126
                    'Servers'      => $this->config['Servers'],
127
                    'SuperGlobals' => $inArray['SuperGlobals'],
128
                ])
129
                . '</div><!--from tabCurl-->';
130
    }
131
132
    private function setFormInfos($inArray)
133
    {
134
        return '<div class="tabbertab'
135
                . (is_null($inArray['SuperGlobals']->get('Label')) ? '' : ' tabbertabdefault')
136
                . '" id="tabConfigs" title="Informations">'
137
                . $this->displayTableFromMultiLevelArray([
138
                    'source'       => $this->localConfiguration['response'],
139
                    'destination'  => $this->serverConfiguration['response'],
140
                    'Servers'      => $this->config['Servers'],
141
                    'SuperGlobals' => $inArray['SuperGlobals'],
142
                ])
143
                . '</div><!--from tabConfigs-->';
144
    }
145
146
    private function setHeaderHtml()
147
    {
148
        return $this->setHeaderCommon([
149
                    'lang'       => 'en-US',
150
                    'title'      => $this->configuredApplicationName(),
151
                    'css'        => 'css/main.css',
152
                    'javascript' => 'js/tabber.min.js',
153
                ])
154
                . $this->setJavascriptContent('document.write(\'<style type="text/css">'
155
                        . '.tabber{display:none;}</style>\');')
156
                . '<h1>' . $this->configuredApplicationName() . '</h1>'
157
                . '<div class="tabber" id="tab">';
158
    }
159
}
160