ServiceFactory::getService()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
crap 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
}