SoapProvider.php 5.97 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Provider;

4 5
/*
 * This file is part of the TYPO3 CMS project.
flyguide's avatar
flyguide committed
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.
flyguide's avatar
flyguide committed
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
flyguide's avatar
flyguide committed
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
flyguide's avatar
flyguide committed
16 17 18 19

/**
 * Extension provider for soap requests
 */
20
class SoapProvider extends \T3o\TerFe2\Provider\AbstractProvider
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
{

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

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

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

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

    /**
44
     * @var \T3o\TerFe2\Service\SoapService
45 46 47 48 49 50
     */
    protected $soapService;

    /**
     * Initialize provider
     *
51
     * @throws \Exception
52 53 54 55
     */
    public function initializeProvider()
    {
        if (empty($this->configuration['wsdlUrl'])) {
56
            throw new \Exception('No wsdl url configured');
57 58 59 60
        }

        $username = (empty($this->configuration['username']) ? $this->configuration['username'] : '');
        $password = (empty($this->configuration['password']) ? $this->configuration['password'] : '');
61 62
        $httpAuth['login'] = $this->configuration['httpAuth']['login'] ?: '';
        $httpAuth['password'] = $this->configuration['httpAuth']['password'] ?: '';
63
        $this->soapService = $this->objectManager->get(\T3o\TerFe2\Service\SoapService::class);
64
        $this->soapService->connect($this->configuration['wsdlUrl'], $username, $password, $httpAuth);
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

        // Set getExtensionsFunc
        if (!empty($this->configuration['getExtensionsFunc'])) {
            $this->getExtensionsFunc = $this->configuration['getExtensionsFunc'];
        }

        // Set getFileUrlFunc
        if (!empty($this->configuration['getFileUrlFunc'])) {
            $this->getFileUrlFunc = $this->configuration['getFileUrlFunc'];
        }

        // Set getFileNameFunc
        if (!empty($this->configuration['getFileNameFunc'])) {
            $this->getFileNameFunc = $this->configuration['getFileNameFunc'];
        }

        // Set getDownloadCountFunc
        if (!empty($this->configuration['getDownloadCountFunc'])) {
            $this->getDownloadCountFunc = $this->configuration['getDownloadCountFunc'];
        }
    }

    /**
     * Returns all extensions since last run
     *
90 91 92
     * @param int $lastRun Timestamp of last update
     * @param int $offset Offset to start with
     * @param int $count Extension count to load
93
     * @return array Extension rows
94
     * @throws \Exception
95 96 97 98
     */
    public function getExtensions($lastRun, $offset, $count)
    {
        if (empty($this->getExtensionsFunc)) {
99
            throw new \Exception('No configuration for "getExtensionsFunc" found');
100
        }
101
        $parameters = [
102 103 104
            'lastRun' => (int)$lastRun,
            'offset' => (int)$offset,
            'count' => (int)$count,
105
        ];
106
        $result = $this->soapService->__call($this->getExtensionsFunc, $parameters);
107
        return !empty($result['extensions']) ? $result['extensions'] : [];
108 109 110 111 112
    }

    /**
     * Returns the url to an extension related file
     *
113
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
114 115
     * @param string $fileType File type
     * @return string Url to file
116
     * @throws \Exception
117
     */
118
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
119 120
    {
        if (empty($this->getFileUrlFunc)) {
121
            throw new \Exception('No configuration for "getFileUrlFunc" found');
122
        }
123
        $parameters = [
124 125 126
            'extension' => (string)$version->getExtension()->getExtKey(),
            'version' => (string)$version->getVersionString(),
            'fileType' => (string)$fileType,
127
        ];
128 129
        $result = $this->soapService->__call($this->getFileUrlFunc, $parameters);
        if (empty($result['url'])) {
130
            throw new \Exception('Could not get url to file from soap server');
131 132 133 134 135 136 137
        }
        return (string)$result['url'];
    }

    /**
     * Returns name of an extension related file
     *
138
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
139 140
     * @param string $fileType File type
     * @return string File name
141
     * @throws \Exception
142
     */
143
    public function getFileName(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
144 145
    {
        if (empty($this->getFileNameFunc)) {
146
            throw new \Exception('No configuration for "getFileNameFunc" found');
147
        }
148
        $parameters = [
149 150 151
            'extension' => (string)$version->getExtension()->getExtKey(),
            'version' => (string)$version->getVersionString(),
            'fileType' => (string)$fileType,
152
        ];
153 154
        $result = $this->soapService->__call($this->getFileNameFunc, $parameters);
        if (empty($result['filename'])) {
155
            throw new \Exception('Could not get filename from soap server');
156 157 158 159 160 161 162
        }
        return (string)$result['filename'];
    }

    /**
     * Returns all information about an extension version
     *
163
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
164
     * @return array Version details
165
     * @throws \Exception
166
     */
167
    public function getVersionDetails(\T3o\TerFe2\Domain\Model\Version $version)
168 169
    {
        if (empty($this->getVersionDetailsFunc)) {
170
            throw new \Exception('No configuration for "getVersionDetailsFunc" found');
171
        }
172
        $parameters = [
173 174
            'extension' => (string)$version->getExtension()->getExtKey(),
            'version' => (string)$version->getVersionString(),
175
        ];
176 177
        $result = $this->soapService->__call($this->getVersionDetailsFunc, $parameters);
        if (empty($result) || !is_array($result)) {
178
            throw new \Exception('Could not get version details from soap server');
179 180 181
        }
        return $result;
    }
flyguide's avatar
flyguide committed
182
}