Completed
Push — master ( 697e3b...a08d75 )
by Aimeos
02:01
created

Jqadm::exportAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\Slim\Controller;
12
13
use Interop\Container\ContainerInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
18
/**
19
 * Aimeos controller for the JQuery admin interface
20
 *
21
 * @package Slim
22
 * @subpackage Controller
23
 */
24
class Jqadm
25
{
26
	/**
27
	 * Returns the JS file content
28
	 *
29
	 * @param ContainerInterface $container Dependency injection container
30
	 * @param ServerRequestInterface $request Request object
31
	 * @param ResponseInterface $response Response object
32
	 * @param array $args Associative list of route parameters
33
	 * @return ResponseInterface $response Modified response object with generated output
34
	 */
35
	public static function fileAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
36
	{
37
		$contents = '';
38
		$files = array();
39
		$aimeos = $container->get( 'aimeos' );
40
		$type = ( isset( $args['type'] ) ? $args['type'] : 'js' );
41
42 View Code Duplication
		foreach( $aimeos->getCustomPaths( 'admin/jqadm' ) as $base => $paths )
43
		{
44
			foreach( $paths as $path )
45
			{
46
				$jsbAbsPath = $base . '/' . $path;
47
				$jsb2 = new \Aimeos\MW\Jsb2\Standard( $jsbAbsPath, dirname( $jsbAbsPath ) );
48
				$files = array_merge( $files, $jsb2->getFiles( $type ) );
49
			}
50
		}
51
52
		foreach( $files as $file )
53
		{
54
			if( ( $content = file_get_contents( $file ) ) !== false ) {
55
				$contents .= $content;
56
			}
57
		}
58
59
		$response->getBody()->write( $contents );
60
61
		if( $type === 'js' ) {
62
			$response = $response->withHeader( 'Content-Type', 'application/javascript' );
63
		} elseif( $type === 'css' ) {
64
			$response = $response->withHeader( 'Content-Type', 'text/css' );
65
		}
66
67
		return $response;
68
	}
69
70
71
	/**
72
	 * Returns the HTML code for a copy of a resource object
73
	 *
74
	 * @param ContainerInterface $container Dependency injection container
75
	 * @param ServerRequestInterface $request Request object
76
	 * @param ResponseInterface $response Response object
77
	 * @param array $args Associative list of route parameters
78
	 * @return ResponseInterface $response Modified response object with generated output
79
	 */
80 View Code Duplication
	public static function copyAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
83
84
		if( ( $html = $cntl->copy() ) == '' ) {
85
			return $cntl->getView()->response();
86
		}
87
88
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::copyAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
	}
90
91
92
	/**
93
	 * Returns the HTML code for a new resource object
94
	 *
95
	 * @param ContainerInterface $container Dependency injection container
96
	 * @param ServerRequestInterface $request Request object
97
	 * @param ResponseInterface $response Response object
98
	 * @param array $args Associative list of route parameters
99
	 * @return ResponseInterface $response Modified response object with generated output
100
	 */
101 View Code Duplication
	public static function createAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
104
105
		if( ( $html = $cntl->create() ) == '' ) {
106
			return $cntl->getView()->response();
107
		}
108
109
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::createAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
110
	}
111
112
113
	/**
114
	 * Deletes the resource object or a list of resource objects
115
	 *
116
	 * @param ContainerInterface $container Dependency injection container
117
	 * @param ServerRequestInterface $request Request object
118
	 * @param ResponseInterface $response Response object
119
	 * @param array $args Associative list of route parameters
120
	 * @return ResponseInterface $response Modified response object with generated output
121
	 */
122 View Code Duplication
	public static function deleteAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
125
126
		if( ( $html = $cntl->delete() ) == '' ) {
127
			return $cntl->getView()->response();
128
		}
129
130
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::deleteAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
131
	}
132
133
134
	/**
135
	 * Exports the requested resource object
136
	 *
137
	 * @param ContainerInterface $container Dependency injection container
138
	 * @param ServerRequestInterface $request Request object
139
	 * @param ResponseInterface $response Response object
140
	 * @param array $args Associative list of route parameters
141
	 * @return ResponseInterface $response Modified response object with generated output
142
	 */
143 View Code Duplication
	public static function exportAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
146
147
		if( ( $html = $cntl->export() ) == '' ) {
148
			return $cntl->getView()->response();
149
		}
150
151
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::exportAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
152
	}
