TypeTraits::typeIsSupported()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Infinitypaul\NairaExchangeRates\Traits;
4
5
use Infinitypaul\NairaExchangeRates\Exceptions\Exceptions;
6
7
trait TypeTraits
8
{
9
    // Supported Types
10
    private $_types = [
11
        'cbn', 'bdc', 'bank', 'moneygram', 'westernunion',
12
    ];
13
14
    // The type (default is cbn):
15
    private $type;
16
17
    // Set the base type:
18
    public function setType(string $type)
19
    {
20
        // Sanitize the code:
21
        $type = $this->sanitizeTypes($type);
22
23
        $this->verifyType($type);
24
25
        $this->type = $type;
26
27
        // Return object to preserve method-chaining:
28
        return $this;
29
    }
30
31
    // Get the specified type:
32
    public function getBaseType()
33
    {
34
        return (is_null($this->type)) ? 'cbn' : $this->type;
35
    }
36
37
    // Sanitize types:
38
    private function sanitizeTypes(string $code)
39
    {
40
        return trim(
41
            strtolower($code)
42
        );
43
    }
44
45
    /**
46
     * Verify Types.
47
     * @param \Infinitypaul\NairaExchangeRates\Traits\string $code
48
     *
49
     * @throws \Infinitypaul\NairaExchangeRates\Exceptions\Exceptions
50
     */
51
    private function verifyType(string $code)
52
    {
53
        $type = $this->sanitizeTypes($code);
54
55
        // Is it a supported type?
56
        if (! $this->typeIsSupported($type)) {
57
            throw Exceptions::create('format.unsupported_type');
58
        }
59
    }
60
61
    // Check if a type is in the supported range:
62
    private function typeIsSupported(string $code)
63
    {
64
        $type = $this->sanitizeTypes($code);
65
66
        return in_array($type, $this->_types);
67
    }
68
}
69