Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

SecureIDEncoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 61
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A decode() 0 11 1
A encode() 0 12 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Encoder;
12
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
15
use Ynlo\GraphQLBundle\Model\NodeInterface;
16
17
class SecureIDEncoder extends SimpleIDEncoder
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $secret;
23
24
    /**
25
     * @var string
26
     */
27
    protected $nonce;
28
29
    /**
30
     * @var string
31
     */
32
    protected $cipher = "aes-128-ctr";
33
34
    /**
35
     * @param DefinitionRegistry $definitionRegistry
36
     * @param Registry           $registry
37
     * @param string             $secret
38
     */
39 22
    public function __construct(DefinitionRegistry $definitionRegistry, Registry $registry, $secret)
40
    {
41 22
        $this->secret = $secret;
42 22
        $this->nonce = mb_strcut($secret, 0, 16);
43
44 22
        parent::__construct($definitionRegistry, $registry);
45 22
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 19
    public function encode(NodeInterface $node): ?string
51
    {
52 19
        $nodeString = parent::encode($node);
53 19
        $ciphertext = openssl_encrypt(
54 19
            $nodeString,
55 19
            $this->cipher,
56 19
            $this->secret,
57 19
            OPENSSL_ZERO_PADDING,
58 19
            $this->nonce
59
        );
60
61 19
        return $ciphertext;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 12
    public function decode($globalId): ?NodeInterface
68
    {
69 12
        $decodedGlobalId = openssl_decrypt(
70 12
            $globalId,
71 12
            $this->cipher,
72 12
            $this->secret,
73 12
            OPENSSL_ZERO_PADDING,
74 12
            $this->nonce
75
        );
76
77 12
        return parent::decode($decodedGlobalId);
78
    }
79
}
80