Completed
Push — 2017.02 ( 5a7d73 )
by Aimeos
13:24 queued 11:34
created

View   B

Complexity

Total Complexity 22

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 16
dl 0
loc 234
rs 8.4614
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 20 1
A addAccess() 0 7 1
A addConfig() 0 8 1
A addCsrf() 0 4 1
A addNumber() 0 11 1
A addParam() 0 8 2
B addRequest() 0 18 7
A addResponse() 0 7 1
A addTranslate() 0 17 2
B addUrl() 0 28 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Base
8
 */
9
10
11
namespace Aimeos\Shop\Base;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Class providing the view objects
18
 *
19
 * @package flow
20
 * @subpackage Base
21
 * @Flow\Scope("singleton")
22
 */
23
class View
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\I18n
27
	 * @Flow\Inject
28
	 */
29
	protected $i18n;
30
31
	/**
32
	 * @var \TYPO3\Fluid\View\StandaloneView
33
	 * @Flow\Inject
34
	 */
35
	protected $view;
36
37
38
	/**
39
	 * Creates the view object for the HTML client.
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param \TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
43
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
44
	 * @param \TYPO3\Flow\Mvc\RequestInterface|null $request Request object
45
	 * @param string|null $langid Language ID
46
	 * @return \Aimeos\MW\View\Iface View object
47
	 */
48
	public function create( \Aimeos\MShop\Context\Item\Iface $context,
49
		\TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder, array $templatePaths,
50
		\TYPO3\Flow\Mvc\RequestInterface $request = null, $langid = null )
51
	{
52
		$engines = array( '.html' => new \Aimeos\MW\View\Engine\Flow( $this->view ) );
53
		$view = new \Aimeos\MW\View\Standard( $templatePaths, $engines );
54
		$config = $context->getConfig();
55
56
		$this->addCsrf( $view );
0 ignored issues
show
Unused Code introduced by
The call to the method Aimeos\Shop\Base\View::addCsrf() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
57
		$this->addAccess( $view, $context );
58
		$this->addConfig( $view, $config );
59
		$this->addNumber( $view, $config );
60
		$this->addParam( $view, $request );
61
		$this->addRequest( $view, $request );
62
		$this->addResponse( $view );
63
		$this->addTranslate( $view, $langid );
64
		$this->addUrl( $view, $uriBuilder, $request );
65
66
		return $view;
67
	}
68
69
70
	/**
71
	 * Adds the "access" helper to the view object
72
	 *
73
	 * @param \Aimeos\MW\View\Iface $view View object
74
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
75
	 * @return \Aimeos\MW\View\Iface Modified view object
76
	 */
77
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
78
	{
79
		$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
80
		$view->addHelper( 'access', $helper );
81
82
		return $view;
83
	}
84
85
86
	/**
87
	 * Adds the "config" helper to the view object
88
	 *
89
	 * @param \Aimeos\MW\View\Iface $view View object
90
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
91
	 * @return \Aimeos\MW\View\Iface Modified view object
92
	 */
93
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
94
	{
95
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
96
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
97
		$view->addHelper( 'config', $helper );
98
99
		return $view;
100
	}
101
102
103
	/**
104
	 * Adds the "access" helper to the view object
105
	 *
106
	 * @param \Aimeos\MW\View\Iface $view View object
107
	 * @return \Aimeos\MW\View\Iface Modified view object
108
	 */
109
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
110
	{
111
		return $view;
112
	}
113
114
115
	/**
116
	 * Adds the "number" helper to the view object
117
	 *
118
	 * @param \Aimeos\MW\View\Iface $view View object
119
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
120
	 * @return \Aimeos\MW\View\Iface Modified view object
121
	 */
122
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
123
	{
124
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
125
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
126
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
127
128
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
129
		$view->addHelper( 'number', $helper );
130
131
		return $view;
132
	}
133
134
135
	/**
136
	 * Adds the "param" helper to the view object
137
	 *
138
	 * @param \Aimeos\MW\View\Iface $view View object
139
	 * @param \TYPO3\Flow\Mvc\RequestInterface|null $request Request object
140
	 * @return \Aimeos\MW\View\Iface Modified view object
141
	 */
