Completed
Pull Request — master (#25)
by
unknown
02:59
created

Standard   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 18
c 3
b 1
f 0
dl 0
loc 73
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 3 1
A get() 0 17 3
A __construct() 0 10 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Cache\Supplier;
12
13
14
/**
15
 * Category cache for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Cache\Base
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Cache\Iface
23
{
24
	/** controller/common/product/import/csv/cache/supplier/name
25
	 * Name of the supplier cache implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Cache\Supplier\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the cache class name
31
	 * @since 2015.10
32
	 * @category Developer
33
	 */
34
35
	private $suppliers = [];
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 */
43
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
44
	{
45
		parent::__construct( $context );
46
47
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
48
		$result = $manager->searchItems( $manager->createSearch() );
49
50
		foreach( $result as $id => $item )
51
		{
52
			$this->suppliers[$item->getCode()] = $id;
53
		}
54
	}
55
56
57
	/**
58
	 * Returns the supplier ID for the given code and type
59
	 *
60
	 * @param string $code Category code
61
	 * @param string|null $type Not used
62
	 * @return string|null Supplier ID or null if not found
63
	 */
64
	public function get( string $code, string $type = null )
65
	{
66
		if( isset( $this->suppliers[$code] ) )
67
		{
68
			return $this->suppliers[$code];
69
		}
70
71
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
72
73
		$search = $manager->createSearch();
74
		$search->setConditions( $search->compare( '==', 'supplier.code', $code ) );
75
76
77
		if( ($item = $manager->searchItems( $search )->first()) !== null )
78
		{
79
			$this->suppliers[$code] = $item->getId();
80
			return $item->getId();
81
		}
82
	}
83
84
85
	/**
86
	 * Adds the supplier item to the cache
87
	 *
88
	 * @param \Aimeos\MShop\Common\Item\Iface $item Supplier object
89
	 */
90
	public function set( \Aimeos\MShop\Common\Item\Iface $item )
91
	{
92
		$this->suppliers[$item->getCode()] = $item->getId();
93
	}
94
}
95