Test Failed
Push — master ( 90f1e5...8cd561 )
by Jim
02:25
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 139
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A basePath() 0 3 2
A __construct() 0 11 1
A registerProviders() 0 4 2
A registerBaseBindings() 0 7 1
A setBasePath() 0 6 1
A logConfiguration() 0 9 3
A getConfig() 0 11 1
A version() 0 3 1
A getProviders() 0 7 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/12/2018
6
 * Time: 11:58 PM
7
 */
8
9
namespace TimSDK\Foundation;
10
11
use TimSDK\Core\API;
12
use TimSDK\Support\Log;
13
use TimSDK\Container\ServiceContainer;
14
use TimSDK\Container\ApplicationInterface as ContractContainer;
15
16
/**
17
 * Class Application
18
 * @package TimSDK\Foundation
19
 * @property \TimSDK\Foundation\Config $config
20
 * @property \GuzzleHttp\Client $httpClient
21
 */
22
class Application extends ServiceContainer implements ContractContainer
23
{
24
    /**
25
     * version.
26
     *
27
     * @var string
28
     */
29
    const VERSION = '0.0.1';
30
31
    protected $basePath;
32
33
    /**
34
     * @var array
35
     */
36
    protected $defaultConfig = [];
37
38
    /**
39
     * @var array
40
     */
41
    protected $userConfig = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $providers = [
47
        //
48
    ];
49
50
    public function __construct(array $config = [], array $prepends = [])
51
    {
52
        $this->userConfig = $config;
53
54
        $this->registerBaseBindings();
55
56
        $this->registerProviders();
57
58
        $this->logConfiguration();
59
60
        parent::__construct($prepends);
61
    }
62
63
    /**
64
     * @param mixed $basePath
65
     */
66
    public function setBasePath($basePath)
67
    {
68
        $this->basePath = rtrim($basePath, '\/');
69
70
        $this->instance('path.base', $this->basePath());
71
        $this->instance('path.cert', $this->basePath('Cert'));
72
    }
73
74
    /**
75
     * Get the base path
76
     *
77
     * @param  string  $path Optionally, a path to append to the base path
78
     * @return string
79
     */
80
    public function basePath($path = '')
81
    {
82
        return $this->basePath.($path ? DIRECTORY_SEPARATOR.$path : $path);
83
    }
84
85
    /**
86
     * Get the version number of the application.
87
     *
88
     * @return string
89
     */
90
    public function version()
91
    {
92
        return static::VERSION;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getConfig()
99
    {
100
        $base = [
101
            // http://docs.guzzlephp.org/en/stable/request-options.html
102
            'http' => [
103
                'timeout' => 5.0,
104
                'base_uri' => API::BASE_URL,
105
            ],
106
        ];
107
108
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
109
    }
110
111
    /**
112
     * Register all service
113
     */
114
    protected function registerProviders()
115
    {
116
        foreach ($this->getProviders() as $provider) {
117
            $this->register(new $provider());
118
        }
119
    }
120
121
    /**
122
     * Get all providers
123
     *
124
     * @return array
125
     */
126
    public function getProviders()
127
    {
128
        return array_merge([
129
            \TimSDK\Foundation\ServiceProviders\LogServiceProvider::class,
130
            \TimSDK\Foundation\ServiceProviders\ConfigServiceProvider::class,
131
            \TimSDK\Foundation\ServiceProviders\HttpClientServiceProvider::class,
132
        ], $this->providers);
133
    }
134
135
    /**
136
     * Register the basic bindings into the container.
137
     *
138
     * @return void
139
     */
140
    public function registerBaseBindings()
141
    {
142
        self::setInstance($this);
143
144
        $this->instance(ServiceContainer::class, $this);
145
146
        $this->instance('app', $this);
147
    }
148
149
    /**
150
     * Log Configuration
151
     */
152
    protected function logConfiguration()
153
    {
154
        $config = new Config($this->getConfig());
155
156
        foreach (['sdkappid', 'account_type'] as $item) {
157
            $config->has($item) && $config->set($item, substr($config->get($item), 0, 5) . '...');
158
        }
159
160
        Log::debug('Current config:', $config->toArray());
161
    }
162
}
163