Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaoFrontController
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
20
 legacy
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2014 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\tao\model\routing;
23
24use GuzzleHttp\Psr7\Response;
25use GuzzleHttp\Psr7\ServerRequest;
26use InterruptedActionException;
27use common_ext_ExtensionsManager;
28use common_http_Request;
29use oat\oatbox\service\ServiceManagerAwareInterface;
30use oat\oatbox\service\ServiceManagerAwareTrait;
31use oat\tao\model\session\restSessionFactory\RestSessionFactory;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34
35/**
36 * A simple controller to replace the ClearFw controller
37 *
38 * @author Joel Bout, <joel@taotesting.com>
39 */
40class TaoFrontController implements ServiceManagerAwareInterface
41{
42    use ServiceManagerAwareTrait;
43
44    /**
45     * Resolve the request and enforce the responsible controller
46     *
47     * - Resolve request
48     * - load the extension
49     * - set the context
50     * - load rest session if needed
51     * - load the language
52     * - enforce controller: $controller->$method()
53     *
54     * @param ServerRequestInterface $request
55     * @param ResponseInterface $response
56     * @throws \ActionEnforcingException
57     * @throws \common_exception_Error
58     * @throws \common_exception_InvalidArgumentType
59     * @throws \common_ext_ExtensionException
60     */
61    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
62    {
63        $resolver = new Resolver($request);
64        $this->propagate($resolver);
65
66        // load the responsible extension
67        $ext = $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID)
68            ->getExtensionById($resolver->getExtensionId());
69
70        \Context::getInstance()->setExtensionName($resolver->getExtensionId());
71
72        try {
73            /** @var RestSessionFactory $service */
74            $service = $this->getServiceLocator()->get(RestSessionFactory::SERVICE_ID);
75            $pRequest = common_http_Request::currentRequest();
76            $service->createSessionFromRequest($pRequest, $resolver);
77        } catch (\common_user_auth_AuthFailedException $e) {
78            $data['success']    = false;
79            $data['errorCode']  = '401';
80            $data['errorMsg']   = 'You are not authorized to access this functionality.';
81            $data['version']    = TAO_VERSION;
82
83            header('HTTP/1.0 401 Unauthorized');
84            header('WWW-Authenticate: Basic realm="' . GENERIS_INSTANCE_NAME . '"');
85            echo json_encode($data);
86            exit(0);
87        }
88
89        // load translations
90        $uiLang = \common_session_SessionManager::getSession()->getInterfaceLanguage();
91        \tao_helpers_I18n::init($ext, $uiLang);
92
93        try {
94            if ($request->getMethod() == 'GET') {
95                $parameters = $request->getQueryParams();
96            } else {
97                $parameters = $request->getParsedBody();
98            }
99            $enforcer = new ActionEnforcer(
100                $resolver->getExtensionId(),
101                $resolver->getControllerClass(),
102                $resolver->getMethodName(),
103                $parameters
104            );
105            $this->propagate($enforcer);
106            $enforcer($request, $response);
107        } catch (InterruptedActionException $iE) {
108            // Nothing to do here.
109        }
110    }
111
112    /**
113     * Run the controller
114     *
115     * @deprecated use $this->__invoke() instead
116     *
117     * @param common_http_Request $pRequest
118     * @throws \ActionEnforcingException
119     * @throws \common_exception_Error
120     * @throws \common_exception_InvalidArgumentType
121     * @throws \common_ext_ExtensionException
122     */
123    public function legacy(common_http_Request $pRequest)
124    {
125        $request = ServerRequest::fromGlobals();
126        $response = new Response();
127        $this($request, $response);
128    }
129}