• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aimeos / aimeos-core / 97735cbe-1831-4730-a2a4-03f2e5cca3fc

24 Feb 2026 02:02PM UTC coverage: 92.593% (+0.008%) from 92.585%
97735cbe-1831-4730-a2a4-03f2e5cca3fc

push

circleci

aimeos
Added test for getCosts() in order item

9738 of 10517 relevant lines covered (92.59%)

80.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/src/MShop/Order/Item/Product/Base.php
1
<?php
2

3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 * @package MShop
7
 * @subpackage Order
8
 */
9

10

11
namespace Aimeos\MShop\Order\Item\Product;
12

13

14
/**
15
 * Order product item abstract class defining available flags.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
abstract class Base extends \Aimeos\MShop\Common\Item\Base
21
{
22
        /**
23
         * No flag used.
24
         * No order product flag set.
25
         */
26
        const FLAG_NONE = 0;
27

28
        /**
29
         * Product is immutable.
30
         * Ordered product can't be modifed or deleted by the customer because it
31
         * was e.g. added by a coupon provider.
32
         */
33
        const FLAG_IMMUTABLE = 1;
34

35

36
        private ?array $attributesMap = null;
37
        private array $attrRmItems = [];
38

39

40
        /**
41
         * Adds new and replaces existing attribute items for the product.
42
         *
43
         * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Product\Attribute\Iface[] $attributes List of order product attribute items
44
         * @return \Aimeos\MShop\Order\Item\Product\Iface Order base product item for chaining method calls
45
         */
46
        public function addAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Product\Iface
47
        {
48
                map( $attributes )->implements( \Aimeos\MShop\Order\Item\Product\Attribute\Iface::class, true );
1✔
49

50
                foreach( $attributes as $attrItem ) {
1✔
51
                        $this->setAttributeItem( $attrItem );
1✔
52
                }
53

54
                return $this;
1✔
55
        }
56

57

58
        /**
59
         * Returns the value or list of values of the attribute item for the ordered product with the given code.
60
         *
61
         * @param string $code Code of the product attribute item
62
         * @param array|string $type Type or list of types of the product attribute items
63
         * @return array|string|null Value or list of values of the attribute item for the ordered product and the given code
64
         */
65
        public function getAttribute( string $code, $type = '' )
66
        {
67
                $list = [];
7✔
68
                $map = $this->getAttributeMap();
7✔
69

70
                foreach( (array) $type as $key )
7✔
71
                {
72
                        if( isset( $map[$key][$code] ) )
7✔
73
                        {
74
                                foreach( $map[$key][$code] as $item ) {
3✔
75
                                        $list[] = $item->getValue();
3✔
76
                                }
77
                        }
78
                }
79

80
                return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
7✔
81
        }
82

83

84
        /**
85
         * Returns the attribute item or list of attribute items for the ordered product with the given code.
86
         *
87
         * @param string $code Code of the product attribute item
88
         * @param array|string $type Type of the product attribute item
89
         * @return \Aimeos\MShop\Order\Item\Product\Attribute\Iface|array|null
90
         *         Attribute item or list of items for the ordered product and the given code
91
         */
92
        public function getAttributeItem( string $code, $type = '' )
93
        {
94
                $list = [];
4✔
95
                $map = $this->getAttributeMap();
4✔
96

97
                foreach( (array) $type as $key )
4✔
98
                {
99
                        if( isset( $map[$key][$code] ) )
4✔
100
                        {
101
                                foreach( $map[$key][$code] as $item ) {
4✔
102
                                        $list[] = $item;
4✔
103
                                }
104
                        }
105
                }
106

107
                return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
4✔
108
        }
109

110

111
        /**
112
         * Returns the list of attribute items for the ordered product.
113
         *
114
         * @param array|string|null $type Filters returned attributes by the given types, type or null for no filtering
115
         * @return \Aimeos\Map List of attribute items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface
116
         */
117
        public function getAttributeItems( $type = null ) : \Aimeos\Map
