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

Standard::checkEntry()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8333
c 0
b 0
f 0
cc 7
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Catalog\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\Catalog\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/catalog/import/csv/processor/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Catalog\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 2018.04
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\Catalog\Import\Csv\Processor\Iface $object Decorated processor
45
	 */
46
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
47
			\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $object = null )
48
	{
49
		parent::__construct( $context, $mapping, $object );
50
51
		/** controller/common/catalog/import/csv/processor/media/listtypes
52
		 * Names of the catalog 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 catalogs and don't want these to be touched during the
56
		 * import, you can specify the catalog list types for these media
57
		 * that shouldn't be updated or removed.
58
		 *
59
		 * @param array|null List of catalog list type names or null for all
60
		 * @since 2018.04
61
		 * @category Developer
62
		 * @category User
63
		 * @see controller/common/catalog/import/csv/domains
64
		 * @see controller/common/catalog/import/csv/processor/attribute/listtypes
65
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
67
		 * @see controller/common/catalog/import/csv/processor/price/listtypes
68
		 * @see controller/common/catalog/import/csv/processor/text/listtypes
69
		 */
70
		$key = 'controller/common/catalog/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, 'catalog/lists/type' );
77
78
			$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
79
			$search->setConditions( $search->compare( '==', 'catalog.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', 'catalog' ) );
91
92
		foreach( $manager->searchItems( $search ) as $item ) {
93
			$this->types[$item->getCode()] = $item->getCode();
94
		}
95
	}
96
97
98
	/**
99
	 * Saves the catalog related data to the storage
100
	 *
101
	 * @param \Aimeos\MShop\Catalog\Item\Iface $catalog Catalog 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\Catalog\Item\Iface $catalog, array $data )
106
	{
107
		$context = $this->getContext();
108
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
109
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' );
110
		$separator = $context->getConfig()->get( 'controller/common/catalog/import/csv/separator', "\n" );
111
112
		$listMap = [];
113
		$map = $this->getMappedChunk( $data, $this->getMapping() );
114
		$listItems = $catalog->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, 'catalog.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
				if( isset( $listMap[$url][$type][$typecode] ) )
136
				{
137
					$listItem = $listMap[$url][$type][$typecode];
138
					$refItem = $listItem->getRefItem();
139
					unset( $listItems[ $listItem->getId() ] );
140
				}
141
				else
142
				{
143
					$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...
144
					$refItem = $manager->createItem( $type, 'catalog' );
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...
145
				}
146
147
				$list['media.url'] = $url;
148
149
				$list = $refItem->fromArray( $this->addItemDefaults( $list ) );
150
				$list = $listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
151
152
				$catalog->addListItem( 'media', $listItem, $refItem );
153
			}
154
		}
155
156
		$catalog->deleteListItems( $listItems, true );
157
158
		return $this->getObject()->process( $catalog, $data );
159
	}
160
161
162
	/**
163
	 * Adds the text item default values and returns the resulting array
164
	 *
165
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
166
	 * @return array Given associative list enriched by default values if they were not already set
167
	 */
168
	protected function addItemDefaults( array $list )
169
	{
170
		if( !isset( $list['media.label'] ) ) {
171
			$list['media.label'] = $list['media.url'];
172
		}
173
174
		if( !isset( $list['media.preview'] ) ) {
175
			$list['media.preview'] = $list['media.url'];
176
		}
177
178
		if( !isset( $list['media.status'] ) ) {
179
			$list['media.status'] = 1;
180
		}
181
182
		return $list;
183
	}
184
185
186
	/**
187
	 * Checks if an entry can be used for updating a media item
188
	 *
189
	 * @param array $list Associative list of key/value pairs from the mapping
190
	 * @return boolean True if valid, false if not
191
	 */
192
	protected function checkEntry( array $list )
193
	{
194
		if( !isset( $list['media.url'] ) || trim( $list['media.url'] ) === '' ) {
195
			return false;
196
		}
197
198
		if( isset( $list['catalog.lists.type'] ) && !in_array( trim( $list['catalog.lists.type'] ), $this->listTypes )
199
			|| isset( $list['media.type'] ) && !in_array( trim( $list['media.type'] ), $this->types )
200
		) {
201
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid media or catalog list type' ) );
202
		}
203
204
		return true;
205
	}
206
}
207