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

aimeos / aimeos-core / e67a5171-b485-4a8f-9441-93d8ff5aa19b

15 Aug 2024 11:37AM UTC coverage: 91.967% (+0.03%) from 91.933%
e67a5171-b485-4a8f-9441-93d8ff5aa19b

push

circleci

aimeos
Simplified order service manager

111 of 117 new or added lines in 4 files covered. (94.87%)

5 existing lines in 3 files now uncovered.

10246 of 11141 relevant lines covered (91.97%)

61.76 hits per line

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

90.2
/src/MShop/Order/Item/Service/Base.php
1
<?php
2

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

10

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

13

14
/**
15
 * Order service item abstract class defining available types.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
abstract class Base extends \Aimeos\MShop\Common\Item\Base implements Iface
21
{
22
        /**
23
         * Delivery service.
24
         */
25
        const TYPE_DELIVERY = 'delivery';
26

27
        /**
28
         * Payment service.
29
         */
30
        const TYPE_PAYMENT = 'payment';
31

32

33
        private ?array $attributesMap = null;
34

35

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

46
                foreach( $attributes as $attrItem ) {
10✔
47
                        $this->setAttributeItem( $attrItem );
9✔
48
                }
49

50
                return $this;
10✔
51
        }
52

53

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

66
                foreach( (array) $type as $key )
13✔
67
                {
68
                        if( isset( $map[$key][$code] ) )
13✔
69
                        {
70
                                foreach( $map[$key][$code] as $item ) {
10✔
71
                                        $list[] = $item->getValue();
10✔
72
                                }
73
                        }
74
                }
75

76
                return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
13✔
77
        }
78

79

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

93
                foreach( (array) $type as $key )
6✔
94
                {
95
                        if( isset( $map[$key][$code] ) )
6✔
96
                        {
97
                                foreach( $map[$key][$code] as $item ) {
6✔
98
                                        $list[] = $item;
6✔
99
                                }
100
                        }
101
                }
102

103
                return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
6✔
104
        }
105

106

107
        /**
108
         * Returns the list of attribute items for the ordered service.
109
         *
110
         * @param string|null $type Filters returned attributes by the given type or null for no filtering
111
         * @return \Aimeos\Map List of attribute items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
112
         */
113
        public function getAttributeItems( string $type = null ) : \Aimeos\Map
114
        {
115
                if( $type === null ) {
21✔
116
                        return map( $this->get( '.attributes', [] ) );
19✔
117
                }
118

119
                $list = [];
2✔
120

121
                foreach( $this->get( '.attributes', [] ) as $attrItem )
2✔
122
                {
123
                        if( $attrItem->getType() === $type ) {
2✔
124
                                $list[] = $attrItem;
1✔
125
                        }
126
                }
127

128
                return map( $list );
2✔
129
        }
130

131

132
        /**
133
         * Adds or replaces the attribute item in the list of service attributes.
134
         *
135
         * @param \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item Service attribute item
136
         * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
137
         */
138
        public function setAttributeItem( \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
139
        {
140
                $this->getAttributeMap();
23✔
141

142
                $type = $item->getType();
23✔
143
                $code = $item->getCode();
23✔
144
                $attrId = $item->getAttributeId();
23✔
145

146
                if( !isset( $this->attributesMap[$type][$code][$attrId] ) )
23✔
147
                {
148
                        $this->set( '.attributes', map( $this->get( '.attributes', [] ) )->push( $item ) );
23✔
149
                        $this->attributesMap[$type][$code][$attrId] = $item;
23✔
150
                }
151

152
                $this->attributesMap[$type][$code][$attrId]->setValue( $item->getValue() );
23✔
153
                $this->setModified();
23✔
154

155
                return $this;
23✔
156
        }
157

158

159
        /**
160
         * Sets the new list of attribute items for the service.
161
         *
162
         * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Service\Attribute\Iface[] $attributes List of order service attribute items
163
         * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
164
         */
165
        public function setAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Service\Iface
166
        {
167
                ( $attributes = map( $attributes ) )->implements( \Aimeos\MShop\Order\Item\Service\Attribute\Iface::class, true );
6✔
168

169
                $this->set( '.attributes', $attributes );
6✔
170
                $this->attributesMap = null;
6✔
171

172
                return $this;
6✔
173
        }
174

175

176
        /**
177
         * Adds a new transaction to the service.
178
         *
179
         * @param \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item Transaction item
180
         * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
181
         */
182
        public function addTransaction( \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
183
        {
NEW
184
                return $this->set( '.transactions', map( $this->get( '.transactions', [] ) )->push( $item ) );
×
185

186
                $this->transactions[] = $item;
187
                $this->setModified();
188

189
                return $this;
190
        }
191

192

193
        /**
194
         * Returns the list of transactions items for the service.
195
         *
196
         * @param string|null $type Filters returned transactions by the given type or null for no filtering
197
         * @return \Aimeos\Map List of transaction items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
198
         */
199
        public function getTransactions( string $type = null ) : \Aimeos\Map
200
        {
201
                return map( $this->get( '.transactions', [] ) );
8✔
202
        }
203

204

205
        /**
206
         * Sets the new list of transactions items for the service.
207
         *
208
         * @param iterable $list List of order service transaction items
209
         * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
210
         */
211
        public function setTransactions( iterable $list ) : \Aimeos\MShop\Order\Item\Service\Iface
212
        {
213
                return $this->set( '.transactions', $list );
1✔
214
        }
215

216

217
        /**
218
         * Checks if the given flag constant is valid.
219
         *
220
         * @param int $value Flag constant value
221
         */
222
        protected function checkFlags( int $value )
223
        {
NEW
224
                if( $value < \Aimeos\MShop\Order\Item\Service\Base::FLAG_NONE ||
×
NEW
225
                        $value > \Aimeos\MShop\Order\Item\Service\Base::FLAG_IMMUTABLE
×
226
                ) {
NEW
227
                        throw new \Aimeos\MShop\Order\Exception( sprintf( 'Flags "%1$s" not within allowed range', $value ) );
×
228
                }
229

NEW
230
                return $value;
×
231
        }
232

233

234
        /**
235
         * Returns the attribute map for the ordered services.
236
         *
237
         * @return array Associative list of type and code as key and an \Aimeos\MShop\Order\Item\Service\Attribute\Iface as value
238
         */
239
        protected function getAttributeMap() : array
240
        {
241
                if( !isset( $this->attributesMap ) )
30✔
242
                {
243
                        $this->attributesMap = [];
30✔
244

245
                        foreach( $this->get( '.attributes', [] ) as $item ) {
30✔
246
                                $this->attributesMap[$item->getType()][$item->getCode()][$item->getAttributeId()] = $item;
6✔
247
                        }
248
                }
249

250
                return $this->attributesMap;
30✔
251
        }
252
}
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