Passed
Push — master ( abc149...20e1a0 )
by Aimeos
03:21
created

Standard   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 119
rs 10
wmc 19

2 Methods

Rating   Name   Duplication   Size   Complexity  
B update() 0 29 10
B process() 0 54 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2020
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Common\Import\Xml\Processor\Lists\Media;
12
13
14
/**
15
 * Media list processor for XML imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Common\Import\Xml\Processor\Base
22
	implements \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface
23
{
24
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
25
26
27
	/** controller/common/common/import/xml/processor/lists/media/name
28
	 * Name of the lists processor implementation
29
	 *
30
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\Processor\Lists\Media\Myname".
31
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
32
	 *
33
	 * @param string Last part of the processor class name
34
	 * @since 2019.04
35
	 * @category Developer
36
	 */
37
38
39
	/**
40
	 * Updates the given item using the data from the DOM node
41
	 *
42
	 * @param \Aimeos\MShop\Common\Item\Iface $item Item which should be updated
43
	 * @param \DOMNode $node XML document node containing a list of nodes to process
44
	 * @return \Aimeos\MShop\Common\Item\Iface Updated item
45
	 */
46
	public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node ) : \Aimeos\MShop\Common\Item\Iface
47
	{
48
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListRef\Iface::class, $item );
49
50
		$listItems = $item->getListItems( 'media', null, null, false )->reverse();
51
		$resource = $item->getResourceType();
52
		$context = $this->getContext();
53
54
		$listManager = \Aimeos\MShop::create( $context, $resource . '/lists' );
55
		$manager = \Aimeos\MShop::create( $context, 'media' );
56
57
		foreach( $node->childNodes as $refNode )
58
		{
59
			if( $refNode->nodeName !== 'mediaitem' ) {
60
				continue;
61
			}
62
63
			if( ( $listItem = $listItems->pop() ) === null ) {
64
				$listItem = $listManager->create();
65
			}
66
67
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
68
				$refItem = $manager->create();
69
			}
70
71
			$list = [];
72
73
			foreach( $refNode->childNodes as $tag )
74
			{
75
				if( in_array( $tag->nodeName, ['lists', 'property'] ) ) {
76
					$refItem = $this->getProcessor( $tag->nodeName )->process( $refItem, $tag );
77
				} else {
78
					$list[$tag->nodeName] = $tag->nodeValue;
79
				}
80
			}
81
82
			$refItem = $this->update( $refItem, $list );
83
84
			foreach( $refNode->attributes as $attrName => $attrNode ) {
85
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
86
			}
87
88
			$name = $resource . '.lists.config';
89
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
90
			$name = $resource . '.lists.type';
91
			$list[$name] = $list[$name] ?? 'default';
92
93
			$this->addType( $resource . '/lists/type', 'media', $list[$resource . '.lists.type'] );
94
95
			$listItem = $listItem->fromArray( $list );
96
			$item->addListItem( 'media', $listItem, $refItem );
97
		}
98
99
		return $item->deleteListItems( $listItems->toArray() );
100
	}
101
102
103
	/**
104
	 * Updates the media item with the given key/value pairs
105
	 *
106
	 * @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update
107
	 * @param array &$list Associative list of key/value pairs, matching pairs are removed
108
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item
109
	 */
110
	protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list )
111
	{
112
		$context = $this->getContext();
113
		$fs = $context->fs( 'fs-media' );
114
		$url = $list['media.url'] ?? '';
115
116
		try
117
		{
118
			if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) {
119
				$refItem->setPreviews( $map )->setUrl( $url );
120
			} elseif( isset( $list['media.preview'] ) ) {
121
				$refItem->setPreview( $list['media.preview'] )->setUrl( $url );
122
			} elseif( $refItem->getPreviews() === [] || $refItem->getUrl() !== $url
123
				|| $fs->has( $url ) && (
124
					!( $fs instanceof \Aimeos\MW\Filesystem\MetaIface )
125
					|| date( 'Y-m-d H:i:s', $fs->time( $url ) ) > $refItem->getTimeModified()
126
				)
127
			) {
128
				$refItem = \Aimeos\Controller\Common\Media\Factory::create( $context )->scale( $refItem->setUrl( $url ) );
129
			}
130
131
			unset( $list['media.previews'], $list['media.preview'] );
132
		}
133
		catch( \Aimeos\Controller\Common\Exception $e )
134
		{
135
			$context->getLogger()->log( sprintf( 'Scaling image "%1$s" failed: %2$s', $url, $e->getMessage() ) );
136
		}
137
138
		return $refItem->fromArray( $list );
139
	}
140
}
141