Completed
Push — master ( 2bc6ba...1ab902 )
by Francesco
09:17
created

CryptoLoader::setResource()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 9
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
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 Mes\Security\CryptoBundle\Loader;
13
14
/**
15
 * Class CryptoLoader.
16
 */
17
class CryptoLoader extends AbstractCryptoLoader
18
{
19
    private $resource;
20
21
    private $config;
22
23
    /**
24
     * @param mixed $resource
25
     *
26
     * @throws \RuntimeException|\InvalidArgumentException
27
     */
28 6
    public function setResource($resource)
29
    {
30 6
        $this->resource = $resource;
31
32 6
        $this->config = array();
33
34 6
        $conditionSatisfied = (is_file($resource) && is_readable($resource)) && 'crypto' === pathinfo($this->resource, PATHINFO_EXTENSION);
35
36 6
        if ($conditionSatisfied) {
37 6
            $result = parse_ini_file($this->resource, true);
38
39 6
            if (false === $result || array() === $result) {
40 1
                throw new \RuntimeException(sprintf('The "%s" file is not correctly formatted. ini format is expected.', $this->resource));
41
            }
42
43 6
            $this->config = $result;
44
        } else {
45 1
            throw new \InvalidArgumentException(sprintf('The "%s" file is not valid.', $this->resource));
46
        }
47 6
    }
48
49
    /**
50
     * @return string
51
     *
52
     * @throws \UnexpectedValueException
53
     */
54 2
    public function loadKey()
55
    {
56
        try {
57 2
            return $this->config['crypto']['key'];
58 1
        } catch (\Exception $ex) {
59 1
            throw new \UnexpectedValueException('The configuration value "key" is missing.');
60
        }
61
    }
62
63
    /**
64
     * @return string
65
     *
66
     * @throws \UnexpectedValueException
67
     */
68 3
    public function loadSecret()
69
    {
70
        try {
71 3
            return $this->config['crypto']['secret'];
72 1
        } catch (\Exception $ex) {
73 1
            throw new \UnexpectedValueException('The configuration value "secret" is missing.');
74
        }
75
    }
76
}
77