Completed
Push — master ( 6114e5...8f259d )
by Aimeos
07:23
created

JqadmController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 165
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 6
dl 20
loc 165
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A copyAction() 0 5 1
A createAction() 0 5 1
A deleteAction() 0 5 1
A getAction() 0 5 1
A saveAction() 0 5 2
A searchAction() 0 5 1
A createClient() 0 15 1
A getHtml() 0 7 1
A setLocale() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package symfony
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\ShopBundle\Controller;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
17
18
/**
19
 * Aimeos controller for the JQAdm admin interface
20
 *
21
 * @package symfony
22
 * @subpackage Controller
23
 */
24
class JqadmController extends Controller
25
{
26
	/**
27
	 * Returns the HTML code for a copy of a resource object
28
	 *
29
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
30
	 * @param string $resource Resource location, e.g. "product"
31
	 * @param integer $id Unique resource ID
32
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
	 * @return string Generated output
34
	 */
35
	public function copyAction( Request $request, $resource, $id, $site = 'default' )
36
	{
37
		$cntl = $this->createClient( $request, $site, $resource );
38
		return $this->getHtml( $site, $cntl->copy( $id ) );
0 ignored issues
show
Unused Code introduced by
The call to Iface::copy() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
	}
40
41
42
	/**
43
	 * Returns the HTML code for a new resource object
44
	 *
45
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
46
	 * @param string $resource Resource location, e.g. "product"
47
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
	 * @return string Generated output
49
	 */
50
	public function createAction( Request $request, $resource, $site = 'default' )
51
	{
52
		$cntl = $this->createClient( $request, $site, $resource );
53
		return $this->getHtml( $site, $cntl->create() );
54
	}
55
56
57
	/**
58
	 * Deletes the resource object or a list of resource objects
59
	 *
60
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
61
	 * @param string $resource Resource location, e.g. "product"
62
	 * @param integer $id Unique resource ID
63
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
	 * @return string Generated output
65
	 */
66
	public function deleteAction( Request $request, $resource, $id, $site = 'default' )
67
	{
68
		$cntl = $this->createClient( $request, $site, $resource );
69
		return $this->getHtml( $site, $cntl->delete( $id ) . $cntl->search() );
0 ignored issues
show
Unused Code introduced by
The call to Iface::delete() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
	}
71
72
73
	/**
74
	 * Returns the HTML code for the requested resource object
75
	 *
76
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
77
	 * @param string $resource Resource location, e.g. "product"
78
	 * @param integer $id Unique resource ID
79
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
80
	 * @return string Generated output
81
	 */
82
	public function getAction( Request $request, $resource, $id, $site = 'default' )
83
	{
84
		$cntl = $this->createClient( $request, $site, $resource );
85
		return $this->getHtml( $site, $cntl->get( $id ) );
0 ignored issues
show
Unused Code introduced by
The call to Iface::get() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
	}
87
88
89
	/**
90
	 * Saves a new resource object
91
	 *
92
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
93
	 * @param string $resource Resource location, e.g. "product"
94
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
	 * @return string Generated output
96
	 */
97
	public function saveAction( Request $request, $resource, $site = 'default' )
98
	{
99
		$cntl = $this->createClient( $request, $site, $resource );
100
		return $this->getHtml( $site, ( $cntl->save() ? : $cntl->search() ) );
101
	}
102
103
104
	/**
105
	 * Returns the HTML code for a list of resource objects
106
	 *
107
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
108
	 * @param string $resource Resource location, e.g. "product"
109
	 * @param string $sitecode Unique site code
0 ignored issues
show
Bug introduced by
There is no parameter named $sitecode. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
	 * @return string Generated output
111
	 */
112
	public function searchAction( Request $request, $resource, $site = 'default' )
113
	{
114
		$cntl = $this->createClient( $request, $site, $resource );
115
		return $this->getHtml( $site, $cntl->search() );
116
	}
117
118
119
	/**
120
	 * Returns the resource controller
121
	 *
122
	 * @param Symfony\Component\HttpFoundation\Request $request Symfony request object
123
	 * @param string $sitecode Unique site code
124
	 * @param string Resource location, e.g. "product"
125
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
126
	 */
127
	protected function createClient( Request $request, $sitecode, $resource )
128
	{
129
		$lang = $request->get( 'lang', 'en' );
130
131
		$aimeos = $this->get( 'aimeos' )->get();
132
		$templatePaths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
133
134
		$context = $this->get( 'aimeos_context' )->get( false );
135
		$context = $this->setLocale( $context, $sitecode, $lang );
136
137
		$view = $this->get( 'aimeos_view' )->create( $context->getConfig(), $templatePaths, $lang );
138
		$context->setView( $view );
139
140
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $templatePaths, $resource );
141
	}
142
143
144
	/**
145
	 * Returns the generated HTML code
146
	 *
147
	 * @param string $site Unique site code
148
	 * @param string $content Content from admin client
149
	 * @return \Illuminate\Contracts\View\View View for rendering the output
150
	 */
151
	protected function getHtml( $site, $content )
0 ignored issues
show
Unused Code introduced by
The parameter $site is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
	{
153
		$version = $this->get( 'aimeos' )->getVersion();
154
		$content = str_replace( array( '{type}', '{version}' ), array( 'Symfony', $version ), $content );
155
156
		return $this->render( 'AimeosShopBundle:Jqadm:index.html.twig', array( 'content' => $content ) );
157
	}
158
159
160
	/**
161
	 * Sets the locale item in the given context
162
	 *
163
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
164
	 * @param string $sitecode Unique site code
165
	 * @param string $lang ISO language code, e.g. "en" or "en_GB"
166
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
167
	 */
168 View Code Duplication
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $sitecode = 'default', $lang = null )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
	{
170
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
171
172
		try
173
		{
174
			$localeItem = $localeManager->bootstrap( $sitecode, '', '', false );
175
			$localeItem->setLanguageId( null );
176
			$localeItem->setCurrencyId( null );
177
		}
178
		catch( \Aimeos\MShop\Locale\Exception $e )
179
		{
180
			$localeItem = $localeManager->createItem();
181
		}
182
183
		$context->setLocale( $localeItem );
184
		$context->setI18n( $this->get( 'aimeos_i18n' )->get( array( $lang ) ) );
185
186
		return $context;
187
	}
188
}
189