AbstractSubForm::generateClientToken()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 22
rs 9.8666
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\Yves\Braintree\Form;
9
10
use Braintree\ClientToken;
11
use Braintree\Configuration;
12
use Braintree\Gateway;
13
use Generated\Shared\Transfer\BraintreePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer 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\Shared\Config\Config;
15
use Spryker\Yves\StepEngine\Dependency\Form\AbstractSubFormType;
16
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
17
use Spryker\Yves\StepEngine\Dependency\Form\SubFormProviderNameInterface;
18
use SprykerEco\Shared\Braintree\BraintreeConfig;
19
use SprykerEco\Shared\Braintree\BraintreeConstants;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
abstract class AbstractSubForm extends AbstractSubFormType implements SubFormInterface, SubFormProviderNameInterface
23
{
24
    protected const CLIENT_TOKEN = 'clientToken';
25
26
    /**
27
     * @var string
28
     */
29
    protected static $clientToken;
30
31
    /**
32
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
33
     *
34
     * @return void
35
     */
36
    public function configureOptions(OptionsResolver $resolver)
37
    {
38
        $resolver->setDefaults([
39
            'data_class' => BraintreePaymentTransfer::class,
40
            SubFormInterface::OPTIONS_FIELD_NAME => [],
41
        ]);
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getProviderName()
48
    {
49
        return BraintreeConfig::PROVIDER_NAME;
50
    }
51
52
    /**
53
     * Generate client token and store it in static class attribute to ensure
54
     * we do not invoke the API twice here for multiple sub forms.
55
     *
56
     * @return string
57
     */
58
    protected function generateClientToken()
59
    {
60
        if (static::$clientToken) {
61
            return static::$clientToken;
62
        }
63
64
        if (Config::hasKey(BraintreeConstants::FAKE_CLIENT_TOKEN)) {
65
            static::$clientToken = Config::get(BraintreeConstants::FAKE_CLIENT_TOKEN);
66
67
            return static::$clientToken;
68
        }
69
70
        $gateway = new Gateway([
71
            'environment' => Config::get(BraintreeConstants::ENVIRONMENT),
72
            'merchantId' => Config::get(BraintreeConstants::MERCHANT_ID),
73
            'publicKey' => Config::get(BraintreeConstants::PUBLIC_KEY),
74
            'privateKey' => Config::get(BraintreeConstants::PRIVATE_KEY),
75
        ]);
76
77
        static::$clientToken = $gateway->clientToken()->generate();
78
79
        return static::$clientToken;
80
    }
81
}
82