AuthorizeTransaction::findTransaction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Business\Payment\Transaction;
9
10
use Braintree\Exception\NotFound;
11
use Braintree\Result\Error;
12
use Braintree\Result\Successful;
13
use Braintree\Transaction as BraintreeTransaction;
14
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
15
16
class AuthorizeTransaction extends AbstractTransaction
17
{
18
    /**
19
     * @return string
20
     */
21
    protected function getTransactionType()
22
    {
23
        return ApiConstants::SALE;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    protected function getTransactionCode()
30
    {
31
        return ApiConstants::TRANSACTION_CODE_AUTHORIZE;
32
    }
33
34
    /**
35
     * @return \Braintree\Result\Error|\Braintree\Transaction
36
     */
37
    public function doTransaction()
38
    {
39
        return $this->authorize();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->authorize() also could return the type Braintree\Result\Successful which is incompatible with the documented return type Braintree\Result\Error|Braintree\Transaction.
Loading history...
40
    }
41
42
    /**
43
     * @param \Braintree\Transaction $response
44
     *
45
     * @return bool
46
     */
47
    protected function isTransactionSuccessful($response)
48
    {
49
        return ($response->success && $response->transaction->processorResponseCode === ApiConstants::PAYMENT_CODE_AUTHORIZE_SUCCESS);
50
    }
51
52
    /**
53
     * @return \Braintree\Result\Successful|\Braintree\Result\Error
54
     */
55
    protected function authorize()
56
    {
57
        try {
58
            $transaction = $this->findTransaction();
59
        } catch (NotFound $e) {
60
            $message = sprintf('Could not find payment with the transaction id "%s"', $this->getTransactionIdentifier());
61
62
            return new Error(['message' => $message, 'errors' => []]);
63
        }
64
65
        return new Successful($transaction);
0 ignored issues
show
Bug introduced by
$transaction of type Braintree\Transaction is incompatible with the type array|null expected by parameter $objsToReturn of Braintree\Result\Successful::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return new Successful(/** @scrutinizer ignore-type */ $transaction);
Loading history...
66
    }
67
68
    /**
69
     * @return \Braintree\Transaction
70
     */
71
    protected function findTransaction()
72
    {
73
        return BraintreeTransaction::find($this->getTransactionIdentifier());
74
    }
75
}
76