Passed
Push — master ( ef75b4...65eb72 )
by Aimeos
10:40
created

Shop::template()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
use Illuminate\Support\Facades\View;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Aimeos\Shop\Base\View. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
15
/**
16
 * Service providing the shop object
17
 *
18
 * @package laravel
19
 * @subpackage Base
20
 */
21
class Shop
22
{
23
	/**
24
	 * @var \Aimeos\MShop\Context\Item\Iface
25
	 */
26
	private $context;
27
28
	/**
29
	 * @var \Aimeos\MW\View\Iface
30
	 */
31
	private $view;
32
33
	/**
34
	 * @var array
35
	 */
36
	private $objects = [];
37
38
39
	/**
40
	 * Initializes the object
41
	 *
42
	 * @param \Aimeos\Shop\Base\Aimeos $aimeos Aimeos object
43
	 * @param \Aimeos\Shop\Base\Context $context Context object
44
	 * @param \Aimeos\Shop\Base\View $view View object
45
	 */
46
	public function __construct( \Aimeos\Shop\Base\Aimeos $aimeos,
47
		\Aimeos\Shop\Base\Context $context, \Aimeos\Shop\Base\View $view )
48
	{
49
		$this->context = $context->get();
50
51
		$langid = $this->context->getLocale()->getLanguageId();
52
		$tmplPaths = $aimeos->get()->getCustomPaths( 'client/html/templates' );
53
54
		$this->view = $view->create( $this->context, $tmplPaths, $langid );
55
		$this->context->setView( $this->view );
56
	}
57
58
59
	/**
60
	 * Returns the HTML client for the given name
61
	 *
62
	 * @param string $name Name of the shop component
63
	 * @return \Aimeos\Client\Html\Iface HTML client
64
	 */
65
	public function get( string $name ) : \Aimeos\Client\Html\Iface
66
	{
67
		if( !isset( $this->objects[$name] ) )
68
		{
69
			$client = \Aimeos\Client\Html::create( $this->context, $name );
70
			$client->setView( clone $this->view );
71
			$client->process();
72
73
			$this->objects[$name] = $client;
74
		}
75
76
		return $this->objects[$name];
77
	}
78
79
80
	/** Returns the view template for the given name
81
	 *
82
	 * @param string $name View name, e.g. "account.index"
83
	 * @return string Template name, e.g. "shop::account.indx"
84
	 */
85
	public function template( string $name ) : string
86
	{
87
		$theme = $this->context->getLocale()->getSiteItem()->getTheme();
88
		return View::exists( $theme . '::' . $name ) ? $theme . '::' . $name : 'shop::' . $name;
89
	}
90
}
91