Completed
Push — master ( 9109fd...709bda )
by Aimeos
02:33
created

Standard::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Standard::compare() 0 5 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 Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Supplier;
12
13
14
/**
15
 * Default implementation of the supplier frontend controller
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	private $conditions = [];
25
	private $filter;
26
	private $manager;
27
28
29
	/**
30
	 * Common initialization for controller classes
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
35
	{
36
		parent::__construct( $context );
37
38
		$this->manager = \Aimeos\MShop::create( $context, 'supplier' );
39
		$this->filter = $this->manager->createSearch( true );
40
		$this->conditions[] = $this->filter->getConditions();
41
	}
42
43
44
	/**
45
	 * Clones objects in controller and resets values
46
	 */
47
	public function __clone()
48
	{
49
		$this->filter = clone $this->filter;
50
	}
51
52
53
	/**
54
	 * Adds generic condition for filtering
55
	 *
56
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
57
	 * @param string $key Search key defined by the supplier manager, e.g. "supplier.status"
58
	 * @param array|string $value Value or list of values to compare to
59
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
60
	 * @since 2019.04
61
	 */
62
	public function compare( $operator, $key, $value )
63
	{
64
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
65
		return $this;
66
	}
67
68
69
	/**
70
	 * Returns the supplier for the given supplier ID
71
	 *
72
	 * @param string $id Unique supplier ID
73
	 * @param string[] $domains Domain names of items that are associated with the suppliers and that should be fetched too
74
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
75
	 * @since 2019.04
76
	 */
77
	public function get( $id, $domains = ['media', 'text'] )
78
	{
79
		return $this->manager->getItem( $id, $domains, true );
80
	}
81
82
83
	/**
84
	 * Returns the supplier for the given supplier code
85
	 *
86
	 * @param string $code Unique supplier code
87
	 * @param string[] $domains Domain names of items that are associated with the suppliers and that should be fetched too
88
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
89
	 * @since 2019.04
90
	 */
91
	public function find( $code, $domains = ['media', 'text'] )
92
	{
93
		return $this->manager->findItem( $code, $domains, null, null, true );
94
	}
95
96
97
	/**
98
	 * Parses the given array and adds the conditions to the list of conditions
99
	 *
100
	 * @param array $conditions List of conditions, e.g. ['=~' => ['supplier.label' => 'test']]
101
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
102
	 * @since 2019.04
103
	 */
104
	public function parse( array $conditions )
105
	{
106
		$this->conditions[] = $this->filter->toConditions( $conditions );
107
		return $this;
108
	}
109
110
111
	/**
112
	 * Returns the suppliers filtered by the previously assigned conditions
113
	 *
114
	 * @param string[] $domains Domain names of items that are associated with the suppliers and that should be fetched too
115
	 * @param integer &$total Parameter where the total number of found suppliers will be stored in
116
	 * @return \Aimeos\MShop\Supplier\Item\Iface[] Ordered list of supplier items
117
	 * @since 2019.04
118
	 */
119
	public function search( $domains = ['media', 'text'], &$total = null )
120
	{
121
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
122
		return $this->manager->searchItems( $this->filter, $domains, $total );
123
	}
124
125
126
	/**
127
	 * Sets the start value and the number of returned supplier items for slicing the list of found supplier items
128
	 *
129
	 * @param integer $start Start value of the first supplier item in the list
130
	 * @param integer $limit Number of returned supplier items
131
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
132
	 * @since 2019.04
133
	 */
134
	public function slice( $start, $limit )
135
	{
136
		$this->filter->setSlice( $start, $limit );
137
		return $this;
138
	}
139
140
141
	/**
142
	 * Sets the sorting of the result list
143
	 *
144
	 * @param string|null $key Sorting key of the result list like "supplier.label", null for no sorting
145
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
146
	 * @since 2019.04
147
	 */
148
	public function sort( $key = null )
149
	{
150
		$direction = '+';
151
152
		if( $key != null && $key[0] === '-' )
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
153
		{
154
			$key = substr( $key, 1 );
155
			$direction = '-';
156
		}
157
158
		switch( $key )
159
		{
160
			case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
161
				$this->filter->setSortations( [] );
162
				break;
163
			default:
164
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
165
		}
166
167
		return $this;
168
	}
169
}
170