Completed
Pull Request — master (#25)
by
unknown
03:05
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
	 * @param bool $create_if_not_exists Try to create a new one if existing is not found
62
	 * @return string|null Supplier ID or null if not found
63
	 */
64
	public function get( string $code, string $type = null, bool $create_if_not_exists = false )
65
	{
66
		if( isset( $this->suppliers[$code] ) ) {
67
			return $this->suppliers[$code];
68
		}
69
70
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
71
72
		$search = $manager->createSearch();
73
		$search->setConditions( $search->compare( '==', 'supplier.code', $code ) );
74
75
76
		if( ( $item = $manager->searchItems( $search )->first() ) == null )
77
		{
78
            $item = $manager->createItem()->setCode($code);
79
            if( ( $item = $manager->saveItem($item) ) == null ) return null;
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            if( ( $item = $manager->/** @scrutinizer ignore-call */ saveItem($item) ) == null ) return null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
		}
81
82
        $this->suppliers[$code] = $item->getId();
83
        return $item->getId();
84
	}
85
86
87
	/**
88
	 * Adds the supplier item to the cache
89
	 *
90
	 * @param \Aimeos\MShop\Common\Item\Iface $item Supplier object
91
	 */
92
	public function set( \Aimeos\MShop\Common\Item\Iface $item )
93
	{
94
		$this->suppliers[$item->getCode()] = $item->getId();
95
	}
96
}
97