BaseProvider::setConfigName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sarahman\SmsService\Providers;
4
5
use Illuminate\Support\Facades\Config;
6
use Sarahman\SmsService\Interfaces\ProviderInterface;
7
8
abstract class BaseProvider implements ProviderInterface
9
{
10
    protected $configName;
11
    protected $config;
12
    protected $url;
13
    protected $recipientPattern = '/^(00|\+)?(8{2})?0?([0-9]{10})$/i';
14
15
    public function __construct(array $config = [], $url = null)
16
    {
17
        $this->loadConfigFromFile();
18
19
        if (!empty($config)) {
20
            foreach ($config as $key => $value) {
21
                $this->config[$key] = $value;
22
            }
23
        }
24
25
        $this->extractUrlFromConfigAndSet($url);
26
    }
27
28
    public function setConfigName($configName)
29
    {
30
        $this->configName = $configName;
31
32
        return $this->loadConfigFromFile();
33
    }
34
35
    public function setConfig(array $config)
36
    {
37
        $this->config = $config;
38
39
        return $this;
40
    }
41
42
    public function getConfig()
43
    {
44
        return $this->config;
45
    }
46
47
    public function setUrl($url)
48
    {
49
        $this->url = $url;
50
51
        return $this;
52
    }
53
54
    public function getUrl()
55
    {
56
        return $this->url;
57
    }
58
59
    private function loadConfigFromFile()
60
    {
61
        $this->configName = !is_null($this->configName) ? $this->configName : get_class($this);
62
        $this->config = Config::get('sms-service-with-bd-providers::config.providers.'.$this->configName);
63
64
        $this->extractUrlFromConfigAndSet();
65
66
        return $this;
67
    }
68
69
    private function extractUrlFromConfigAndSet($url = null)
70
    {
71
        if (isset($this->config['url'])) {
72
            $url = $this->config['url'];
73
            unset($this->config['url']);
74
        }
75
76
        is_null($url) || $this->setUrl($url);
77
    }
78
}
79