Completed
Push — master ( 06e75e...4dd477 )
by Florian
06:57
created

mapInvoicingData()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * @category        Payone
16
 * @package         Payone_Api
17
 * @subpackage      Mapper
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone_Api
28
 * @subpackage      Mapper
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
abstract class Payone_Api_Mapper_Request_Payment_Abstract
34
    extends Payone_Api_Mapper_Request_Abstract
35
    implements Payone_Api_Mapper_Request_Interface
36
{
37
    /**
38
     * @param Payone_Api_Request_Interface $request
39
     * @return bool
40
     */
41
    protected function mapAmount(Payone_Api_Request_Interface $request)
42
    {
43
        /** @var $request Payone_Api_Request_Authorization  */
44
        $amount = $request->getAmount();
45
        $currency = $request->getCurrency();
46
47
        $mappedAmount = $this->getMapperCurrency()->mapAmountToSub($amount, $currency);
48
49
        $request->setAmount($mappedAmount);
50
51
        return true;
52
    }
53
54
    /**
55
     * @todo we should introduce an Request_Payment_Interface
56
     *
57
     * @param Payone_Api_Request_Interface $request
58
     * @return bool
59
     */
60
    protected function mapInvoicingData(Payone_Api_Request_Interface $request)
61
    {
62
        /** @var $invoicing Payone_Api_Request_Parameter_Invoicing_Transaction */
63
        $invoicing = $request->getInvoicing();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Payone_Api_Request_Interface as the method getInvoicing() does only exist in the following implementations of said interface: Payone_Api_Request_Authorization, Payone_Api_Request_Authorization_Abstract, Payone_Api_Request_Capture, Payone_Api_Request_CreateAccess, Payone_Api_Request_Debit, Payone_Api_Request_Preauthorization, Payone_Api_Request_Refund, Payone_Api_Request_UpdateAccess, Payone_Api_Request_Vauthorization.

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...
64
        if ($invoicing == null) {
65
            return false;
66
        }
67
68
        if ($invoicing->hasItems()) {
69
            /** @var $request Payone_Api_Request_Authorization  */
70
            $currency = $request->getCurrency();
71
            $invoicingItems = $invoicing->getItems();
72
            foreach ($invoicingItems as $item) {
73
                /** @var $item Payone_Api_Request_Parameter_Invoicing_Item */
74
                $price = $item->getPr();
75
                $mappedPrice = $this->getMapperCurrency()->mapAmountToSub($price, $currency);
76
                $item->setPr($mappedPrice);
77
            }
78
        }
79
80
        return true;
81
    }
82
}
83