AbstractTerBasedController.php 3.2 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Controller;

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

/**
 * Abstract controller
 */
20
abstract class AbstractTerBasedController extends \T3o\TerFe2\Controller\AbstractController
21 22 23 24 25
{

    /**
     * @var array
     */
26
    protected $frontendUser = [];
27 28

    /**
29
     * @var \T3o\TerFe2\Service\TerService
30 31 32 33 34 35
     */
    protected $terConnection;

    /**
     * @var array
     */
36
    protected $terSettings = [];
37 38 39 40

    /**
     * @var array
     */
41
    protected $terAccount = [];
42 43 44 45 46 47 48

    /**
     * Initialize action
     */
    public function initializeAction()
    {
        parent::initializeAction();
49 50
        $this->frontendUser = (!empty($GLOBALS['TSFE']->fe_user->user) ? $GLOBALS['TSFE']->fe_user->user : []);
        $this->terSettings = (!empty($this->settings['terConnection']) ? $this->settings['terConnection'] : []);
51 52 53 54 55 56 57
        $this->terAccount = $this->getTerAccount();
        $this->terConnection = $this->getTerConnection();
    }

    /**
     * Initializes the view, add login state to template variables
     *
58
     * @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view
59
     */
60
    public function initializeView(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view)
61
    {
62
        $this->view = $view;
Markus Sommer's avatar
Markus Sommer committed
63
        $this->view->assign('loggedIn', false);
64
        if (!empty($this->frontendUser)) {
Markus Sommer's avatar
Markus Sommer committed
65
            $this->view->assign('loggedIn', true);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            $this->view->assign('userName', $this->frontendUser['username']);
            $this->view->assign('userId', $this->frontendUser['uid']);
        }
    }

    /**
     * Returns the account data from current frontend user
     *
     * @return array Frontend user account data
     */
    protected function getTerAccount()
    {
        $username = $this->frontendUser['username'];
        $password = $this->frontendUser['password'];
        if (!empty($this->terSettings['username']) && !empty($this->terSettings['password'])) {
            $username = $this->terSettings['username'];
            $password = $this->terSettings['password'];
        }
84
        return [
85 86
            'username' => $username,
            'password' => $password,
87
        ];
88 89 90 91 92
    }

    /**
     * Create a connection to the TER server
     *
93
     * @return \T3o\TerFe2\Service\TerService The TER connection
94
     * @throws \Exception
95 96 97 98 99
     */
    protected function getTerConnection()
    {
        // Check the wsdl uri
        if (empty($this->terSettings['wsdl'])) {
100
            throw new \Exception('No wsdl set to connect to TER server');
101 102
        }

103 104 105 106
        if (empty($this->terSettings['httpAuth'])) {
            $this->terSettings['httpAuth'] = [];
        }

107 108
        // Create connection
        $wsdl = $this->terSettings['wsdl'];
109
        return $this->objectManager->get(\T3o\TerFe2\Service\TerService::class, $wsdl, $this->terAccount['username'], $this->terAccount['password'], $this->terSettings['httpAuth']);
110 111
    }
}