Completed
Pull Request — master (#2026)
by
unknown
150:18 queued 143:21
created

DeliveryRepository::findOrCreate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 3
Ratio 15 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 5
nop 3
dl 3
loc 20
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Repository;
24
25
use Doctrine\ORM\EntityRepository;
26
27
/**
28
 * DelivRepository
29
 *
30
 * This class was generated by the Doctrine ORM. Add your own custom
31
 * repository methods below.
32
 */
33
class DeliveryRepository extends EntityRepository
34
{
35 8
    public function findOrCreate($id, $Creator = null, $ProductType = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
36
    {
37 8
        if ($id == 0) {
38 8
            $em = $this->getEntityManager();
39
40 8 View Code Duplication
            if ($Creator == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 8
                $Creator = $em->getRepository('\Eccube\Entity\Member')->findOneBy(array(), array('rank' => 'DESC'));
42
            }
43
44 8
            if ($ProductType == null) {
45 8
                $ProductType = $em->getRepository('\Eccube\Entity\Master\ProductType')->findOneBy(array(), array('rank' => 'DESC'));
46
            }
47
48 8
            $Delivery = $this->createDelivery($Creator, $ProductType);
49
        } else {
50 3
            $Delivery = $this->find($id);
51
        }
52
53 8
        return $Delivery;
54
    }
55
56
    /**
57
     * create Delivery
58
     * @param \Eccube\Entity\Member $Creator
59
     * @param \Eccube\Entity\Master\ProductType $ProductType
60
     * @return \Eccube\Entity\Delivery
61
     */
62 8 View Code Duplication
    private function createDelivery($Creator, $ProductType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 8
        $DeliveryOld = $this->findOneBy(array(), array('rank' => 'DESC'));
65
66 8
        $rank = 1;
67 8
        if ($DeliveryOld) {
68 8
            $rank = $DeliveryOld->getRank() + 1;
69
        }
70
71 8
        $Delivery = new \Eccube\Entity\Delivery();
72
        $Delivery
73 8
            ->setRank($rank)
74 8
            ->setDelFlg(0)
75 8
            ->setCreator($Creator)
76 8
            ->setProductType($ProductType);
77
78 8
        return $Delivery;
79
    }
80
81
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
82
     * 複数の商品種別から配送業者を取得
83
     *
84
     * @param $productTypes
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
85
     * @return array
86
     */
87 93
    public function getDeliveries($productTypes)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
88
    {
89
90 93
        $deliveries = $this->createQueryBuilder('d')
91 93
            ->where('d.ProductType in (:productTypes)')
92 93
            ->setParameter('productTypes', $productTypes)
93 93
            ->getQuery()
94 93
            ->getResult();
95
96 93
        return $deliveries;
97
98
    }
99
100
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$payments" missing
Loading history...
introduced by
Doc comment for parameter "$productTypes" missing
Loading history...
101
     * 選択可能な配送業者を取得
102
     *
103
     * @param $payments
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
104
     * @return array
105
     */
106 2
    public function findAllowedDeliveries($productTypes, $payments)
107
    {
108
109 2
        $d = $this->getDeliveries($productTypes);
110 2
        $arr = array();
111
112 2
        foreach ($d as $Delivery) {
113 1
            $paymentOptions = $Delivery->getPaymentOptions();
114
115 1
            foreach ($paymentOptions as $PaymentOption) {
116 1
                foreach ($payments as $Payment) {
117
                    if ($PaymentOption->getPayment()->getId() == $Payment['id']) {
118
                        $arr[$Delivery->getId()] = $Delivery;
119 2
                        break;
120
                    }
121
                }
122
            }
123
        }
124
125 2
        return array_values($arr);
126
127
    }
128
}
129