ServiceFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 57
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getService() 0 23 3
1
<?php
2
3
namespace MoneyMan;
4
use MoneyMan\Exception\UnsupportedExchangeServiceProvidedException;
5
use Swap\Swap;
6
7
/**
8
 * Service Factory Class
9
 *
10
 * The class is used to create the appropriate \Swap\Swap object
11
 * needed for the \MoneyMan\Exchange class.
12
 *
13
 * @package MoneyMan
14
 */
15
class ServiceFactory
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
16
{
17
    /**
18
     * List of usable exchange rate services.
19
     * @var string(s)
20
     */
21
    const FIXER  = 'fixer';
22
    const YAHOO  = 'yahoo';
23
24
    /**
25
     * A list of all usable exchange rate services.
26
     * @var array
27
     */
28
    private static $SERVICES = [
29
        self::FIXER,
30
        self::YAHOO
31
    ];
32
33
    /**
34
     * Constructor
35
     *
36
     * This class cannot be instantiated.
37
     */
38
    private function __construct() {}
39
40
    /**
41
     * Create and return a \Swap\Swap object based on the provided
42
     * service name.
43
     *
44
     * @param string $service  The name of the service to use.
45
     *
46
     * @return \Swap\Swap
47
     */
48 3
    public static function getService($service)
49
    {
50
        switch ($service) {
51
52
            // Fixer
53 3
            case self::FIXER:
54 1
                return (new \Swap\Builder())
55 1
                    ->add('fixer')
56 1
                    ->build();
57
58
            // Yahoo
59 2
            case self::YAHOO:
60 1
                return (new \Swap\Builder())
61 1
                    ->add('yahoo')
62 1
                    ->build();
63
64
            default:
65 1
                throw new UnsupportedExchangeServiceProvidedException(
66 1
                    "'$service' is not supported in MoneyMan." .
67 1
                    "Please use one of the following: " . implode(", ", self::$SERVICES)
68
                );
69
        }
70
    }
71
}