Completed
Pull Request — 4.0 (#3658)
by Kentaro
06:26
created

DeliveryRepository::findOrCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
ccs 14
cts 15
cp 0.9333
crap 3.0026
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Eccube\Entity\Delivery;
17
use Eccube\Entity\Master\SaleType;
18
use Eccube\Entity\Payment;
19
use Symfony\Bridge\Doctrine\RegistryInterface;
20
21
/**
22
 * DelivRepository
23
 *
24
 * This class was generated by the Doctrine ORM. Add your own custom
25
 * repository methods below.
26
 */
27
class DeliveryRepository extends AbstractRepository
28
{
29 219
    public function __construct(RegistryInterface $registry)
30
    {
31 219
        parent::__construct($registry, Delivery::class);
32
    }
33
34
    /**
35
     * 複数の販売種別から配送業者を取得
36
     *
37
     * @param $saleTypes
38
     *
39
     * @return array
40
     */
41 5
    public function getDeliveries($saleTypes)
42
    {
43 5
        $deliveries = $this->createQueryBuilder('d')
44 5
            ->where('d.SaleType in (:saleTypes)')
45
            ->andWhere('d.visible = :visible')
46
            ->setParameter('saleTypes', $saleTypes)
47 5
            ->setParameter('visible', true)
48 5
            ->orderBy('d.sort_no', 'DESC')
49
            ->getQuery()
50 5
            ->getResult();
51
52 5
        return $deliveries;
53 5
    }
54 5
55
    /**
56
     * 選択可能な配送業者を取得
57 5
     *
58
     * @param $saleTypes
59 5
     * @param $payments
60 5
     *
61 5
     * @return array
62
     */
63
    public function findAllowedDeliveries($saleTypes, $payments)
64
    {
65
        $d = $this->getDeliveries($saleTypes);
66 5
        $arr = [];
67
68
        foreach ($d as $Delivery) {
69
            $paymentOptions = $Delivery->getPaymentOptions();
70
71
            foreach ($paymentOptions as $PaymentOption) {
72
                foreach ($payments as $Payment) {
73
                    if ($PaymentOption->getPayment() instanceof Payment) {
74
                        if ($PaymentOption->getPayment()->getId() == $Payment['id']) {
75
                            $arr[$Delivery->getId()] = $Delivery;
76 57
                            break;
77
                        }
78 57
                    }
79 57
                }
80 57
            }
81 57
        }
82 57
83 57
        return array_values($arr);
84 57
    }
85
}
86