TerService.php 7.18 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Service;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16 17 18 19

/**
 * Service for TER operations
 */
20
class TerService
21 22 23 24 25
{

    /**
     * @var array
     */
26
    protected $userData = [];
27 28 29 30 31 32 33

    /**
     * @var string
     */
    protected $wsdlUrl;

    /**
34
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
35 36 37 38
     */
    protected $objectManager;

    /**
39
     * @var \T3o\TerFe2\Service\SoapService
40 41 42 43 44 45
     */
    protected $soapService;

    /**
     * Load TER connection
     *
46 47 48 49
     * @param string $wsdlUrl
     * @param string $username
     * @param string $password
     * @param array $httpAuth
50
     */
51
    public function __construct($wsdlUrl, $username, $password, array $httpAuth = [])
52 53
    {
        $this->wsdlUrl = $wsdlUrl;
54
        $this->userData = [
55 56
            'username' => $username,
            'password' => $password,
57
        ];
58
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
59
        $this->soapService = $objectManager->get(\T3o\TerFe2\Service\SoapService::class);
Markus Sommer's avatar
Markus Sommer committed
60
        $this->soapService->connect($this->wsdlUrl, '', '', true, $httpAuth);
61 62 63 64 65 66
    }

    /**
     * Check if an extension key is valid
     *
     * @param string $extensionKey Extension key
67
     * @param string $error Error key
68
     * @return bool TRUE if extension key is valid
69 70 71 72 73 74 75 76
     */
    public function checkExtensionKey($extensionKey, &$error)
    {
        $result = $this->soapService->checkExtensionKey($this->userData, $extensionKey);

        // if the result is empty
        if (empty($result['resultCode'])) {
            $error = 'result_empty';
Markus Sommer's avatar
Markus Sommer committed
77
            return false;
78 79
        }

80 81 82
        // result code invalid 10502 = TX_TER_RESULT_EXTENSIONKEYNOTVALID
        if ($result['resultCode'] === '10502') {
            $error = 'key_invalid';
Markus Sommer's avatar
Markus Sommer committed
83
            return false;
84 85 86 87
        }
        // key exists 10500 = TX_TER_RESULT_EXTENSIONKEYALREADYEXISTS
        if ($result['resultCode'] === '10500') {
            $error = 'key_exists';
Markus Sommer's avatar
Markus Sommer committed
88
            return false;
89 90 91
        }

        // 10501 = TX_TER_RESULT_EXTENSIONKEYDOESNOTEXIST
92
        return !empty($result['resultCode']) && $result['resultCode'] === '10501';
93 94 95 96 97 98
    }

    /**
     * Register extension
     *
     * @param array $extensionData Extension information
99
     * @return bool TRUE if success
100 101 102 103 104 105 106
     */
    public function registerExtension(array $extensionData)
    {
        $result = $this->soapService->registerExtensionKey($this->userData, $extensionData);

        // if the result is empty
        if (empty($result['resultCode'])) {
Markus Sommer's avatar
Markus Sommer committed
107
            return false;
108 109 110 111
        }

        // result code invalid 10502 = TX_TER_RESULT_EXTENSIONKEYNOTVALID
        if ($result['resultCode'] === '10502') {
Markus Sommer's avatar
Markus Sommer committed
112
            return false;
113 114 115
        }
        // key exists 10500 = TX_TER_RESULT_EXTENSIONKEYALREADYEXISTS
        if ($result['resultCode'] === '10500') {
Markus Sommer's avatar
Markus Sommer committed
116
            return false;
117 118 119
        }

        // 10503 = TX_TER_RESULT_EXTENSIONKEYSUCCESSFULLYREGISTERED
120
        return !empty($result['resultCode']) && $result['resultCode'] === '10503';
121 122 123 124 125 126 127 128
    }

    /**
     * Assign extension key to an other user
     *
     * @param string $extensionKey Extension key
     * @param string $username New username
     * @param string $error Contains the error
129
     * @return bool TRUE if success
130 131 132
     */
    public function assignExtensionKey($extensionKey, $username, &$error = '')
    {
133
        $result = $this->soapService->modifyExtensionKey($this->userData, [
134 135
            'extensionKey' => $extensionKey,
            'ownerUsername' => $username,
136
        ]);
137 138 139

        if (empty($result['resultCode'])) {
            $error = 'no_result';
Markus Sommer's avatar
Markus Sommer committed
140
            return false;
141 142 143 144
        }
        // 102 = TX_TER_ERROR_GENERAL_USERNOTFOUND
        if ($result['resultCode'] === '102') {
            $error = 'user_not_found';
Markus Sommer's avatar
Markus Sommer committed
145
            return false;
146 147 148 149 150
        }

        // 102 = TX_TER_ERROR_GENERAL_USERNOTFOUND
        if ($result['resultCode'] === '102') {
            $error = 'user_not_found';
Markus Sommer's avatar
Markus Sommer committed
151
            return false;
152 153 154 155 156
        }

        // 602 = TX_TER_ERROR_MODIFYEXTENSIONKEY_KEYDOESNOTEXIST
        if ($result['resultCode'] === '602') {
            $error = 'key_not_found';
Markus Sommer's avatar
Markus Sommer committed
157
            return false;
158 159 160
        }

        // 10000 = TX_TER_RESULT_GENERAL_OK
161
        return $result['resultCode'] === '10000';
162 163 164 165 166 167
    }

    /**
     * Remove an extension kex from system
     *
     * @param string $extensionKey Extension key
168
     * @return bool TRUE if success
169 170 171 172 173
     */
    public function deleteExtensionKey($extensionKey)
    {
        $result = $this->soapService->deleteExtensionKey($this->userData, $extensionKey);
        // 10000 = TX_TER_RESULT_GENERAL_OK
174
        return !empty($result['resultCode']) && $result['resultCode'] === '10000';
175 176 177 178 179 180 181 182 183 184 185 186 187
    }

    /**
     * Removes an extension version from the TER
     *
     * @param $extensionKey
     * @param $versionString
     * @return bool
     */
    public function deleteExtensionVersion($extensionKey, $versionString)
    {
        $result = $this->soapService->deleteExtension($this->userData, $extensionKey, $versionString);
        // 10505 = TX_TER_RESULT_EXTENSIONSUCCESSFULLYDELETED
188
        return !empty($result['resultCode']) && $result['resultCode'] === '10505';
189 190 191 192 193 194 195
    }

    /**
     * Set review state of an extension version
     *
     * @param string $extensionKey The extension key
     * @param string $versionString Version as string
196
     * @param int $reviewState New review state
197
     * @param string $error Contains the error
198
     * @return bool TRUE if success
199 200 201
     */
    public function setReviewState($extensionKey, $versionString, $reviewState, &$error = '')
    {
202
        $parameters = [
203 204 205
            'extensionKey' => $extensionKey,
            'version' => $versionString,
            'reviewState' => $reviewState,
206
        ];
207 208 209

        try {
            $this->soapService->setReviewState($this->userData, $parameters);
210
        } catch (\SoapFault $exception) {
211
            $error = $exception->faultstring;
Markus Sommer's avatar
Markus Sommer committed
212
            return false;
213 214
        }

Markus Sommer's avatar
Markus Sommer committed
215
        return true;
216 217 218 219 220 221 222 223 224 225
    }

    /**
     * Returns an array of the users extensions
     *
     * @param $error
     * @return bool
     */
    public function getExtensionKeysByUser(&$error)
    {
226
        $parameter = ['username' => $this->userData['username']];
227 228 229 230 231 232
        $response = $this->soapService->getExtensionKeys($this->userData, $parameter);
        $result = $response['simpleResult'];
        $extensionKeys = $response['extensionKeyData'];

        if (empty($result['resultCode'])) {
            $error = 'no_result';
Markus Sommer's avatar
Markus Sommer committed
233
            return false;
234 235 236 237
        }
        // 102 = TX_TER_ERROR_GENERAL_USERNOTFOUND
        if ($result['resultCode'] === '102') {
            $error = 'user_not_found';
Markus Sommer's avatar
Markus Sommer committed
238
            return false;
239 240 241 242
        }

        if ($result['resultCode'] !== '10000') {
            $error = 'user_not_found';
Markus Sommer's avatar
Markus Sommer committed
243
            return false;
244
        }
245
        return $extensionKeys;
246 247
    }
}