Completed
Push — master ( 1619f6...bbea89 )
by Carlos
40s
created

Application   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 66.04%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 16
c 5
b 1
f 1
lcom 1
cbo 9
dl 0
loc 163
ccs 35
cts 53
cp 0.6604
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A addProvider() 0 6 1
A setProviders() 0 8 2
A getProviders() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
A registerProviders() 0 6 2
A registerBase() 0 18 1
B initializeLogger() 0 16 5
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Application.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    overtrue <[email protected]>
21
 * @copyright 2015
22
 *
23
 * @link      https://github.com/overtrue/wechat
24
 * @link      http://overtrue.me
25
 */
26
namespace EasyWeChat\Foundation;
27
28
use Doctrine\Common\Cache\FilesystemCache;
29
use EasyWeChat\Core\AccessToken;
30
use EasyWeChat\Support\Log;
31
use Monolog\Handler\NullHandler;
32
use Monolog\Handler\StreamHandler;
33
use Monolog\Logger;
34
use Pimple\Container;
35
use Symfony\Component\HttpFoundation\Request;
36
37
/**
38
 * Class Application.
39
 *
40
 * @property \EasyWeChat\Server\Guard                    $server
41
 * @property \EasyWeChat\User\User                       $user
42
 * @property \EasyWeChat\User\Group                      $user_group
43
 * @property \EasyWeChat\Js\Js                           $js
44
 * @property \Overtrue\Socialite\SocialiteManager        $oauth
45
 * @property \EasyWeChat\Menu\Menu                       $menu
46
 * @property \EasyWeChat\Notice\Notice                   $notice
47
 * @property \EasyWeChat\Material\Material               $material
48
 * @property \EasyWeChat\Material\Temporary              $material_temporary
49
 * @property \EasyWeChat\Staff\Staff                     $staff
50
 * @property \EasyWeChat\Url\Url                         $url
51
 * @property \EasyWeChat\QRCode\QRCode                   $qrcode
52
 * @property \EasyWeChat\Semantic\Semantic               $semantic
53
 * @property \EasyWeChat\Stats\Stats                     $stats
54
 * @property \EasyWeChat\Payment\Merchant                $merchant
55
 * @property \EasyWeChat\Payment\Payment                 $payment
56
 * @property \EasyWeChat\Payment\LuckyMoney\LuckyMoney   $lucky_money
57
 * @property \EasyWeChat\Payment\MerchantPay\MerchantPay $merchant_pay
58
 * @property \EasyWeChat\Reply\Reply                     $reply
59
 * @property \EasyWeChat\Broadcast\Broadcast             $broadcast
60
 */
61
class Application extends Container
62
{
63
    /**
64
     * Service Providers.
65
     *
66
     * @var array
67
     */
68
    protected $providers = [
69
        ServiceProviders\ServerServiceProvider::class,
70
        ServiceProviders\UserServiceProvider::class,
71
        ServiceProviders\JsServiceProvider::class,
72
        ServiceProviders\OAuthServiceProvider::class,
73
        ServiceProviders\MenuServiceProvider::class,
74
        ServiceProviders\NoticeServiceProvider::class,
75
        ServiceProviders\MaterialServiceProvider::class,
76
        ServiceProviders\StaffServiceProvider::class,
77
        ServiceProviders\UrlServiceProvider::class,
78
        ServiceProviders\QRCodeServiceProvider::class,
79
        ServiceProviders\SemanticServiceProvider::class,
80
        ServiceProviders\StatsServiceProvider::class,
81
        ServiceProviders\PaymentServiceProvider::class,
82
        ServiceProviders\POIServiceProvider::class,
83
        ServiceProviders\ReplyServiceProvider::class,
84
        ServiceProviders\BroadcastServiceProvider::class,
85
    ];
86
87
    /**
88
     * Application constructor.
89
     *
90
     * @param array $config
91
     */
92 3
    public function __construct($config)
93
    {
94 3
        parent::__construct();
95
96
        $this['config'] = function () use ($config) {
97 3
            return new Config($config);
98
        };
99
100 3
        if ($this['config']['debug']) {
101
            error_reporting(E_ALL);
102
        }
103
104 3
        $this->registerProviders();
105 3
        $this->registerBase();
106 3
        $this->initializeLogger();
107
108 3
        Log::debug('Current configuration:', $config);
109 3
    }
110
111
    /**
112
     * Add a provider.
113
     *
114
     * @param string $provider
115
     *
116
     * @return Application
117
     */
118 1
    public function addProvider($provider)
119
    {
120 1
        array_push($this->providers, $provider);
121
122 1
        return $this;
123
    }
124
125
    /**
126
     * Set providers.
127
     *
128
     * @param array $providers
129
     */
130 1
    public function setProviders(array $providers)
131
    {
132 1
        $this->providers = [];
133
134 1
        foreach ($providers as $provider) {
135 1
            $this->addProvider($provider);
136 1
        }
137 1
    }
138
139
    /**
140
     * Return all providers.
141
     *
142
     * @return array
143
     */
144 2
    public function getProviders()
145
    {
146 2
        return $this->providers;
147
    }
148
149
    /**
150
     * Magic get access.
151
     *
152
     * @param string $id
153
     *
154
     * @return mixed
155
     */
156 1
    public function __get($id)
157
    {
158 1
        return $this->offsetGet($id);
159
    }
160
161
    /**
162
     * Magic set access.
163
     *
164
     * @param string $id
165
     * @param mixed  $value
166
     */
167 1
    public function __set($id, $value)
168
    {
169 1
        $this->offsetSet($id, $value);
170 1
    }
171
172
    /**
173
     * Register providers.
174
     */
175 3
    private function registerProviders()
176
    {
177 3
        foreach ($this->providers as $provider) {
178 3
            $this->register(new $provider());
179 3
        }
180 3
    }
181
182
    /**
183
     * Register basic providers.
184
     */
185 3
    private function registerBase()
186
    {
187
        $this['request'] = function () {
188
            return Request::createFromGlobals();
189
        };
190
191
        $this['cache'] = function () {
192
            return new FilesystemCache(sys_get_temp_dir());
193
        };
194
195
        $this['access_token'] = function () {
196
           return new AccessToken(
197
               $this['config']['app_id'],
198
               $this['config']['secret'],
199
               $this['cache']
200
           );
201
        };
202 3
    }
203
204
    /**
205
     * Initialize logger.
206
     */
207 3
    private function initializeLogger()
208
    {
209 3
        if (Log::hasLogger()) {
210 3
            return;
211
        }
212
        
213
        $logger = new Logger('easywechat');
214
215
        if (!$this['config']['debug'] || defined('PHPUNIT_RUNNING')) {
216
            $logger->pushHandler(new NullHandler());
217
        } elseif ($logFile = $this['config']['log.file']) {
218
            $logger->pushHandler(new StreamHandler($logFile, $this['config']->get('log.level', Logger::WARNING)));
219
        }
220
221
        Log::setLogger($logger);
222
    }
223
}
224