Completed
Push — master ( aa2f0e...9144ee )
by Aimeos
03:14
created

Standard::getItems()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm\Catalog;
12
13
14
/**
15
 * JSON API catalog client
16
 *
17
 * @package Admin
18
 * @subpackage JsonAdm
19
 */
20
class Standard
21
	extends \Aimeos\Admin\JsonAdm\Standard
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Standard" and comma; 1 found
Loading history...
22
	implements \Aimeos\Admin\JsonAdm\Common\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/** admin/jsonadm/catalog/decorators/excludes
25
	 * Excludes decorators added by the "common" option from the JSON API clients
26
	 *
27
	 * Decorators extend the functionality of a class by adding new aspects
28
	 * (e.g. log what is currently done), executing the methods of the underlying
29
	 * class only in certain conditions (e.g. only for logged in users) or
30
	 * modify what is returned to the caller.
31
	 *
32
	 * This option allows you to remove a decorator added via
33
	 * "admin/jsonadm/common/decorators/default" before they are wrapped
34
	 * around the Jsonadm client.
35
	 *
36
	 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
37
	 *
38
	 * This would remove the decorator named "decorator1" from the list of
39
	 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
40
	 * "admin/jsonadm/common/decorators/default" for the JSON API client.
41
	 *
42
	 * @param array List of decorator names
43
	 * @since 2016.01
44
	 * @category Developer
45
	 * @see admin/jsonadm/common/decorators/default
46
	 * @see admin/jsonadm/catalog/decorators/global
47
	 * @see admin/jsonadm/catalog/decorators/local
48
	 */
49
50
	/** admin/jsonadm/catalog/decorators/global
51
	 * Adds a list of globally available decorators only to the Jsonadm client
52
	 *
53
	 * Decorators extend the functionality of a class by adding new aspects
54
	 * (e.g. log what is currently done), executing the methods of the underlying
55
	 * class only in certain conditions (e.g. only for logged in users) or
56
	 * modify what is returned to the caller.
57
	 *
58
	 * This option allows you to wrap global decorators
59
	 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
60
	 * client.
61
	 *
62
	 *  admin/jsonadm/catalog/decorators/global = array( 'decorator1' )
63
	 *
64
	 * This would add the decorator named "decorator1" defined by
65
	 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
66
	 * "catalog" Jsonadm client.
67
	 *
68
	 * @param array List of decorator names
69
	 * @since 2016.01
70
	 * @category Developer
71
	 * @see admin/jsonadm/common/decorators/default
72
	 * @see admin/jsonadm/catalog/decorators/excludes
73
	 * @see admin/jsonadm/catalog/decorators/local
74
	 */
75
76
	/** admin/jsonadm/catalog/decorators/local
77
	 * Adds a list of local decorators only to the Jsonadm client
78
	 *
79
	 * Decorators extend the functionality of a class by adding new aspects
80
	 * (e.g. log what is currently done), executing the methods of the underlying
81
	 * class only in certain conditions (e.g. only for logged in users) or
82
	 * modify what is returned to the caller.
83
	 *
84
	 * This option allows you to wrap local decorators
85
	 * ("\Aimeos\Admin\Jsonadm\Catalog\Decorator\*") around the Jsonadm
86
	 * client.
87
	 *
88
	 *  admin/jsonadm/catalog/decorators/local = array( 'decorator2' )
89
	 *
90
	 * This would add the decorator named "decorator2" defined by
91
	 * "\Aimeos\Admin\Jsonadm\Catalog\Decorator\Decorator2" only to the
92
	 * "catalog" Jsonadm client.
93
	 *
94
	 * @param array List of decorator names
95
	 * @since 2016.01
96
	 * @category Developer
97
	 * @see admin/jsonadm/common/decorators/default
98
	 * @see admin/jsonadm/catalog/decorators/excludes
99
	 * @see admin/jsonadm/catalog/decorators/global
100
	 */
101
102
103
	/**
104
	 * Returns the requested resource or the resource list
105
	 *
106
	 * @param string $body Request body
107
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
108
	 * @param integer &$status Variable which contains the HTTP status afterwards
109
	 * @return string Content for response body
110
	 */
111
	public function get( $body, array &$header, &$status )
112
	{
113
		/** admin/jsonadm/partials/catalog/template-data
114
		 * Relative path to the data partial template file for the catalog client
115
		 *
116
		 * Partials are templates which are reused in other templates and generate
117
		 * reoccuring blocks filled with data from the assigned values. The data
118
		 * partial creates the "data" part for the JSON API response.
119
		 *
120
		 * The partial template files are usually stored in the templates/partials/ folder
121
		 * of the core or the extensions. The configured path to the partial file must
122
		 * be relative to the templates/ folder, e.g. "partials/data-standard.php".
123
		 *
124
		 * @param string Relative path to the template file
125
		 * @since 2016.07
126
		 * @category Developer
127
		 */
128
		$this->getView()->assign( array( 'partial-data' => 'admin/jsonadm/partials/catalog/template-data' ) );
129
130
		return parent::get( $body, $header, $status );
131
	}
