Completed
Push — master ( 12a313...e137a7 )
by ARCANEDEV
7s
created

CurrencyLayerService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 90
ccs 18
cts 21
cp 0.8571
rs 10
wmc 7
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheKey() 0 4 1
A setConfigs() 0 4 1
A getAccessKey() 0 8 2
A request() 0 17 3
1
<?php namespace Arcanedev\Currencies\Services;
2
3
use Arcanedev\Currencies\Exceptions\ApiException;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     CurrencyLayerService
8
 *
9
 * @package  Arcanedev\Currencies\Services
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class CurrencyLayerService extends AbstractApiService
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The base URL.
20
     *
21
     * @var string
22
     */
23
    protected $baseUrl = 'http://apilayer.net/api';
24
25
    /**
26
     * The Access Key.
27
     *
28
     * @var string
29
     */
30
    protected $accessKey;
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Getters & Setters
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Set the configs.
38
     *
39
     * @param  array  $configs
40
     */
41 48
    protected function setConfigs(array $configs)
42
    {
43 48
        $this->accessKey = Arr::get($configs, 'access-key');
44 48
    }
45
46
    /**
47
     * Get the Access Key.
48
     *
49
     * @return string
50
     *
51
     * @throws \Arcanedev\Currencies\Exceptions\ApiException
52
     */
53 24
    private function getAccessKey()
54
    {
55 24
        if ( ! $this->accessKey) {
56
            throw new ApiException('Currencylayer.com requires an access key.');
57
        }
58
59 24
        return $this->accessKey;
60
    }
61
62
    /**
63
     * Get cache key.
64
     *
65
     * @return string
66
     */
67
    protected function getCacheKey()
68
    {
69
        return parent::getCacheKey() . '.currencylayer';
70
    }
71
72
    /* ------------------------------------------------------------------------------------------------
73
     |  Main Functions
74
     | ------------------------------------------------------------------------------------------------
75
     */
76
    /**
77
     * Make an API request.
78
     *
79
     * @param  string  $from
80
     * @param  array   $to
81
     *
82
     * @return \Arcanedev\Currencies\Entities\RateCollection
83
     */
84 24
    protected function request($from, array $to = [])
85
    {
86 24
        $response = $this->client->get('/live', [
87 24
            'access_key' => $this->getAccessKey(),
88 24
            'source'     => $from,
89 24
            'currencies' => empty($to) ? null : implode(',', $to),
90 18
        ]);
91
92 24
        $rates = [];
93 24
        $data  = json_decode($response, true);
94
95 24
        foreach ($data['quotes'] as $key => $ratio) {
96 24
            $rates[substr($key, 3)] = $ratio;
97 18
        }
98
99 24
        return $rates;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $rates; (array) is incompatible with the return type declared by the abstract method Arcanedev\Currencies\Ser...ractApiService::request of type Arcanedev\Currencies\Entities\RateCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
100
    }
101
}
102