Completed
Push — master ( b23d1e...03b6f4 )
by Jean-Christophe
04:33
created

JsUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 62
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 12 3
A addViewElement() 0 6 2
A createScriptVariable() 0 3 1
A forward() 0 9 1
A renderContent() 0 11 3
A fromDispatcher() 0 8 2
1
<?php
2
3
namespace Ajax\php\symfony;
4
5
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\HttpKernelInterface;
8
use Ajax\service\JString;
9
class JsUtils extends \Ajax\JsUtils{
10
	public function getUrl($url){
11
		//$request = Request::createFromGlobals();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
12
		$router=$this->getInjected();
13
		if(isset($router)===true){
14
			try {
15
			$url=$router->generate($url);
16
			}catch (\Exception $e){
17
				return $router->getContext()->getBaseUrl();
18
			}
19
		}
20
		return $url;
21
	}
22
	public function addViewElement($identifier,$content,&$view){
23
		if(\array_key_exists("q", $view)===false){
24
			$view["q"]=array();
25
		}
26
		$view["q"][$identifier]=$content;
27
	}
28
29
	public function createScriptVariable(&$view,$view_var, $output){
30
		$view[$view_var]=$output;
31
	}
32
33
	/**
34
	 * @param Symfony\Component\DependencyInjection\ContainerInterface $initialControllerInstance
35
	 * @param string $controllerName
36
	 * @param string $actionName
37
	 * @param array $params
38
	 * @see \Ajax\JsUtils::forward()
39
	 */
40
	public function forward($initialControllerInstance,$controllerName,$actionName,$params=array()){
41
		$path=$params;
42
		$request = $initialControllerInstance->get('request_stack')->getCurrentRequest();
43
		$path['_forwarded'] = $request->attributes;
44
		$path['_controller'] = $controllerName.":".$actionName;
45
		$subRequest = $request->duplicate([], null, $path);
46
		$response= $initialControllerInstance->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
47
		return $response->getContent();
48
	}
49
50
	public function renderContent($initialControllerInstance,$viewName, $params=NULL) {
51
        if ($initialControllerInstance->has('templating')) {
52
            return $initialControllerInstance->get('templating')->render($viewName, $params);
53
        }
54
55
        if (!$initialControllerInstance->has('twig')) {
56
            throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available.');
57
        }
58
59
        return $initialControllerInstance->get('twig')->render($viewName, $params);
60
	}
61
62
	public function fromDispatcher($dispatcher){
63
		$request = $dispatcher->get('request_stack')->getCurrentRequest();
64
		$uri=$request->getPathInfo();
65
		if(JString::startswith($uri, "/")){
66
			$uri=\substr($uri, 1);
67
		}
68
		return \explode("/", $uri);
69
	}
70
}