118
        {
119
                if( $type === null ) {
123✔
120
                        return map( $this->get( '.attributes', [] ) );
119✔
121
                }
122

123
                $list = [];
4✔
124

125
                foreach( $this->get( '.attributes', [] ) as $attrItem )
4✔
126
                {
127
                        if( in_array( $attrItem->getType(), (array) $type ) ) {
4✔
128
                                $list[] = $attrItem;
3✔
129
                        }
130
                }
131

132
                return map( $list );
4✔
133
        }
134

135

136
        /**
137
         * Returns the deleted attribute items for the service.
138
         *
139
         * @return \Aimeos\Map List of items to be removed
140
         */
141
        public function getAttributeItemsDeleted() : \Aimeos\Map
142
        {
143
                return map( $this->attrRmItems );
7✔
144
        }
145

146

147
        /**
148
         * Adds or replaces the attribute item in the list of service attributes.
149
         *
150
         * @param \Aimeos\MShop\Order\Item\Product\Attribute\Iface $item Service attribute item
151
         * @return \Aimeos\MShop\Order\Item\Product\Iface Order base product item for chaining method calls
152
         */
153
        public function setAttributeItem( \Aimeos\MShop\Order\Item\Product\Attribute\Iface $item ) : \Aimeos\MShop\Order\Item\Product\Iface
154
        {
155
                $this->getAttributeMap();
5✔
156

157
                $type = $item->getType();
5✔
158
                $code = $item->getCode();
5✔
159
                $attrId = $item->getAttributeId();
5✔
160

161
                if( !isset( $this->attributesMap[$type][$code][$attrId] ) )
5✔
162
                {
163
                        $this->set( '.attributes', map( $this->get( '.attributes', [] ) )->push( $item ) );
5✔
164
                        $this->attributesMap[$type][$code][$attrId] = $item;
5✔
165
                }
166

167
                $this->attributesMap[$type][$code][$attrId]->setValue( $item->getValue() );
5✔
168

169
                return $this->setModified();
5✔
170
        }
171

172

173
        /**
174
         * Sets the new list of attribute items for the product.
175
         *
176
         * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Product\Attribute\Iface[] $attributes List of order product attribute items
177
         * @return \Aimeos\MShop\Order\Item\Product\Iface Order base product item for chaining method calls
178
         */
179
        public function setAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Product\Iface
180
        {
181
                ( $attributes = map( $attributes ) )->implements( \Aimeos\MShop\Order\Item\Product\Attribute\Iface::class, true );
7✔
182

183
                $this->attrRmItems = map( $this->get( '.attributes', [] ) )
7✔
184
                        ->diff( $attributes )
7✔
185
                        ->merge( $this->attrRmItems )
7✔
186
                        ->unique()
7✔
187
                        ->toArray();
7✔
188

189
                $this->set( '.attributes', $attributes->toArray() );
7✔
190
                $this->attributesMap = null;
7✔
191

192
                return $this;
7✔
193
        }
194

195

196
        /**
197
         * Checks if the given flag constant is valid.
198
         *
199
         * @param int $value Flag constant value
200
         */
201
        protected function checkFlags( int $value )
202
        {
203
                if( $value < \Aimeos\MShop\Order\Item\Product\Base::FLAG_NONE ||
17✔
204
                        $value > \Aimeos\MShop\Order\Item\Product\Base::FLAG_IMMUTABLE
17✔
205
                ) {
206
                        throw new \Aimeos\MShop\Order\Exception( sprintf( 'Flags "%1$s" not within allowed range', $value ) );
×
207
                }
208

209
                return $value;
17✔
210
        }
211

212

213
        /**
214
         * Returns the attribute map for the ordered products.
215
         *
216
         * @return array Associative list of type and code as key and an \Aimeos\MShop\Order\Item\Product\Attribute\Iface as value
217
         */
218
        protected function getAttributeMap() : array
219
        {
220
                if( !isset( $this->attributesMap ) )
12✔
221
                {
222
                        $this->attributesMap = [];
12✔
223

224
                        foreach( $this->get( '.attributes', [] ) as $item ) {
12✔
225
                                $this->attributesMap[$item->getType()][$item->getCode()][$item->getAttributeId()] = $item;
9✔
226
                        }
227
                }
228

229
                return $this->attributesMap;
12✔
230
        }
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc