GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CurrencyConverter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 114
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setProvider() 0 6 1
A from() 0 6 1
A to() 0 6 1
A convert() 0 15 2
A validate() 0 10 4
A reset() 0 5 1
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\CurrencyConverter;
14
15
use Thelia\CurrencyConverter\Exception\MissingProviderException;
16
use Thelia\CurrencyConverter\Provider\ProviderInterface;
17
use Thelia\Math\Number;
18
19
/**
20
 * Class Currency
21
 * @package Thelia\CurrencyConverter
22
 * @author Manuel Raynaud <[email protected]>
23
 */
24
class CurrencyConverter
25
{
26
    /**
27
     * @var ProviderInterface
28
     */
29
    protected $provider;
30
31
    /**
32
     * @var string Currency ISO Code 4217
33
     */
34
    protected $from;
35
36
    /**
37
     * @var string Currency ISO Code 4217
38
     */
39
    protected $to;
40
41
    /**
42
     * @param ProviderInterface $provider optional parameter
43
     */
44
    public function __construct(ProviderInterface $provider = null)
45
    {
46
        $this->provider = $provider;
47
    }
48
49
    /**
50
     * @param ProviderInterface $provider
51
     * @return self
52
     */
53
    public function setProvider(ProviderInterface $provider)
54
    {
55
        $this->provider = $provider;
56
57
        return $this;
58
    }
59
60
    /**
61
     * The origin currency
62
     *
63
     * @param string $value ISO Code 4217 (example : USD, EUR). See http://fr.wikipedia.org/wiki/ISO_4217
64
     * @return self
65
     */
66
    public function from($value)
67
    {
68
        $this->from = $value;
69
70
        return $this;
71
    }
72
73
    /**
74
     *
75
     * the currency desired
76
     *
77
     * @param string $value ISO Code 4217 (example : USD, EUR). See http://fr.wikipedia.org/wiki/ISO_4217
78
     * @return self
79
     */
80
    public function to($value)
81
    {
82
        $this->to = $value;
83
84
        return $this;
85
    }
86
87
    /**
88
     *
89
     * convert a currency from one to another one
90
     *
91
     * eg : $converter->from('EUR')->to('USD')->convert(Number('1'));
92
     *
93
     * @param \Thelia\Math\Number $number
94
     * @return \Thelia\Math\Number
95
     */
96
    public function convert(Number $number)
97
    {
98
        $this->validate();
99
        $this->provider->from($this->from);
100
        $this->provider->to($this->to);
101
        $result = $this->provider->convert($number);
102
103
        if (!($result instanceof \Thelia\Math\Number)) {
104
            throw new \LogicException('your provider must return a Thelia\Math\Number instance');
105
        }
106
107
        $this->reset();
108
109
        return $result;
110
    }
111
112
    /**
113
     * Verify if the conversation can be done.
114
     *
115
     * @throws MissingProviderException thrown if the provider is missing
116
     * @throws \RuntimeException thrown if to or from parameters are missing
117
     */
118
    private function validate()
119
    {
120
        if (null === $this->provider) {
121
            throw new MissingProviderException('A provider must be set for converting a currency');
122
        }
123
124
        if (null === $this->from || null === $this->to) {
125
            throw new \RuntimeException('from and to parameters must be provided');
126
        }
127
    }
128
129
    /**
130
     * remove from and to configuration.
131
     */
132
    private function reset()
133
    {
134
        $this->from = null;
135
        $this->to = null;
136
    }
137
}
138