Completed
Push — master ( a4551f...f2d5c9 )
by Paweł
19:10 queued 05:45
created

CryptedGatewayConfigTypeExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B buildForm() 0 31 4
A getExtendedType() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\PayumBundle\Form\Extension;
13
14
use Payum\Core\Security\CryptedInterface;
15
use Payum\Core\Security\CypherInterface;
16
use Sylius\Bundle\PayumBundle\Form\Type\GatewayConfigType;
17
use Symfony\Component\Form\AbstractTypeExtension;
18
use Symfony\Component\Form\FormBuilderInterface;
19
use Symfony\Component\Form\FormEvent;
20
use Symfony\Component\Form\FormEvents;
21
22
/**
23
 * @author Kamil Kokot <[email protected]>
24
 */
25
final class CryptedGatewayConfigTypeExtension extends AbstractTypeExtension
26
{
27
    /**
28
     * @var CypherInterface|null
29
     */
30
    private $cypher;
31
32
    /**
33
     * @param CypherInterface|null $cypher
34
     */
35
    public function __construct(CypherInterface $cypher = null)
36
    {
37
        $this->cypher = $cypher;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function buildForm(FormBuilderInterface $builder, array $options)
44
    {
45
        if (null === $this->cypher) {
46
            return;
47
        }
48
49
        $builder
50
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
51
                $gatewayConfig = $event->getData();
52
53
                if (!$gatewayConfig instanceof CryptedInterface) {
54
                    return;
55
                }
56
57
                $gatewayConfig->decrypt($this->cypher);
0 ignored issues
show
Bug introduced by
It seems like $this->cypher can be null; however, decrypt() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
59
                $event->setData($gatewayConfig);
60
            })
61
            ->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
62
                $gatewayConfig = $event->getData();
63
64
                if (!$gatewayConfig instanceof CryptedInterface) {
65
                    return;
66
                }
67
68
                $gatewayConfig->encrypt($this->cypher);
0 ignored issues
show
Bug introduced by
It seems like $this->cypher can be null; however, encrypt() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
70
                $event->setData($gatewayConfig);
71
            })
72
        ;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getExtendedType()
79
    {
80
        return GatewayConfigType::class;
81
    }
82
}
83