Passed
Push — issue#767 ( 902b86...e6d68a )
by Guilherme
05:21
created

PublicKey::getPrivateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
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 Gaufrette\Exception\FileNotFound;
15
use OAuth2\Storage\PublicKeyInterface;
16
17
class PublicKey implements PublicKeyInterface
18
{
19
    /** @var Filesystem */
20
    private $filesystem;
21
22
    /** @var string */
23
    private $fileName;
24
25 1
    public function setFilesystem(Filesystem $filesystem, $fileName = 'private.pem')
26
    {
27 1
        $this->filesystem = $filesystem;
28 1
        $this->fileName = $fileName;
29 1
    }
30
31 1
    public function getEncryptionAlgorithm($client_id = null)
32
    {
33 1
        return 'RS256';
34
    }
35
36 1
    public function getPrivateKey($client_id = null)
37
    {
38 1
        $key = $this->getPrivateKeyResource();
39 1
        openssl_pkey_export($key, $priv);
40
41 1
        return $priv;
42
    }
43
44 1
    public function getPublicKey($client_id = null)
45
    {
46 1
        $details = openssl_pkey_get_details($this->getPrivateKeyResource());
47
48 1
        return $details['key'];
49
    }
50
51
    /**
52
     * @return \Gaufrette\File
53
     */
54 1
    private function createKeys()
55
    {
56 1
        $priv = null;
57 1
        $key = openssl_pkey_new();
58 1
        openssl_pkey_export($key, $priv);
59 1
        $file = $this->filesystem->get($this->fileName, true);
60 1
        $file->setContent($priv);
61
62 1
        return $file;
63
    }
64
65
    /**
66
     * @return resource
67
     */
68 1
    private function getPrivateKeyResource()
69
    {
70
        try {
71 1
            $file = $this->filesystem->get($this->fileName);
72 1
        } catch (FileNotFound $e) {
73 1
            $file = $this->createKeys();
74
        }
75
76 1
        $key = openssl_pkey_get_private($file->getContent());
77
78 1
        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...
79
    }
80
}
81