Failed Conditions
Pull Request — experimental/3.1 (#2526)
by Kentaro
40:11 queued 13:18
created

OrderHelper::createOrderItemsFromOrderDetails()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 28
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 38
ccs 0
cts 26
cp 0
crap 30
rs 8.439
1
<?php
2
3
namespace Eccube\Service;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\EntityManager;
7
use Eccube\Annotation\Inject;
8
use Eccube\Annotation\Service;
9
use Eccube\Common\Constant;
10
use Eccube\Entity\CartItem;
11
use Eccube\Entity\Customer;
12
use Eccube\Entity\CustomerAddress;
13
use Eccube\Entity\Master\OrderItemType;
14
use Eccube\Entity\Master\ShippingStatus;
15
use Eccube\Entity\Master\TaxDisplayType;
16
use Eccube\Entity\Master\TaxType;
17
use Eccube\Entity\Order;
18
use Eccube\Entity\OrderDetail;
19
use Eccube\Entity\OrderItem;
20
use Eccube\Entity\Shipping;
21
use Eccube\Repository\DeliveryFeeRepository;
22
use Eccube\Repository\DeliveryRepository;
23
use Eccube\Repository\Master\OrderItemTypeRepository;
24
use Eccube\Repository\Master\OrderStatusRepository;
25
use Eccube\Repository\Master\ShippingStatusRepository;
26
use Eccube\Repository\TaxRuleRepository;
27
use Eccube\Repository\OrderRepository;
28
use Eccube\Repository\PaymentRepository;
29
use Eccube\Util\Str;
30
31
/**
32
 * OrderやOrderに関連するエンティティを構築するクラス
33
 * namespaceやクラス名は要検討
34
 *
35
 * @package Eccube\Service
36
 * @Service
37
 */
38
class OrderHelper
39
{
40
    /**
41
     * @Inject(OrderItemTypeRepository::class)
42
     * @var OrderItemTypeRepository
43
     */
44
    protected $orderItemTypeRepository;
45
46
    /**
47
     * @Inject(OrderStatusRepository::class)
48
     * @var OrderStatusRepository
49
     */
50
    protected $orderStatusRepository;
51
52
    /**
53
     * @Inject(TaxRuleRepository::class)
54
     * @var TaxRuleRepository
55
     */
56
    protected $taxRuleRepository;
57
58
    /**
59
     * @Inject(DeliveryFeeRepository::class)
60
     * @var DeliveryFeeRepository
61
     */
62
    protected $deliveryFeeRepository;
63
64
    /**
65
     * @Inject(DeliveryRepository::class)
66
     * @var DeliveryRepository
67
     */
68
    protected $deliveryRepository;
69
70
    /**
71
     * @Inject(PaymentRepository::class)
72
     * @var PaymentRepository
73
     */
74
    protected $paymentRepository;
75
76
    /**
77
     * @Inject(OrderRepository::class)
78
     * @var OrderRepository
79
     */
80
    protected $orderRepository;
81
82
    /**
83
     * @Inject(ShippingStatusRepository::class)
84
     * @var ShippingStatusRepository
85
     */
86
    protected $shippingStatusRepository;
87
88
    /**
89
     * @Inject("orm.em")
90
     * @var EntityManager
91
     */
92
    protected $entityManager;
93
94
    /**
95
     * @Inject("config")
96
     * @var array
97
     */
98
    protected $appConfig;
99
100
    /**
101
     * 購入処理中の受注データを生成する.
102
     *
103
     * @param Customer $Customer
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
104
     * @param CustomerAddress $CustomerAddress
105
     * @param array $CartItems
0 ignored issues
show
introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
106
     * @return Order
107
     */
108 11
    public function createProcessingOrder(Customer $Customer, CustomerAddress $CustomerAddress, $CartItems)
109
    {
110 11
        $OrderStatus = $this->orderStatusRepository->find($this->appConfig['order_processing']);
111 11
        $Order = new Order($OrderStatus);
112
113
        // pre_order_idを生成
114 11
        $Order->setPreOrderId($this->createPreOrderId());
115
116
        // 顧客情報の設定
117 11
        $this->setCustomer($Order, $Customer);
118
119
        // 明細情報の設定
120 11
        $OrderItems = $this->createOrderItemsFromCartItems($CartItems);
0 ignored issues
show
Documentation introduced by
$CartItems is of type array, but the function expects a object<Doctrine\Common\C...ctions\ArrayCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $OrderItemsGroupByProductType = array_reduce($OrderItems, function($result, $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
122
            /* @var OrderItem $item */
123 11
            $productTypeId = $item->getProductClass()->getProductType()->getId();
124 11
            $result[$productTypeId][] = $item;
125 11
            return $result;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
126 11
        }, []);
127
128 11
        foreach ($OrderItemsGroupByProductType as $OrderItems) {
129 11
            $Shipping = $this->createShippingFromCustomerAddress($CustomerAddress);
130 11
            $this->addOrderItems($Order, $Shipping, $OrderItems);
131 11
            $this->setDefaultDelivery($Shipping);
132 11
            $this->entityManager->persist($Shipping);
133
        }
134
135 11
        $this->setDefaultPayment($Order);
136
137 11
        $this->entityManager->persist($Order);
138 11
        $this->entityManager->flush();
139
140 11
        return $Order;
141
    }
142
143 11
    public function createPreOrderId()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
144
    {
145
        // ランダムなpre_order_idを作成
146 View Code Duplication
        do {
147 11
            $preOrderId = sha1(Str::random(32));
148
149 11
            $Order = $this->orderRepository->findOneBy(
150
                [
151 11
                    'pre_order_id' => $preOrderId,
152 11
                    'OrderStatus' => $this->appConfig['order_processing'],
153
                ]
154
            );
155 11
        } while ($Order);
156
157 11
        return $preOrderId;
158
    }
159
160 11
    public function setCustomer(Order $Order, Customer $Customer)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
161
    {
162 11
        if ($Customer->getId()) {
163 8
            $Order->setCustomer($Customer);
164
        }
165
166 11
        $Order->copyProperties(
167 11
            $Customer,
168
            [
169 11
                'id',
170
                'create_date',
171
                'update_date',
172
                'del_flg',
173
            ]
174
        );
175
    }
176
177
    /**
178
     * @param ArrayCollection $CartItems
179
     * @return OrderItem[]
180
     */
181 11
    private function createOrderItemsFromCartItems($CartItems)
182
    {
183 11
        $ProductItemType = $this->orderItemTypeRepository->find(OrderItemType::PRODUCT);
184
        // TODO
185 11
        $TaxExclude = $this->entityManager->getRepository(TaxDisplayType::class)->find(TaxDisplayType::EXCLUDED);
186 11
        $Taxion = $this->entityManager->getRepository(TaxType::class)->find(TaxType::TAXATION);
187
188 11
        return array_map(function($item) use ($ProductItemType, $TaxExclude, $Taxion) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
189
            /* @var $item CartItem */
190
            /* @var $ProductClass \Eccube\Entity\ProductClass */
191 11
            $ProductClass = $item->getObject();
192
            /* @var $Product \Eccube\Entity\Product */
193 11
            $Product = $ProductClass->getProduct();
194 11
            $TaxRule = $this->taxRuleRepository->getByRule($Product, $ProductClass);
195
196 11
            $OrderItem = new OrderItem();
197
            $OrderItem
198 11
                ->setProduct($Product)
199 11
                ->setProductClass($ProductClass)
200 11
                ->setProductName($Product->getName())
201 11
                ->setProductCode($ProductClass->getCode())
202 11
                ->setPrice($ProductClass->getPrice02())
203 11
                ->setQuantity($item->getQuantity())
204 11
                ->setTaxRule($TaxRule->getId())
205 11
                ->setTaxRate($TaxRule->getTaxRate())
206 11
                ->setOrderItemType($ProductItemType)
207 11
                ->setTaxDisplayType($TaxExclude)
208 11
                ->setTaxType($Taxion);
209
210 11
            $ClassCategory1 = $ProductClass->getClassCategory1();
211 11
            if (!is_null($ClassCategory1)) {
212 11
                $OrderItem->setClasscategoryName1($ClassCategory1->getName());
213 11
                $OrderItem->setClassName1($ClassCategory1->getClassName()->getName());
214
            }
215 11
            $ClassCategory2 = $ProductClass->getClassCategory2();
216 11
            if (!is_null($ClassCategory2)) {
217 11
                $OrderItem->setClasscategoryName2($ClassCategory2->getName());
218 11
                $OrderItem->setClassName2($ClassCategory2->getClassName()->getName());
219
            }
220
221 11
            return $OrderItem;
222 11
        }, $CartItems->toArray());
223
    }
224
225
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$CartItems" missing
Loading history...
226
     * @deprecated
227
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
228
    public function createOrderDetailsFromCartItems($CartItems)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
229
    {
230
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
231
        $OrderDetails = [];
232
233
        foreach ($CartItems as $item) {
234
            /* @var $ProductClass \Eccube\Entity\ProductClass */
235
            $ProductClass = $item->getObject();
236
            /* @var $Product \Eccube\Entity\Product */
237
            $Product = $ProductClass->getProduct();
238
239
            $OrderDetail = new OrderDetail();
240
            $TaxRule = $this->taxRuleRepository->getByRule($Product, $ProductClass);
241
            $OrderDetail
242
                ->setProduct($Product)
243
                ->setProductClass($ProductClass)
244
                ->setProductName($Product->getName())
245
                ->setProductCode($ProductClass->getCode())
246
                ->setPrice($ProductClass->getPrice02())
247
                ->setQuantity($item->getQuantity())
248
                ->setTaxRule($TaxRule->getId())
249
                ->setTaxRate($TaxRule->getTaxRate());
250
251
            $ClassCategory1 = $ProductClass->getClassCategory1();
252
            if (!is_null($ClassCategory1)) {
253
                $OrderDetail->setClasscategoryName1($ClassCategory1->getName());
254
                $OrderDetail->setClassName1($ClassCategory1->getClassName()->getName());
255
            }
256
            $ClassCategory2 = $ProductClass->getClassCategory2();
257
            if (!is_null($ClassCategory2)) {
258
                $OrderDetail->setClasscategoryName2($ClassCategory2->getName());
259
                $OrderDetail->setClassName2($ClassCategory2->getClassName()->getName());
260
            }
261
262
            $OrderDetails[] = $OrderDetail;
263
        }
264
265
        return $OrderDetails;
266
    }
267
268
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
introduced by
Doc comment for parameter "$OrderDetails" missing
Loading history...
269
     * @deprecated
270
     */
271
    public function addOrderDetails(Order $Order, array $OrderDetails)
272
    {
273
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
274
        foreach ($OrderDetails as $OrderDetail) {
275
            $Order->addOrderDetail($OrderDetail);
276
            $OrderDetail->setOrder($Order);
277
        }
278
    }
279
280 11
    public function createShippingFromCustomerAddress(CustomerAddress $CustomerAddress)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
281
    {
282 11
        $Shipping = new Shipping();
283
        $Shipping
284 11
            ->setName01($CustomerAddress->getName01())
285 11
            ->setName02($CustomerAddress->getName02())
286 11
            ->setKana01($CustomerAddress->getKana01())
287 11
            ->setKana02($CustomerAddress->getKana02())
288 11
            ->setCompanyName($CustomerAddress->getCompanyName())
289 11
            ->setTel01($CustomerAddress->getTel01())
290 11
            ->setTel02($CustomerAddress->getTel02())
291 11
            ->setTel03($CustomerAddress->getTel03())
292 11
            ->setFax01($CustomerAddress->getFax01())
293 11
            ->setFax02($CustomerAddress->getFax02())
294 11
            ->setFax03($CustomerAddress->getFax03())
295 11
            ->setZip01($CustomerAddress->getZip01())
296 11
            ->setZip02($CustomerAddress->getZip02())
297 11
            ->setZipCode($CustomerAddress->getZip01().$CustomerAddress->getZip02())
298 11
            ->setPref($CustomerAddress->getPref())
299 11
            ->setAddr01($CustomerAddress->getAddr01())
300 11
            ->setAddr02($CustomerAddress->getAddr02());
301
302 11
        $ShippingStatus = $this->shippingStatusRepository->find(ShippingStatus::PREPARED);
303 11
        $Shipping->setShippingStatus($ShippingStatus);
304
305 11
        return $Shipping;
306
    }
307
308
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
introduced by
Doc comment for parameter "$Shipping" missing
Loading history...
309
     * @deprecated
310
     */
311
    public function addShipping(Order $Order, Shipping $Shipping)
312
    {
313
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
314
        $Order->addShipping($Shipping);
315
        $Shipping->setOrder($Order);
316
    }
317
318 11
    public function setDefaultDelivery(Shipping $Shipping)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
319
    {
320
        // 配送商品に含まれる商品種別を抽出.
321 11
        $OrderItems = $Shipping->getOrderItems();
322 11
        $ProductTypes = [];
323 11 View Code Duplication
        foreach ($OrderItems as $OrderItem) {
324 11
            $ProductClass = $OrderItem->getProductClass();
325 11
            $ProductType = $ProductClass->getProductType();
326 11
            $ProductTypes[$ProductType->getId()] = $ProductType;
327
        }
328
329
        // 商品種別に紐づく配送業者を取得.
330 11
        $Deliveries = $this->deliveryRepository->getDeliveries($ProductTypes);
331
332
        // 初期の配送業者を設定
333 11
        $Delivery = current($Deliveries);
334 11
        $Shipping->setDelivery($Delivery);
335 11
        $Shipping->setShippingDeliveryName($Delivery->getName());
336
337
        // TODO 配送料の取得方法はこれで良いか要検討
338 11
        $deliveryFee = $this->deliveryFeeRepository->findOneBy(array('Delivery' => $Delivery, 'Pref' => $Shipping->getPref()));
339 11
        $Shipping->setShippingDeliveryFee($deliveryFee->getFee());
340
    }
341
342 11
    public function setDefaultPayment(Order $Order)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
343
    {
344 11
        $OrderItems = $Order->getOrderItems();
345
346
        // 受注明細に含まれる商品種別を抽出.
347 11
        $ProductTypes = [];
348
        /** @var OrderItem $OrderItem */
349 11 View Code Duplication
        foreach ($OrderItems as $OrderItem) {
350 11
            $ProductClass = $OrderItem->getProductClass();
351 11
            if (is_null($ProductClass)) {
352
                // 商品明細のみ対象とする. 送料明細等はスキップする.
353
                continue;
354
            }
355 11
            $ProductType = $ProductClass->getProductType();
356 11
            $ProductTypes[$ProductType->getId()] = $ProductType;
357
        }
358
359
        // 商品種別に紐づく配送業者を抽出
360 11
        $Deliveries = $this->deliveryRepository->getDeliveries($ProductTypes);
361
362
        // 利用可能な支払い方法を抽出.
363 11
        $Payments = $this->paymentRepository->findAllowedPayments($Deliveries, true);
364
365
        // 初期の支払い方法を設定.
366 11
        $Payment = current($Payments);
367 11
        $Order->setPayment($Payment);
368 11
        $Order->setPaymentMethod($Payment->getMethod());
369
        // TODO CalculateChargeStrategy でセットする
370
        // $Order->setCharge($Payment->getCharge());
371
    }
372
373
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$OrderDetails" missing
Loading history...
introduced by
Doc comment for parameter "$groupByProductType" missing
Loading history...
374
     * @deprecated
375
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
376
    public function createOrderItemsFromOrderDetails($OrderDetails, $groupByProductType = true)
377
    {
378
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
379
        $OrderItems = [];
380
381
        foreach ($OrderDetails as $OrderDetail) {
382
            $Product = $OrderDetail->getProduct();
383
            $ProductClass = $OrderDetail->getProductClass();
384
            $ProductType = $ProductClass->getProductType();
385
386
            $OrderItem = new OrderItem();
387
            $OrderItem
388
                ->setProductClass($ProductClass)
389
                ->setProduct($Product)
390
                ->setProductName($Product->getName())
391
                ->setProductCode($ProductClass->getCode())
392
                ->setPrice($ProductClass->getPrice02())
393
                ->setQuantity($OrderDetail->getQuantity());
394
395
            $ClassCategory1 = $ProductClass->getClassCategory1();
396
            if (!is_null($ClassCategory1)) {
397
                $OrderItem->setClasscategoryName1($ClassCategory1->getName());
398
                $OrderItem->setClassName1($ClassCategory1->getClassName()->getName());
399
            }
400
            $ClassCategory2 = $ProductClass->getClassCategory2();
401
            if (!is_null($ClassCategory2)) {
402
                $OrderItem->setClasscategoryName2($ClassCategory2->getName());
403
                $OrderItem->setClassName2($ClassCategory2->getClassName()->getName());
404
            }
405
            if ($groupByProductType) {
406
                $OrderItems[$ProductType->getId()][] = $OrderItem;
407
            } else {
408
                $OrderItems[] = $OrderItem;
409
            }
410
        }
411
412
        return $OrderItems;
413
    }
414
415 11
    public function addOrderItems(Order $Order, Shipping $Shipping, array $OrderItems)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
416
    {
417 11
        foreach ($OrderItems as $OrderItem) {
418 11
            $Shipping->addOrderItem($OrderItem);
419 11
            $Order->addOrderItem($OrderItem);
420 11
            $OrderItem->setOrder($Order);
421 11
            $OrderItem->setShipping($Shipping);
422
        }
423
    }
424
425
}
426