142
	protected function addParam( \Aimeos\MW\View\Iface $view, \TYPO3\Flow\Mvc\RequestInterface $request = null )
143
	{
144
		$params = ( $request !== null ? $request->getArguments() : array() );
145
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
146
		$view->addHelper( 'param', $helper );
147
148
		return $view;
149
	}
150
151
152
	/**
153
	 * Adds the "request" helper to the view object
154
	 *
155
	 * @param \Aimeos\MW\View\Iface $view View object
156
	 * @param \TYPO3\Flow\Mvc\RequestInterface|null $request Request object
157
	 * @return \Aimeos\MW\View\Iface Modified view object
158
	 */
159
	protected function addRequest( \Aimeos\MW\View\Iface $view, \TYPO3\Flow\Mvc\RequestInterface $request = null )
160
	{
161
		if( $request !== null )
162
		{
163
			$req = $request->getHttpRequest();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3\Flow\Mvc\RequestInterface as the method getHttpRequest() does only exist in the following implementations of said interface: TYPO3\Flow\Mvc\ActionRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
164
165
			$files = ( is_array( $_FILES ) ? $_FILES : array() );
166
			$query = ( is_array( $_GET ) ? $_GET : array() );
167
			$post = ( is_array( $_POST ) ? $_POST : array() );
168
			$cookie = ( is_array( $_COOKIE ) ? $_COOKIE : array() );
169
			$server = ( is_array( $_SERVER ) ? $_SERVER : array() );
170
171
			$helper = new \Aimeos\MW\View\Helper\Request\Flow( $view, $req, $files, $query, $post, $cookie, $server );
172
			$view->addHelper( 'request', $helper );
173
		}
174
175
		return $view;
176
	}
177
178
179
	/**
180
	 * Adds the "response" helper to the view object
181
	 *
182
	 * @param \Aimeos\MW\View\Iface $view View object
183
	 * @return \Aimeos\MW\View\Iface Modified view object
184
	 */
185
	protected function addResponse( \Aimeos\MW\View\Iface $view )
186
	{
187
		$helper = new \Aimeos\MW\View\Helper\Response\Flow( $view );
188
		$view->addHelper( 'response', $helper );
189
190
		return $view;
191
	}
192
193
194
	/**
195
	 * Adds the "translate" helper to the view object
196
	 *
197
	 * @param \Aimeos\MW\View\Iface $view View object
198
	 * @param string|null $langid ISO language code, e.g. "de" or "de_CH"
199
	 * @return \Aimeos\MW\View\Iface Modified view object
200
	 */
201
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $langid )
202
	{
203
		if( $langid !== null )
204
		{
205
			$i18n = $this->i18n->get( array( $langid ) );
206
			$translation = $i18n[$langid];
207
		}
208
		else
209
		{
210
			$translation = new \Aimeos\MW\Translation\None( 'en' );
211
		}
212
213
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
214
		$view->addHelper( 'translate', $helper );
215
216
		return $view;
217
	}
218
219
220
	/**
221
	 * Adds the "url" helper to the view object
222
	 *
223
	 * @param \Aimeos\MW\View\Iface $view View object
224
	 * @param \TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
225
	 * @param \TYPO3\Flow\Mvc\RequestInterface|null $request Request object
226
	 * @return \Aimeos\MW\View\Iface Modified view object
227
	 */
228
	protected function addUrl( \Aimeos\MW\View\Iface $view,
229
		\TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder,
230
		\TYPO3\Flow\Mvc\RequestInterface $request = null )
231
	{
232
		$fixed = array();
233
234
		if( $request !== null )
235
		{
236
			$params = $request->getArguments();
237
238
			if( isset( $params['site'] ) ) {
239
				$fixed['site'] = $params['site'];
240
			}
241
242
			if( isset( $params['locale'] ) ) {
243
				$fixed['locale'] = $params['locale'];
244
			}
245
246
			if( isset( $params['currency'] ) ) {
247
				$fixed['currency'] = $params['currency'];
248
			}
249
		}
250
251
		$helper = new \Aimeos\MW\View\Helper\Url\Flow( $view, $uriBuilder, $fixed );
252
		$view->addHelper( 'url', $helper );
253
254
		return $view;
255
	}
256
}