Failed Conditions
Push — issue#767 ( 50d9b1...25878f )
by Guilherme
07:31
created

PublicKey::getPublicKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
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 LoginCidadao\OpenIDBundle\Storage;
12
13
use Gaufrette\Filesystem;
14
use Doctrine\ORM\EntityManager;
15
use Gaufrette\Exception\FileNotFound;
16
use OAuth2\Storage\PublicKeyInterface;
17
18
class PublicKey implements PublicKeyInterface
19
{
20
    /** @var EntityManager */
21
    private $em;
22
23
    /** @var Filesystem */
24
    private $filesystem;
25
26
    /** @var string */
27
    private $fileName;
28
29
    public function __construct(EntityManager $em)
30
    {
31
        $this->em = $em;
32
    }
33
34
    public function setFilesystem(Filesystem $filesystem,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
35
                                    $fileName = 'private.pem')
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 36
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
36
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
37
        $this->filesystem = $filesystem;
38
        $this->fileName   = $fileName;
39
    }
40
41
    public function getEncryptionAlgorithm($client_id = null)
42
    {
43
        return 'RS256';
44
    }
45
46
    public function getPrivateKey($client_id = null)
47
    {
48
        $key = $this->getPrivateKeyResource();
49
        openssl_pkey_export($key, $priv);
50
        return $priv;
51
    }
52
53
    public function getPublicKey($client_id = null)
54
    {
55
        $details = openssl_pkey_get_details($this->getPrivateKeyResource());
56
57
        return $details['key'];
58
    }
59
60
    /**
61
     * @return \Gaufrette\File
62
     */
63
    private function createKeys()
64
    {
65
        $priv = null;
66
        $key  = openssl_pkey_new();
67
        openssl_pkey_export($key, $priv);
68
        $file = $this->filesystem->get($this->fileName, true);
69
        $file->setContent($priv);
70
71
        return $file;
72
    }
73
74
    /**
75
     * @return resource
76
     */
77
    private function getPrivateKeyResource()
78
    {
79
        try {
80
            $file = $this->filesystem->get($this->fileName);
81
        } catch (FileNotFound $e) {
82
            $file = $this->createKeys();
83
        }
84
85
        $key = openssl_pkey_get_private($file->getContent());
86
87
        return $key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $key could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
88
    }
89
}
90