Failed Conditions
Pull Request — experimental/sf (#3236)
by Kentaro
144:19 queued 116:23
created

UsePointProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
ccs 0
cts 40
cp 0
wmc 11
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 16 3
A addPointDiscountItem() 0 33 2
A usePointToPrice() 0 4 1
A removePointDiscountItem() 0 9 4
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\Service\PurchaseFlow\Processor;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\ItemHolderInterface;
19
use Eccube\Entity\Master\OrderItemType;
20
use Eccube\Entity\Master\TaxDisplayType;
21
use Eccube\Entity\Master\TaxType;
22
use Eccube\Entity\Order;
23
use Eccube\Entity\OrderItem;
24
use Eccube\Service\PurchaseFlow\ItemHolderProcessor;
25
use Eccube\Service\PurchaseFlow\ProcessResult;
26
use Eccube\Service\PurchaseFlow\PurchaseContext;
27
28
/**
29
 * 使用ポイント値引明細追加.
30
 */
31
class UsePointProcessor implements ItemHolderProcessor
32
{
33
    /**
34
     * @var EntityManagerInterface
35
     */
36
    protected $entityManager;
37
38
    /**
39
     * @var BaseInfo
40
     */
41
    protected $BaseInfo;
42
43
    /**
44
     * UsePointProcessor constructor.
45
     *
46
     * @param EntityManagerInterface $entityManager
47
     * @param BaseInfo $BaseInfo
48
     */
49
    public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo)
50
    {
51
        $this->entityManager = $entityManager;
52
        $this->BaseInfo = $BaseInfo;
53
    }
54
55
    /**
56
     * @param ItemHolderInterface $itemHolder
57
     * @param PurchaseContext     $context
58
     *
59
     * @return ProcessResult
60
     */
61
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
62
    {
63
        if ($itemHolder->getUsePoint() > 0) {
64
            if ($itemHolder->getUsePoint() > $context->getUser()->getPoint()) {
65
                // TODO カートに戻さないように修正する
66
                return ProcessResult::error('利用ポイントが所有ポイントを上回っています.');
67
            }
68
69
            // XXX delete/insert ではなく, update/insert の方がいいかも
70
            $this->removePointDiscountItem($itemHolder);
71
72
            return $this->addPointDiscountItem($itemHolder);
73
        }
74
75
        return ProcessResult::success();
76
    }
77
78
    /**
79
     * 明細追加処理
80
     *
81
     * @param ItemHolderInterface $itemHolder
82
     */
83
    protected function addPointDiscountItem(ItemHolderInterface $itemHolder)
84
    {
85
        $DiscountType = $this->entityManager
86
            ->find(OrderItemType::class, OrderItemType::DISCOUNT);
87
        // TODO
88
        $TaxInclude = $this->entityManager
89
            ->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
90
        $Taxion = $this->entityManager
91
            ->find(TaxType::class, TaxType::TAXATION);
92
93
        /** @var Order $Order */
94
        $Order = $itemHolder;
95
96
        $priceOfUsePoint = $this->usePointToPrice($Order->getUsePoint());
97
        if (($itemHolder->getTotal() + $priceOfUsePoint) < 0) {
98
            // TODO カートに戻さないように修正する
99
            // TODO 送料・手数料も考慮する
100
            return ProcessResult::error('利用ポイントがお支払い金額を上回っています.');
101
        }
102
        $OrderItem = new OrderItem();
103
        $OrderItem->setProductName('ポイント値引')
0 ignored issues
show
Bug introduced by
The method setPriceIncTax() does not exist on Eccube\Entity\OrderItem. Did you maybe mean setPrice()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
104
            ->setPrice($priceOfUsePoint)
105
            ->setPriceIncTax($priceOfUsePoint)
106
            ->setTaxRate(8)
107
            ->setQuantity(1)
108
            ->setOrderItemType($DiscountType)
109
            ->setTaxDisplayType($TaxInclude)
110
            ->setTaxType($Taxion)
111
            ->setOrder($itemHolder);
112
        $itemHolder->addItem($OrderItem);
113
114
        return ProcessResult::success();
115
    }
116
117
    /**
118
     * 既存のポイント明細を削除する.
119
     *
120
     * @param ItemHolderInterface $itemHolder
121
     */
122
    protected function removePointDiscountItem(ItemHolderInterface $itemHolder)
123
    {
124
        foreach ($itemHolder->getItems() as $item) {
125
            if ($item->isDiscount() && $item->getProductName() == 'ポイント値引') {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Eccube\Entity\ItemInterface as the method getProductName() does only exist in the following implementations of said interface: Eccube\Entity\OrderItem.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
126
                $itemHolder->removeOrderItem($item);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Eccube\Entity\ItemHolderInterface as the method removeOrderItem() does only exist in the following implementations of said interface: Eccube\Entity\Order.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
127
                $this->entityManager->remove($item);
128
            }
129
        }
130
    }
131
132
    /**
133
     * 利用ポイントを単価に換算する.
134
     *
135
     * @param integer $usePoint 利用ポイント
136
     *
137
     * @return integer
138
     */
139
    protected function usePointToPrice($usePoint)
140
    {
141
        return ($usePoint * $this->BaseInfo->getPointConversionRate()) * -1;
142
    }
143
}
144