Skip to content
Snippets Groups Projects
ClientFactory.php 3.12 KiB
Newer Older
 * Copyright (C) 2020 Leipzig University Library
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author  Sebastian Kehr <kehr@ub.uni-leipzig.de>
 * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
 */

namespace fid\Service;

use Psr\Container\ContainerInterface;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Symfony\Component\Serializer\SerializerInterface;
use VuFind\Config\PluginManager;
use VuFind\Cookie\CookieManager;
use Zend\Config\Config;
use Zend\Session\Container as Session;
use Zend\Session\SessionManager;

class ClientFactory
{

    protected const ERROR_CONFIG_FILE = 'Invalid configuration file "%s.ini".';

    /**
     * @param ContainerInterface $container
     *
     * @return Client
     * @throws ClientException
     */
    public function __invoke(ContainerInterface $container): Client
    {
        $config = $container->get(PluginManager::class)
            ->get(self::CONFIG_KEY)->Client;

        if (!$config instanceof Config || !$config->baseUrl) {
            $message = sprintf(self::ERROR_CONFIG_FILE, self::CONFIG_KEY);
            throw new ClientException($message);
        }

        $baseUrl = $container->get(BaseUrlListener::class)->getBaseUrl();
        $configArray = compact('baseUrl') + $config->toArray();

        /** @var SessionManager $sessionManager */
        $sessionManager = $container->get(SessionManager::class);
        $session = new Session(Client::class, $sessionManager);
        /** @var CookieManager $cookies */
        $cookies = $container->get(CookieManager::class);
        /** @var SerializerInterface $serializer */
        $serializer = $container->get(SerializerInterface::class);
        /** @var HttpClientInterface $httpClient */
        $httpClient = $container->get(HttpClientInterface::class);
        /** @var UriFactoryInterface $uriFactory */
        $uriFactory = $container->get(UriFactoryInterface::class);
        /** @var StreamFactoryInterface $streamFactory */
        $streamFactory = $container->get(StreamFactoryInterface::class);
        /** @var RequestFactoryInterface $requestFactory */
        $requestFactory = $container->get(RequestFactoryInterface::class);

        return new Client(
            $configArray, $session, $cookies, $serializer, $httpClient,
            $uriFactory, $streamFactory, $requestFactory
        );