132
133
134
	/**
135
	 * Returns the items with parent/child relationships
136
	 *
137
	 * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface
138
	 * @param array $include List of resource types that should be fetched
139
	 * @return array List of items implementing \Aimeos\MShop\Common\Item\Iface
140
	 */
141
	protected function getChildItems( array $items, array $include )
142
	{
143
		$list = array();
144
145
		if( in_array( 'catalog', $include ) )
146
		{
147
			foreach( $items as $item ) {
148
				$list = array_merge( $list, $item->getChildren() );
149
			}
150
		}
151
152
		return $list;
153
	}
154
155
156
	/**
157
	 * Retrieves the item or items and adds the data to the view
158
	 *
159
	 * @param \Aimeos\MW\View\Iface $view View instance
160
	 * @return \Aimeos\MW\View\Iface View instance with additional data assigned
161
	 */
162
	protected function getItems( \Aimeos\MW\View\Iface $view )
163
	{
164
		$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : array() );
165
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'catalog' );
166
		$search = $this->initCriteria( $manager->createSearch(), $view->param() );
167
		$total = 1;
168
169
		if( ( $id = $view->param( 'id' ) ) == null )
170
		{
171
			$view->data = $manager->searchItems( $search, array(), $total );
172
			$view->listItems = $this->getListItems( $view->data, $include );
173
			$view->childItems = $this->getChildItems( $view->data, $include );
174
		}
175
		else
176
		{
177
			$view->data = $manager->getTree( $id, array(), \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST, $search );
178
			$view->listItems = $this->getListItems( array( $id => $view->data ), $include );
179
			$view->childItems = $this->getChildItems( array( $view->data ), $include );
180
		}
181
182
		$view->refItems = $this->getRefItems( $view->listItems );
183
		$view->total = $total;
184
185
		return $view;
186
	}
187
188
189
	/**
190
	 * Returns the list items for association relationships
191
	 *
192
	 * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface
193
	 * @param array $include List of resource types that should be fetched
194
	 * @return array List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface
195
	 */
196
	protected function getListItems( array $items, array $include )
197
	{
198
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'catalog/lists' );
199
200
		$search = $manager->createSearch();
201
		$expr = array(
202
			$search->compare( '==', 'catalog.lists.parentid', array_keys( $items ) ),
203
			$search->compare( '==', 'catalog.lists.domain', $include ),
204
		);
205
		$search->setConditions( $search->combine( '&&', $expr ) );
206
207
		return $manager->searchItems( $search );
208
	}
209
210
211
	/**
212
	 * Saves and returns the new or updated item
213
	 *
214
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items
215
	 * @param \stdClass $entry Object including "id" and "attributes" elements
216
	 * @return \Aimeos\MShop\Common\Item\Iface New or updated item
217
	 */
218
	protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry )
219
	{
220
		$targetId = ( isset( $entry->targetid ) ? $entry->targetid : null );
221
		$refId = ( isset( $entry->refid ) ? $entry->refid : null );
222
223
		if( isset( $entry->id ) )
224
		{
225
			$item = $manager->getItem( $entry->id );
226
			$item = $this->addItemData( $manager, $item, $entry, $item->getResourceType() );
227
			$manager->saveItem( $item );
228
229
			if( isset( $entry->parentid ) && $targetId !== null ) {
230
				$manager->moveItem( $item->getId(), $entry->parentid, $targetId, $refId );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method moveItem() does only exist in the following implementations of said interface: Aimeos\MShop\Attribute\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Decorator\Sitecheck, Aimeos\MShop\Catalog\Manager\Lists\Standard, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Common\Manager\Lists\Base, Aimeos\MShop\Customer\Manager\Lists\Standard, Aimeos\MShop\Locale\Manager\Site\Standard, Aimeos\MShop\Media\Manager\Lists\Standard, Aimeos\MShop\Price\Manager\Lists\Standard, Aimeos\MShop\Product\Manager\Lists\Standard, Aimeos\MShop\Service\Manager\Lists\Standard, Aimeos\MShop\Supplier\Manager\Lists\Standard, Aimeos\MShop\Text\Manager\Lists\Standard.

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...
231
			}
232
		}
233
		else
234
		{
235
			$item = $manager->createItem();
236
			$item = $this->addItemData( $manager, $item, $entry, $item->getResourceType() );
237
			$manager->insertItem( $item, $targetId, $refId );
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Manager\Iface as the method insertItem() does only exist in the following implementations of said interface: Aimeos\MShop\Catalog\Manager\Decorator\Base, Aimeos\MShop\Catalog\Manager\Decorator\Sitecheck, Aimeos\MShop\Catalog\Manager\Standard, Aimeos\MShop\Locale\Manager\Site\Standard.

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...
238
		}
239
240
		if( isset( $entry->relationships ) ) {
241
			$this->saveRelationships( $manager, $item, $entry->relationships );
242
		}
243
244
		return $manager->getItem( $item->getId() );
245
	}
246
}
247