153
154
155
	/**
156
	 * Returns the HTML code for the requested resource object
157
	 *
158
	 * @param ContainerInterface $container Dependency injection container
159
	 * @param ServerRequestInterface $request Request object
160
	 * @param ResponseInterface $response Response object
161
	 * @param array $args Associative list of route parameters
162
	 * @return ResponseInterface $response Modified response object with generated output
163
	 */
164 View Code Duplication
	public static function getAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
167
168
		if( ( $html = $cntl->get() ) == '' ) {
169
			return $cntl->getView()->response();
170
		}
171
172
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::getAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
173
	}
174
175
176
	/**
177
	 * Saves a new resource object
178
	 *
179
	 * @param ContainerInterface $container Dependency injection container
180
	 * @param ServerRequestInterface $request Request object
181
	 * @param ResponseInterface $response Response object
182
	 * @param array $args Associative list of route parameters
183
	 * @return ResponseInterface $response Modified response object with generated output
184
	 */
185 View Code Duplication
	public static function saveAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
188
189
		if( ( $html = $cntl->save() ) == '' ) {
190
			return $cntl->getView()->response();
191
		}
192
193
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::saveAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
194
	}
195
196
197
	/**
198
	 * Returns the HTML code for a list of resource objects
199
	 *
200
	 * @param ContainerInterface $container Dependency injection container
201
	 * @param ServerRequestInterface $request Request object
202
	 * @param ResponseInterface $response Response object
203
	 * @param array $args Associative list of route parameters
204
	 * @return ResponseInterface $response Modified response object with generated output
205
	 */
206 View Code Duplication
	public static function searchAction( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
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
		$cntl = self::createClient( $container, $request, $response, $args );
209
210
		if( ( $html = $cntl->search() ) == '' ) {
211
			return $cntl->getView()->response();
212
		}
213
214
		return self::getHtml( $container, $response, $html );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::getHtml($co...ner, $response, $html); (Illuminate\Contracts\View\View) is incompatible with the return type documented by Aimeos\Slim\Controller\Jqadm::searchAction of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
215
	}
216
217
218
	/**
219
	 * Returns the resource controller
220
	 *
221
	 * @param ContainerInterface $container Dependency injection container
222
	 * @param ServerRequestInterface $request Request object
223
	 * @param ResponseInterface $response Response object
224
	 * @param array $args Associative list of route parameters
225
	 * @return \Aimeos\Admin\JQAdm\Iface JQAdm client
226
	 */
227
	protected static function createClient( ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response, array $args )
228
	{
229
		$aimeos = $container->get( 'aimeos' );
230
		$templatePaths = $aimeos->getCustomPaths( 'admin/jqadm/templates' );
231
		$params = $args + (array) $request->getParsedBody() + (array) $request->getQueryParams();
232
233
		$resource = ( isset( $params['resource'] ) ? $params['resource'] : null );
234
		$site = ( isset( $params['site'] ) ? $params['site'] : 'default' );
235
		$lang = ( isset( $params['lang'] ) ? $params['lang'] : 'en' );
236
237
		$context = $container->get( 'aimeos_context' )->get( false, $args, 'backend' );
238
		$context->setI18n( $container->get( 'aimeos_i18n' )->get( array( $lang, 'en' ) ) );
239
		$context->setLocale( $container->get( 'aimeos_locale' )->getBackend( $context, $site ) );
240
241
		$view = $container->get( 'aimeos_view' )->create( $context, $request, $response, $args, $templatePaths, $lang );
242
		$context->setView( $view );
243
244
		return \Aimeos\Admin\JQAdm\Factory::createClient( $context, $templatePaths, $resource )->setAimeos( $aimeos );
245
	}
246
247
248
	/**
249
	 * Returns the generated HTML code
250
	 *
251
	 * @param ContainerInterface $container Dependency injection container
252
	 * @param ResponseInterface $response Response object
253
	 * @param string $content Content from admin client
254
	 * @return \Illuminate\Contracts\View\View View for rendering the output
255
	 */
256
	protected static function getHtml( ContainerInterface $container, ResponseInterface $response, $content )
257
	{
258
		$version = \Aimeos\Slim\Bootstrap::getVersion();
259
		$extnames = implode( ',', $container->get( 'aimeos' )->getExtensions() );
260
		$content = str_replace( ['{type}', '{version}', '{extensions}'], ['Slim', $version, $extnames], $content );
261
262
		return $container->get( 'view' )->render( $response, 'Jqadm/index.html.twig', array( 'content' => $content ) );
263
	}
264
}
265