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

Standard::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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
			$this->suppliers[$item->getCode()] = $id;
52
		}
53
	}
54
55
56
	/**
57
	 * Returns the supplier ID for the given code and type
58
	 *
59
	 * @param string $code Category code
60
	 * @param string|null $type Not used
61
	 * @return string|null Supplier ID or null if not found
62
	 */
63
	public function get( string $code, string $type = null )
64
	{
65
		if( isset( $this->suppliers[$code] ) ) {
66
			return $this->suppliers[$code];
67
		}
68
69
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
70
71
		$search = $manager->createSearch();
72
		$search->setConditions( $search->compare( '==', 'supplier.code', $code ) );
73
74
75
		if( ( $item = $manager->searchItems( $search )->first() ) !== null )
76
		{
77
			$this->suppliers[$code] = $item->getId();
78
			return $item->getId();
79
		}
80
	}
81
82
83
	/**
84
	 * Adds the supplier item to the cache
85
	 *
86
	 * @param \Aimeos\MShop\Common\Item\Iface $item Supplier object
87
	 */
88
	public function set( \Aimeos\MShop\Common\Item\Iface $item )
89
	{
90
		$this->suppliers[$item->getCode()] = $item->getId();
91
	}
92
}
93