Completed
Push — master ( df309f...9e12f0 )
by Rafael
08:32
created

SecureIDEncoder::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 1
    public function __construct(DefinitionRegistry $definitionRegistry, Registry $registry, $secret)
40
    {
41 1
        $this->secret = $secret;
42 1
        $this->nonce = mb_strcut($secret, 0, 16);
43
44 1
        parent::__construct($definitionRegistry, $registry);
45 1
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 1
    public function encode(NodeInterface $node): ?string
51
    {
52 1
        $nodeString = parent::encode($node);
53 1
        $ciphertext = openssl_encrypt(
54 1
            $nodeString,
55 1
            $this->cipher,
56 1
            $this->secret,
57 1
            OPENSSL_ZERO_PADDING,
58 1
            $this->nonce
59
        );
60
61 1
        return $ciphertext;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 1
    public function decode($globalId): ?NodeInterface
68
    {
69 1
        $decodedGlobalId = openssl_decrypt(
70 1
            $globalId,
71 1
            $this->cipher,
72 1
            $this->secret,
73 1
            OPENSSL_ZERO_PADDING,
74 1
            $this->nonce
75
        );
76
77 1
        return parent::decode($decodedGlobalId);
78
    }
79
}
80