Converter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 36
c 4
b 0
f 0
dl 0
loc 121
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A validateInput() 0 8 3
A cache() 0 7 1
A cconv() 0 10 1
A checkInput() 0 16 6
1
<?php
2
3
namespace CConverter;
4
5
use CConverter\Calculate\Calculate;
6
use CConverter\Cache\Cache;
7
8
class Converter implements ConverterInterface
9
{
10
    /**
11
     * @var fixer.io api key string
0 ignored issues
show
Documentation Bug introduced by
The doc comment fixer.io at position 0 could not be parsed: Unknown type name 'fixer.io' at position 0 in fixer.io.
Loading history...
12
     */
13
    protected $api_key;
14
15
    /**
16
     * sortage api key lenght 
17
     *
18
     * @var int
19
     */
20
    public $api_length = 32;
21
22
    /**
23
     * sortage cache or not 
24
     *
25
     * @var bool
26
     */
27
    public $isCache = false;
28
29
    /**
30
     * contains storage cache in minutes
31
     *
32
     * @var int
33
     */
34
    public $cacheTime = 60;
35
36
    /**
37
     * connect with API fixer.io
38
     *
39
     * @param  string $api_key
40
     * @return bool
41
     */
42
    public function __construct($api_key)
43
    {
44
        if(strlen($api_key)==$this->api_length){
45
            $this->api_key = $api_key;
46
        }else{
47
            throw new Exception\InvalidArgumentException('Invalid length of API KEY');
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * initial currency conversion
55
     *
56
     * @param  string|array $from
57
     * @param  string|array $to
58
     * @param  float        $amount
59
     * @param  bool|int     $short
60
     * @return string|array
61
     */
62
    public function cconv($from, $to, $amount = 1, $short = false)
63
    {
64
        $getFrom = $this->checkInput($from);
65
        $getTo = $this->checkInput($to);
66
67
        $calculate = new Calculate($this->api_key);
68
69
        $output = $calculate->getValues($getFrom, $getTo, $amount, $short, $this->isCache, $this->cacheTime);
70
71
        return $output;
72
    }
73
74
    /**
75
     * validate input data
76
     *
77
     * @param  string $data
78
     * @return bool
79
     */
80
    public function validateInput($data)
81
    {
82
        if(strlen($data)==2 || strlen($data)==3){
83
            $output = ConverterCurrency::getCurrency($data);
84
        }else{
85
            $output = false;
86
        }
87
        return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $output also could return the type string which is incompatible with the documented return type boolean.
Loading history...
88
    }
89
90
    /**
91
     * initial cache or not
92
     *
93
     * @param  bool	 $is
94
     * @param  int	 $time
95
     * @return bool
96
     */
97
    public function cache($is = false, $time = 60)
98
    {
99
        $parm = Cache::setCache($is, $time);
100
        $this->isCache = $parm; 
101
        $this->cacheTime = $time;
102
103
        return true;
104
    }
105
106
    /**
107
     * check input data for validator
108
     *
109
     * @param  string|array	 $data
110
     * @return string|array
111
     * @throws Exception\InvalidArgumentException
112
     */
113
    protected function checkInput($data)
114
    {
115
        if(is_string($data)){
116
            $value = $this->validateInput($data);
117
            if($value===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter a valid country or currency name');
118
        }else if(is_array($data)){
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
119
            $value = array();
120
            foreach($data as $single){
121
                $opt = $this->validateInput($single);
122
                if($opt===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names');
123
                array_push($value, $opt);
124
            }
125
        }else{
126
            throw new Exception\InvalidArgumentException('Invalid data. Please use string or array value');
127
        }
128
        return $value;
129
    }
130
}
131