Completed
Push — master ( 984043...a9b00f )
by Aimeos
02:00
created

Standard::checkEntry()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.6666
c 0
b 0
f 0
cc 7
nc 4
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Media;
12
13
14
/**
15
 * Media processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/product/import/csv/processor/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Media\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2015.10
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
	private $types = [];
37
38
39
	/**
40
	 * Initializes the object
41
	 *
42
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
43
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
44
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
45
	 */
46
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
47
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
48
	{
49
		parent::__construct( $context, $mapping, $object );
50
51
		/** controller/common/product/import/csv/processor/media/listtypes
52
		 * Names of the product list types for media that are updated or removed
53
		 *
54
		 * If you want to associate media items manually via the administration
55
		 * interface to products and don't want these to be touched during the
56
		 * import, you can specify the product list types for these media
57
		 * that shouldn't be updated or removed.
58
		 *
59
		 * @param array|null List of product list type names or null for all
60
		 * @since 2015.05
61
		 * @category Developer
62
		 * @category User
63
		 * @see controller/common/product/import/csv/domains
64
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
65
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/product/import/csv/processor/product/listtypes
67
		 * @see controller/common/product/import/csv/processor/price/listtypes
68
		 * @see controller/common/product/import/csv/processor/text/listtypes
69
		 */
70
		$key = 'controller/common/product/import/csv/processor/media/listtypes';
71
		$this->listTypes = $context->getConfig()->get( $key );
72
73
		if( $this->listTypes === null )
74
		{
75
			$this->listTypes = [];
76
			$manager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists/type' );
77
78
			$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
79
			$search->setConditions( $search->compare( '==', 'product.lists.type.domain', 'media' ) );
80
81
			foreach( $manager->searchItems( $search ) as $item ) {
82
				$this->listTypes[$item->getCode()] = $item->getCode();
83
			}
84
		}
85
86
87
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media/type' );
88
89
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
90
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'product' ) );
91
92
		foreach( $manager->searchItems( $search ) as $item ) {
93
			$this->types[$item->getCode()] = $item->getCode();
94
		}
95
	}
96
97
98
	/**
99
	 * Saves the product related data to the storage
100
	 *
101
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
102
	 * @param array $data List of CSV fields with position as key and data as value
103
	 * @return array List of data which hasn't been imported
104
	 */
105
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
106
	{
107
		$context = $this->getContext();
108
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
109
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' );
110
		$separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" );
111
112
		$listMap = [];
113
		$map = $this->getMappedChunk( $data, $this->getMapping() );
114
		$listItems = $product->getListItems( 'media', $this->listTypes );
115
116
		foreach( $listItems as $listItem )
117
		{
118
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
119
				$listMap[ $refItem->getUrl() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
120
			}
121
		}
122
123
		foreach( $map as $pos => $list )
124
		{
125
			if( $this->checkEntry( $list ) === false ) {
126
				continue;
127
			}
128
129
			$urls = explode( $separator, trim( $list['media.url'] ) );
130
			$type = trim( $this->getValue( $list, 'media.type', 'default' ) );
0 ignored issues
show
Documentation Bug introduced by
The method getValue does not exist on object<Aimeos\Controller...ocessor\Media\Standard>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
131
			$typecode = trim( $this->getValue( $list, 'product.lists.type', 'default' ) );
0 ignored issues
show
Documentation Bug introduced by
The method getValue does not exist on object<Aimeos\Controller...ocessor\Media\Standard>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
132
133
			foreach( $urls as $url )
134
			{
135
				$url = trim( $url );
136
137
				if( isset( $listMap[$url][$type][$typecode] ) )
138
				{
139
					$listItem = $listMap[$url][$type][$typecode];
140
					$refItem = $listItem->getRefItem();
141
					unset( $listItems[ $listItem->getId() ] );
142
				}
143
				else
144
				{
145
					$listItem = $listManager->createItem( $typecode, 'media' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $typecode.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
					$refItem = $manager->createItem( $type, 'product' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $type.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
147
				}
148
149
				$list['media.url'] = $url;
150
151
				$refItem->fromArray( $this->addItemDefaults( $list ) );
152
				$listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
153
154
				$product->addListItem( 'media', $listItem, $refItem );
155
			}
156
		}
157
158
		$product->deleteListItems( $listItems, true );
159
160
		return $this->getObject()->process( $product, $data );
161
	}
162
163
164
	/**
165
	 * Adds the text item default values and returns the resulting array
166
	 *
167
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
168
	 * @return array Given associative list enriched by default values if they were not already set
169
	 */
170
	protected function addItemDefaults( array $list )
171
	{
172
		if( !isset( $list['media.label'] ) ) {
173
			$list['media.label'] = $list['media.url'];
174
		}
175
176
		if( !isset( $list['media.preview'] ) ) {
177
			$list['media.preview'] = $list['media.url'];
178
		}
179
180
		if( !isset( $list['media.status'] ) ) {
181
			$list['media.status'] = 1;
182
		}
183
184
		return $list;
185
	}
186
187
188
	/**
189
	 * Checks if an entry can be used for updating a media item
190
	 *
191
	 * @param array $list Associative list of key/value pairs from the mapping
192
	 * @return boolean True if valid, false if not
193
	 */
194
	protected function checkEntry( array $list )
195
	{
196
		if( !isset( $list['media.url'] ) || trim( $list['media.url'] ) === '' ) {
197
			return false;
198
		}
199
200
		if( isset( $list['product.lists.type'] ) && !in_array( trim( $list['product.lists.type'] ), $this->listTypes ) )
201
		{
202
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $list['product.lists.type'], 'product list' );
203
			throw new \Aimeos\Controller\Common\Exception( $msg );
204
		}
205
206
		if( isset( $list['media.type'] ) && !in_array( trim( $list['media.type'] ), $this->types ) )
207
		{
208
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $list['media.type'], 'media' );
209
			throw new \Aimeos\Controller\Common\Exception( $msg );
210
		}
211
212
		return true;
213
	}
214
}
215