Passed
Pull Request — master (#9)
by Volodymyr
05:00
created

BraintreePreCheckPlugin::checkCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Braintree\Communication\Plugin\Checkout;
9
10
use Generated\Shared\Transfer\BraintreeTransactionResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sactionResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\CheckoutErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutErrorTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\CheckoutResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Spryker\Zed\Kernel\Communication\AbstractPlugin as BaseAbstractPlugin;
15
use Spryker\Zed\Payment\Dependency\Plugin\Checkout\CheckoutPreCheckPluginInterface;
16
17
/**
18
 * @method \SprykerEco\Zed\Braintree\Business\BraintreeFacadeInterface getFacade()
19
 */
20
class BraintreePreCheckPlugin extends BaseAbstractPlugin implements CheckoutPreCheckPluginInterface
21
{
22
    /**
23
     * Specification:
24
     * - Checks a condition before the order is saved. If the condition fails, an error is added to the response transfer and 'false' is returned.
25
     * - Check could be passed (returns 'true') along with errors added to the checkout response.
26
     * - Quote transfer should not be changed
27
     * - Don't use this plugin to write to a DB
28
     *
29
     * @api
30
     *
31
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
32
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
33
     *
34
     * @return bool
35
     */
36
    public function execute(
37
        QuoteTransfer $quoteTransfer,
38
        CheckoutResponseTransfer $checkoutResponseTransfer
39
    ) {
40
        $braintreeTransactionResponseTransfer = $this->getFacade()->preCheckPayment($quoteTransfer);
41
        $isPassed = $this->checkForErrors($braintreeTransactionResponseTransfer, $checkoutResponseTransfer);
42
43
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
44
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Spryker\Zed\Payment\Depe...ginInterface::execute() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
45
        }
46
47
        $quoteTransfer->getPayment()->getBraintree()
48
            ->setTransactionId($braintreeTransactionResponseTransfer->getTransactionId());
49
50
        return $isPassed;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $isPassed returns the type boolean which is incompatible with the return type mandated by Spryker\Zed\Payment\Depe...ginInterface::execute() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
51
    }
52
53
    /**
54
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
55
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
56
     *
57
     * @return bool
58
     */
59
    protected function checkForErrors(
60
        BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer,
61
        CheckoutResponseTransfer $checkoutResponseTransfer
62
    ) {
63
        if ($braintreeTransactionResponseTransfer->getIsSuccess()) {
64
            return true;
65
        }
66
67
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: 500;
68
        $error = new CheckoutErrorTransfer();
69
        $error
70
            ->setErrorCode($errorCode)
71
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
72
        $checkoutResponseTransfer->addError($error);
73
74
        return false;
75
    }
76
}
77