Key::check_exists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace UAPAY;
4
5
use UAPAY\Exception;
6
7
class Key
8
{
9
    const KEYS_IN_FILES = 'files';
10
    const KEYS_IN_VALUES = 'values';
11
12
    private $type;
13
14
    public function __construct($type = null) {
15
        $this->type = empty($type) ? self::KEYS_IN_FILES : $type;
16
    }
17
18
    /**
19
     *      Get content of key file
20
     *      @param string $fname
21
     *      @param string $type
22
     *      @return string
23
     *      @throws Exception\Runtime
24
     */
25
    public function get($fname, $type)
26
    {
27
        if ($this->type === self::KEYS_IN_VALUES) {
28
            return $fname;
29
        }
30
31
        try
32
        {
33
            $this->check_exists($fname);
34
35
            $key = $this->load($fname);
36
        }
37
        catch (\Exception $e)
38
        {
39
            throw new Exception\Runtime('The file with the '.$type.' key was '.$e->getMessage().'!');
40
        }
41
42
        return $key;
43
    }
44
45
    /**
46
     *      Check if exist key file
47
     *
48
     *      @param string $fname
49
     *      @throws Exception\Runtime
50
     */
51
    protected function check_exists($fname)
52
    {
53
        if ( ! file_exists($fname))
54
        {
55
            throw new Exception\Runtime('not exists');
56
        }
57
    }
58
59
    /**
60
     *      Load key file
61
     *
62
     *      @param string $fname
63
     *      @return string
64
     *      @throws Exception\Runtime
65
     */
66
    protected function load($fname)
67
    {
68
        $key = @file_get_contents($fname);
69
        if ($key === FALSE)
70
        {
71
            throw new Exception\Runtime('not read');
72
        }
73
74
        return $key;
75
    }
76
}
77