Passed
Branch simplistic (3d6a47)
by Ali
02:39
created

SaudiAddress::getApiInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * Part of the Saudi Address API PHP package.
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * Licensed under the MIT.
9
 *
10
 * This source file is subject to the MIT License that is
11
 * bundled with this package in the LICENSE file.
12
 *
13
 * @package    Saudi Address
14
 * @version    2.0
15
 * @author     Ali Alharthi
16
 * @license    MIT
17
 * @copyright  (c) 2020, Ali Alharthi
18
 * @link       https://aalharthi.sa
19
 */
20
21
namespace AliAlharthi\SaudiAddress;
22
23
use ReflectionClass;
24
25
class SaudiAddress
26
{
27
    /**
28
     * The package version.
29
     *
30
     * @var  string
31
     */
32
    const VERSION = 2.0;
33
34
    /**
35
     * The Config repository instance.
36
     *
37
     * @var \AliAlharthi\SaudiAddress\ConfigInterface
38
     */
39
    protected $config;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param   string  $apiKey
45
     * @param   string  $apiSubscription
46
     */
47
    public function __construct($apiKey = null, $apiSubscription = 'Development', $locale = 'E', $cache = false)
48
    {
49
        $this->config = new Config($apiKey, $apiSubscription, $locale, $cache);
50
    }
51
52
    /**
53
     * Create a new Saudi National Address API instance.
54
     *
55
     * @param   string   $apiKey
56
     * @param   string   $apiSubscription
57
     * @return  SaudiAddress
58
     */
59
    public static function make($apiKey = null, $apiSubscription = 'Development', $locale = 'E', $cache = false)
60
    {
61
        return new static($apiKey, $apiSubscription, $locale, $cache);
62
    }
63
64
    /**
65
     * Returns the current package version.
66
     *
67
     * @return  double
68
     */
69
    public static function getVersion()
70
    {
71
        return self::VERSION;
72
    }
73
74
    /**
75
     * Returns the Config repository instance.
76
     *
77
     * @return  \AliAlharthi\SaudiAddress\ConfigInterface
78
     */
79
    public function getConfig()
80
    {
81
        return $this->config;
82
    }
83
84
    /**
85
     * Sets the Config repository instance.
86
     *
87
     * @param   \AliAlharthi\SaudiAddress\ConfigInterface   $config
88
     * @return  SaudiAddress
89
     */
90
    public function setConfig(ConfigInterface $config, ...$params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

90
    public function setConfig(ConfigInterface $config, /** @scrutinizer ignore-unused */ ...$params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92
        $this->config = $config;
93
94
        return $this;
95
    }
96
    /**
97
     * Returns the Saudi National Address API key.
98
     *
99
     * @return  string
100
     */
101
    public function getApiKey()
102
    {
103
        return $this->config->getApiKey();
104
    }
105
106
    /**
107
     * Sets the Saudi National Address API key.
108
     *
109
     * @param   string  $apiKey
110
     * @return  SaudiAddress
111
     */
112
    public function setApiKey($apiKey)
113
    {
114
        $this->config->setApiKey($apiKey);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Dynamically handle missing methods.
121
     *
122
     * @param   string  $method
123
     * @param   mixed   $params
124
     * @return  \AliAlharthi\SaudiAddress\Api\ApiInterface
125
     */
126
    public function __call($method, $params)
127
    {
128
        $class = "\\AliAlharthi\SaudiAddress\\Api\\" . ucwords($method);
129
130
        if (class_exists($class) && !(new ReflectionClass($class))->isAbstract()) {
131
            $obj = (new ReflectionClass($class))->newInstanceArgs(
132
                array_merge([$this->config], $params));
133
            // $obj->setConfig($this->config);
134
            return $obj;
135
        }
136
137
        throw new \BadMethodCallException("Undefined method [{$method}] called.");
138
139
    }
140
141
}
142