VIISSchools.php 4.94 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php

namespace Lu\VIISSchools;

use Lu\VIISSchools\Helpers\Builder;
use Lu\VIISSchools\Helpers\WsseAuthHeader;
use Lu\VIISSchools\Models\ReturnType;

/**
 * Class VIISSchools
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @since   30.09.2020
 *
 * @package Lu\VIISSchools
 */
class VIISSchools
{
    /**
     * Define environment type
     *
     * @var string
     */
    private $environment = "LIVE";

    /**
     * Active soap connection
     *
     * @var \SoapClient
     */
    private $soapClient;

    /**
     * Return type
     *
     * @var string
     */
    private $returnType = ReturnType::JSON;

    /**
     * Live connection url
     *
     * @var string
     */
    private $urlLive = "https://test.viis.lv:442/Services/UniversalDataSetService.svc?wsdl";

    /**
     * Test connection url
     *
     * @var string
     */
    private $urlTest = "https://test.viis.lv:442/Services/UniversalDataSetService.svc?wsdl";

    /**
     * Connection user
     *
     * @var string
     */
    private $user;

    /**
     * Connection password
     *
     * @var string
     */
    private $pass;

    /**
     * Connection ns
     *
     * @var string
     */
    private $ns;

    /**
     * VIISSchools constructor.
     *
     * @param string      $user
     * @param string      $pass
     * @param string|null $ns
     */
    public function __construct(string $user, string $pass, string $ns = null)
    {
        $this->user = $user;
        $this->pass = $pass;
        $this->ns   = $ns;
    }

    /**
     * Destroy SOAP connection
     */
    public function __destruct()
    {
        unset($this->soapClient);
    }

    /**
     * Set test environment
     */
    public function setTestEnvironment()
    {
        $this->environment = "TEST";
    }

    /**
     * Sets desired return type. Default: ReturnType::JSON
     *
     * @param string $returnType
     *
     * @throws \Exception
     */
    public function setReturnType(string $returnType)
    {
        // check if correct return requested
        if (!in_array($returnType, ReturnType::allTypesArray())) {
            throw new \Exception("Return type must be a valid ReturnType object constant: ".$this->getReturnTypes());
        }

        $this->returnType = $returnType;
    }

    /**
     * Retrieve environment type
     *
     * @return string
     */
    public function getEnvironment()
    {
        return $this->environment;
    }

    /**
     * Return const list of available return types
     *
     * @return string
     */
    public function getReturnTypes()
    {
        return \json_encode(ReturnType::allTypesArray(false));
    }

    /**
     * Fetches schools
     *
     * @return array|false|string
     * @throws \SoapFault
     */
    public function getSchoolList()
    {
        // check if correct return requested
        if (!in_array($this->returnType, ReturnType::allTypesArray())) {
            throw new \Exception("Return type must a valid ReturnType object constant: ".$this->getReturnTypes());
        }

        $schools = $this->fetchAllSchools();
        return $this->returnSchools($schools);
    }

    /**
     * Initialize SOAP client and headers
     *
     * @throws \SoapFault
     */
    private function initSoapAndHeaders()
    {
        // Initialize client
        $this->soapClient = new \SoapClient(
            $this->environment == "LIVE" ? $this->urlLive : $this->urlTest,
            [
                "trace"      => true,
                "exceptions" => 1,
            ]
        );

        // Make headers
        $wsse_header = new WsseAuthHeader($this->user, $this->pass, $this->ns);

        // Prepare for execution
        $this->soapClient->__setSoapHeaders([$wsse_header]);
    }

    /**
     * Fetch schools
     *
     * @return mixed
     * @throws \SoapFault
     */
    private function fetchAllSchools()
    {
        // Open soap connection
        $this->initSoapAndHeaders();

        // Try connection
        try {
            $respObj = $this->soapClient->Execute([
                'dataSetFilterCode'  => 'Iestazu_info',
            ]);
        } catch (\SoapFault $e) {
            throw new \Exception($e);
        }

        // Fetch schools list
        return reset($respObj)->any;
    }

    /**
     * Prepares school data in requested format and returns
     *
     * @param $schools
     *
     * @return array|false|string
     */
    private function returnSchools($schools)
    {
        // Return requested return type
        switch ($this->returnType) {
            default:
            case ReturnType::JSON:
                header('Content-type: application/json');
                return json_encode(Builder::makeArray($schools));
            case ReturnType::XML:
                header('Content-Type: text/xml');
                return $schools;
            case ReturnType::ARRAY:
                return Builder::makeArray($schools);
            case ReturnType::OBJECT:
                return Builder::makeObject($schools);
        }
    }
}