Passed
Push — master ( d9aba3...996e86 )
by Aimeos
04:12
created

ResolveController::cms()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
rs 9.8666
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2023
6
 */
7
8
9
namespace Aimeos\Shop\Controller;
10
11
use Aimeos\Shop\Facades\Shop;
12
use Illuminate\Routing\Controller;
13
use Illuminate\Support\Facades\Request;
14
use Illuminate\Support\Facades\Response;
15
use Illuminate\Support\Facades\Route;
16
use Illuminate\Support\Facades\View;
17
18
19
/**
20
 * Aimeos controller for dispatching requests.
21
 */
22
class ResolveController extends Controller
23
{
24
	private static $fcn = [];
25
26
27
	/**
28
	 * Register a new resolver function.
29
	 *
30
	 * @param string $name Name of the resolver function
31
	 * @param \Closure $fcn Resolver function
32
	 */
33
	public static function register( string $name, \Closure $fcn )
34
	{
35
		self::$fcn[$name] = $fcn;
36
	}
37
38
39
	/**
40
	 * Initializes the object.
41
	 */
42
	public function __construct()
43
	{
44
		self::$fcn['product'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
45
			return $this->product( $context, $path );
46
		};
47
48
		self::$fcn['catalog'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
49
			return $this->catalog( $context, $path );
50
		};
51
	}
52
53
54
	/**
55
	 * Returns the html of the resolved URLs.
56
	 *
57
	 * @param \Illuminate\Http\Request $request Laravel request object
58
	 * @return \Illuminate\Http\Response Laravel response object containing the generated output
59
	 */
60
	public function indexAction( \Illuminate\Http\Request $request )
61
	{
62
		if( ( $path = $request->route( 'path', $request->input( 'path' ) ) ) === null ) {
63
			abort( 404 );
64
		}
65
66
		$context = app( 'aimeos.context' )->get( true );
67
68
		foreach( self::$fcn as $name => $fcn )
69
		{
70
			try {
71
				return $fcn( $context, $path );
72
			} catch( \Exception $e ) {} // not found
73
		}
74
75
		abort( 404 );
76
	}
77
78
79
	/**
80
	 * Returns the category page if the give path can be resolved to a category.
81
	 *
82
	 * @param \Aimeos\MShop\ContextIface $context Context object
83
	 * @param string $path URL path to resolve
84
	 * @return Response Response object
85
	 */
86
	protected function catalog( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
87
	{
88
		$item = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->resolve( $path );
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
		$item = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->/** @scrutinizer ignore-call */ resolve( $path );
Loading history...
89
		$view = Shop::view();
0 ignored issues
show
Bug introduced by
The method view() does not exist on Aimeos\Shop\Facades\Shop. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
		/** @scrutinizer ignore-call */ 
90
  $view = Shop::view();
Loading history...
90
91
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
92
		$params += ['path' => $path, 'f_name' => $path, 'f_catid' => $item->getId(), 'page' => 'page-catalog-tree'];
93
94
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
95
		$view->addHelper( 'param', $helper );
96
97
		foreach( app( 'config' )->get( 'shop.page.catalog-tree' ) as $name )
98
		{
99
			$client = Shop::get( $name );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Shop\Facades\Shop::get() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
			/** @scrutinizer ignore-call */ 
100
   $client = Shop::get( $name );

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. Please note the @ignore annotation hint above.

Loading history...
100
101
			$params['aiheader'][$name] = $client->header();
102
			$params['aibody'][$name] = $client->body();
103
		}
104
105
		return Response::view( Shop::template( 'catalog.tree' ), $params )
0 ignored issues
show
Bug introduced by
The method template() does not exist on Aimeos\Shop\Facades\Shop. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
		return Response::view( Shop::/** @scrutinizer ignore-call */ template( 'catalog.tree' ), $params )
Loading history...
106
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
Unused Code introduced by
The call to config() has too many arguments starting with 30. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
			->header( 'Cache-Control', 'private, max-age=' . /** @scrutinizer ignore-call */ config( 'shop.cache_maxage', 30 ) );

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. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
107
	}
108
109
110
	/**
111
	 * Returns the CMS page if the give path can be resolved to a CMS page.
112
	 *
113
	 * @param \Aimeos\MShop\ContextIface $context Context object
114
	 * @param string $path URL path to resolve
115
	 * @return Response Response object
116
	 */
117
	protected function cms( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
118
	{
119
		$item = \Aimeos\Controller\Frontend::create( $context, 'cms' )->resolve( $path );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
120
		$view = Shop::view();
121
122
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
123
		$params += ['path' => $path, 'page' => 'page-index'];
124
125
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
126
		$view->addHelper( 'param', $helper );
127
128
		foreach( app( 'config' )->get( 'shop.page.cms' ) as $name )
129
		{
130
			$client = Shop::get( $name );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Shop\Facades\Shop::get() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
			/** @scrutinizer ignore-call */ 
131
   $client = Shop::get( $name );

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. Please note the @ignore annotation hint above.

Loading history...
131
132
			$params['aiheader'][$name] = $client->header();
133
			$params['aibody'][$name] = $client->body();
134
		}
135
136
		return Response::view( Shop::template( 'page.index' ), $params )
137
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
Bug introduced by
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
Unused Code introduced by
The call to config() has too many arguments starting with 30. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
			->header( 'Cache-Control', 'private, max-age=' . /** @scrutinizer ignore-call */ config( 'shop.cache_maxage', 30 ) );

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. Please note the @ignore annotation hint above.

Loading history...
138
	}
139
140
141
	/**
142
	 * Returns the product page if the give path can be resolved to a product.
143
	 *
144
	 * @param \Aimeos\MShop\ContextIface $context Context object
145
	 * @param string $path URL path to resolve
146
	 * @return Response Response object
147
	 */
148
	protected function product( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
149
	{
150
		$item = \Aimeos\Controller\Frontend::create( $context, 'product' )->resolve( $path );
151
		$view = Shop::view();
152
153
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
154
		$params += ['path' => $path, 'd_name' => $path, 'd_prodid' => $item->getId(), 'page' => 'page-catalog-detail'];
155
156
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
157
		$view->addHelper( 'param', $helper );
158
159
		foreach( app( 'config' )->get( 'shop.page.catalog-detail' ) as $name )
160
		{
161
			$client = Shop::get( $name );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Shop\Facades\Shop::get() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
			/** @scrutinizer ignore-call */ 
162
   $client = Shop::get( $name );

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. Please note the @ignore annotation hint above.

Loading history...
162
163
			$params['aiheader'][$name] = $client->header();
164
			$params['aibody'][$name] = $client->body();
165
		}
166
167
		return Response::view( Shop::template( 'catalog.detail' ), $params )
168
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
Unused Code introduced by
The call to config() has too many arguments starting with 30. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
			->header( 'Cache-Control', 'private, max-age=' . /** @scrutinizer ignore-call */ config( 'shop.cache_maxage', 30 ) );

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. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
169
	}
170
}