Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.88% covered (danger)
5.88%
2 / 34
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequiredActionRedirectUrlPart
5.88% covered (danger)
5.88%
2 / 34
20.00% covered (danger)
20.00%
1 / 5
47.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 __toPhpCode
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getTransformedUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExcludedRoutes
0.00% covered (danger)
0.00%
0 / 8
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) 2015 (original work) Open Assessment Technologies SA;
19 *
20 *
21 */
22
23namespace oat\tao\model\requiredAction\implementation;
24
25use oat\tao\model\requiredAction\RequiredActionAbstract;
26use oat\tao\model\routing\FlowController;
27
28/**
29 * Class RequiredAction
30 *
31 * RequiredAction is action which should be executed by user before performing any activities in the TAO
32 *
33 * @package oat\tao\model\requiredAction\implementation
34 */
35class RequiredActionRedirectUrlPart extends RequiredActionAbstract
36{
37    /**
38     * Route to be ignored
39     *
40     * @var array
41     */
42    protected $excludedRoutes = [
43        [
44            'extension' => 'tao',
45            'module' => 'ClientConfig',
46            'action' => 'config',
47        ]
48    ];
49
50    /**
51     * Array of url parts
52     *
53     * @var array
54     */
55    protected $url;
56
57    /**
58     * RequiredActionRedirectUrlPart constructor.
59     * @param string $name
60     * @param array $rules
61     * @param array $url
62     */
63    public function __construct($name, array $rules, array $url)
64    {
65        parent::__construct($name, $rules);
66        $this->url = $url;
67    }
68
69    /**
70     * Execute an action
71     *
72     * @param array $params
73     * @return string The callback url
74     */
75    public function execute(array $params = [])
76    {
77        $context = \Context::getInstance();
78        $excludedRoutes = $this->getExcludedRoutes();
79        $currentRoute = [
80            'extension' => $context->getExtensionName(),
81            'module' => $context->getModuleName(),
82            'action' => $context->getActionName(),
83        ];
84
85        if (! in_array($currentRoute, $excludedRoutes)) {
86            $currentUrl = \common_http_Request::currentRequest()->getUrl();
87
88            $transformedUrl = $this->getTransformedUrl($params);
89            $url = $transformedUrl . (parse_url($transformedUrl, PHP_URL_QUERY) ? '&' : '?') . 'return_url='
90                . urlencode($currentUrl);
91
92            $flowController = new FlowController();
93            $flowController->redirect($url);
94        }
95    }
96
97    /**
98     * @see \oat\oatbox\PhpSerializable::__toPhpCode()
99     */
100    public function __toPhpCode()
101    {
102        $class = get_class($this);
103        $name = $this->name;
104        $rules = \common_Utils::toHumanReadablePhpString($this->getRules());
105        $url = \common_Utils::toHumanReadablePhpString($this->url);
106        return "new $class(
107            '$name',
108            $rules,
109            $url
110        )";
111    }
112
113    /**
114     * Get url string from $this->url
115     *
116     * @param array $params
117     * @return string
118     */
119    protected function getTransformedUrl(array $params = [])
120    {
121        return call_user_func_array('_url', array_merge($this->url, [$params]));
122    }
123
124    /**
125     * Some actions should not be redirected (such as retrieving requireJs config)
126     *
127     * @return array
128     */
129    protected function getExcludedRoutes()
130    {
131        $result = $this->excludedRoutes;
132        $resolver = new \Resolver($this->getTransformedUrl());
133
134        $result[] = [
135            'extension' => $resolver->getExtensionFromURL(),
136            'module' => $resolver->getModule(),
137            'action' => $resolver->getAction(),
138        ];
139
140        return $result;
141    }
142}