Completed
Push — master ( aa9489...67c4a6 )
by Aimeos
02:16
created

JqadmController::saveAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Shop\Controller;
12
13
use Illuminate\Support\Facades\View;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Input;
16
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
17
18
19
/**
20
 * Aimeos controller for the JQuery admin interface
21
 *
22
 * @package laravel
23
 * @subpackage Controller
24
 */
25
class JqadmController extends AdminController
26
{
27
	use AuthorizesRequests;
28
29
30
	/**
31
	 * Returns the JS file content
32
	 *
33
	 * @return \Illuminate\Http\Response Response object containing the generated output
34
	 */
35
	public function fileAction()
36
	{
37
		if( config( 'shop.authorize', true ) ) {
38
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
39
		}
40
41
		$contents = '';
42
		$files = array();
43
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
44
		$type = Route::input( 'type', Input::get( 'type', 'js' ) );
45
46
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
47
		{
48
			foreach( $paths as $path )
49
			{
50
				$jsbAbsPath = $base . '/' . $path;
51
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
52
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
53
			}
54
		}
55
56
		foreach( $files as $file )
57
		{
58
			if( ( $content = file_get_contents( $file ) ) !== false ) {
59
				$contents .= $content;
60
			}
61
		}
62
63
		$response = response( $contents );
64
65
		if( $type === 'js' ) {
66
			$response->header( 'Content-Type', 'application/javascript' );
0 ignored issues
show
Bug introduced by
The method header does only exist in Illuminate\Http\Response, but not in Illuminate\Contracts\Routing\ResponseFactory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
		} elseif( $type === 'css' ) {
68
			$response->header( 'Content-Type', 'text/css' );
69
		}
70
71
		return $response;
72
	}
73
74
75
	/**
76
	 * Returns the HTML code for a copy of a resource object
77
	 *
78
	 * @return string Generated output
79
	 */
80 View Code Duplication
	public function copyAction()
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...
81
	{
82
		if( config( 'shop.authorize', true ) ) {
83
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
84
		}
85
86
		$cntl = $this->createAdmin();
87
88
		if( ( $html = $cntl->copy() ) == '' ) {
89
			return $cntl->getView()->response();
90
		}
91
92
		return $this->getHtml( $html );
93
	}
94
95
96
	/**
97
	 * Returns the HTML code for a new resource object
98
	 *
99
	 * @return string Generated output
100
	 */
101 View Code Duplication
	public function createAction()
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...
102
	{
103
		if( config( 'shop.authorize', true ) ) {
104
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
105
		}
106
107
		$cntl = $this->createAdmin();
108
109
		if( ( $html = $cntl->create() ) == '' ) {
110
			return $cntl->getView()->response();
111
		}
112
113
		return $this->getHtml( $html );
114
	}
115
116
117
	/**
118
	 * Deletes the resource object or a list of resource objects
119
	 *
120
	 * @return string Generated output
121
	 */
122 View Code Duplication
	public function deleteAction()
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...
123
	{
124
		if( config( 'shop.authorize', true ) ) {
125
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
126
		}
127
128
		$cntl = $this->createAdmin();
129
130
		if( ( $html = $cntl->delete() ) == '' ) {
131
			return $cntl->getView()->response();
132
		}
133
134
		return $this->getHtml( $html );
135
	}
136
137
138
	/**
139
	 * Exports the data for a resource object
140
	 *
141
	 * @return string Generated output
142
	 */
143 View Code Duplication
	public function exportAction()
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...
144
	{
145
		if( config( 'shop.authorize', true ) ) {
146
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
147
		}
148
149
		$cntl = $this->createAdmin();
150
151
		if( ( $html = $cntl->export() ) == '' ) {
152
			return $cntl->getView()->response();
153
		}
154
155
		return $this->getHtml( $html );
156
	}
157
158
159
	/**
160
	 * Returns the HTML code for the requested resource object
161
	 *
162
	 * @return string Generated output
163
	 */
164 View Code Duplication
	public function getAction()
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...
165
	{
166
		if( config( 'shop.authorize', true ) ) {
167
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
168
		}
169
170
		$cntl = $this->createAdmin();
171
172
		if( ( $html = $cntl->get() ) == '' ) {
173
			return $cntl->getView()->response();
174
		}
175
176
		return $this->getHtml( $html );
177
	}
178
179
180
	/**
181
	 * Saves a new resource object
182
	 *
183
	 * @return string Generated output
184
	 */
185 View Code Duplication
	public function saveAction()
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...
186
	{
187
		if( config( 'shop.authorize', true ) ) {
188
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
189
		}
190
191
		$cntl = $this->createAdmin();
192
193
		if( ( $html = $cntl->save() ) == '' ) {
194
			return $cntl->getView()->response();
195
		}
196
197
		return $this->getHtml( $html );
198
	}
199
200
201
	/**
202
	 * Returns the HTML code for a list of resource objects
203
	 *
204
	 * @return string Generated output
205
	 */
206 View Code Duplication
	public function searchAction()
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...
207
	{
208
		if( config( 'shop.authorize', true ) ) {
209
			$this->authorize( 'admin', [JqadmController::class, ['admin', 'editor']] );
210
		}
211
212
		$cntl = $this->createAdmin();
213
214
		if( ( $html = $cntl->search() ) == '' ) {
215
			return $cntl->getView()->response();
216
		}
217
218
		return $this->getHtml( $html );
219
	}
220
221
222
	/**
223
	 * Returns the resource controller
224
	 *
225
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
226
	 */
227
	protected function createAdmin()
228
	{
229
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
230
		$lang = Input::get( 'lang', config( 'app.locale', 'en' ) );
231
		$resource = Route::input( 'resource' );
232
233
		$aimeos = app( '\Aimeos\Shop\Base\Aimeos' )->get();
234
		$paths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
235
236
		$context = app( '\Aimeos\Shop\Base\Context' )->get( false, 'backend' );
237
		$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $lang, 'en' ) ) );
238
		$context->setLocale( app('\Aimeos\Shop\Base\Locale')->getBackend( $context, $site ) );
239
240
		$view = app( '\Aimeos\Shop\Base\View' )->create( $context, $paths, $lang );
241
242
		$view->aimeosType = 'Laravel';
243
		$view->aimeosVersion = app( '\Aimeos\Shop\Base\Aimeos' )->getVersion();
244
		$view->aimeosExtensions = implode( ',', $aimeos->getExtensions() );
245
246
		$context->setView( $view );
247
248
		return \Aimeos\Admin\JQAdm::create( $context, $aimeos, $resource );
249
	}
250
251
252
	/**
253
	 * Returns the generated HTML code
254
	 *
255
	 * @param string $content Content from admin client
256
	 * @return \Illuminate\Contracts\View\View View for rendering the output
257
	 */
258
	protected function getHtml( $content )
259
	{
260
		$site = Route::input( 'site', Input::get( 'site', 'default' ) );
261
		return View::make( 'shop::jqadm.index', array( 'content' => $content, 'site' => $site ) );
262
	}
263
}
264