ChildishServer   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 184
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setupContainer() 0 7 2
A setAsGlobal() 0 4 1
A getContainer() 0 4 1
A setupDefaultConfiguration() 0 6 1
A setupManager() 0 5 1
A connection() 0 4 1
A table() 0 4 1
A getConnection() 0 4 1
A addConnection() 0 8 1
A setFetchMode() 0 6 1
A getDatabaseManager() 0 4 1
A bootEloquent() 0 4 1
A __callStatic() 0 4 1
1
<?php
2
namespace Childish;
3
4
use PDO;
5
use Childish\database\DatabaseManager;
6
use Childish\connection\ConnectionFactory;
7
use Childish\support\Container;
8
use Childish\support\Fluent;
9
10
/**
11
 * ChildishServer
12
 *
13
 * @author    Pu ShaoWei <[email protected]>
14
 * @date      2017/12/6
15
 * @package   App\db
16
 * @version   1.0
17
 */
18
class ChildishServer
19
{
20
    /**
21
     * @var \Childish\database\DatabaseManager $manager
22
     */
23
    protected $manager;
24
25
    /**
26
     * The current globally used instance.
27
     *
28
     * @var ChildishServer
29
     */
30
    protected static $instance;
31
32
    /**
33
     * @var \Childish\support\Container $container
34
     */
35
    protected $container;
36
37
    /**
38
     * ChildishServer constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->setupContainer(new Container);
43
        $this->setupDefaultConfiguration();
44
        $this->setupManager();
45
    }
46
47
    /**
48
     * setupContainer
49
     *
50
     * @param \Childish\support\Container $container
51
     */
52
    protected function setupContainer(Container $container)
53
    {
54
        $this->container = $container;
55
        if (!$this->container->bound('config')) {
56
            $this->container->instance('config', new Fluent);
57
        }
58
    }
59
60
    /**
61
     * Make this capsule instance available globally.
62
     *
63
     * @return void
64
     */
65
    public function setAsGlobal()
66
    {
67
        static::$instance = $this;
68
    }
69
70
    /**
71
     * Get the IoC container instance.
72
     *
73
     * @return \Childish\support\Container $container
74
     */
75
    public function getContainer()
76
    {
77
        return $this->container;
78
    }
79
80
    /**
81
     * Setup the default database configuration options.
82
     *
83
     * @return void
84
     */
85
    protected function setupDefaultConfiguration()
86
    {
87
        $this->container['config']['database.fetch'] = PDO::FETCH_OBJ;
88
89
        $this->container['config']['database.default'] = 'default';
90
    }
91
92
    /**
93
     * Build the database manager instance.
94
     *
95
     * @return void
96
     */
97
    protected function setupManager()
98
    {
99
        $factory       = new ConnectionFactory($this->container);
100
        $this->manager = new DatabaseManager($this->container, $factory);
101
    }
102
103
    /**
104
     * Get a connection instance from the global manager.
105
     *
106
     * @param  string $connection
107
     * @return \Childish\connection\Connection
108
     */
109
    public static function connection($connection = null)
110
    {
111
        return static::$instance->getConnection($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by parameter $connection on line 109 can also be of type string; however, Childish\ChildishServer::getConnection() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
112
    }
113
114
    /**
115
     * Get a fluent query builder instance.
116
     *
117
     * @param  string $table
118
     * @param  string $connection
119
     * @return  \Childish\query\Builder;
0 ignored issues
show
Documentation introduced by
The doc-type \Childish\query\Builder; could not be parsed: Expected "|" or "end of type", but got ";" at position 23. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
120
     */
121
    public static function table($table, $connection = null)
122
    {
123
        return static::$instance->connection($connection)->table($table);
124
    }
125
126
    /**
127
     * @param  string $name
128
     * @return \\Connection
129
     */
130
    /**
131
     * Get a registered connection instance.
132
     *
133
     * @param null $name
134
     * @return \Childish\connection\Connection
135
     */
136
    public function getConnection($name = null)
137
    {
138
        return $this->manager->connection($name);
139
    }
140
141
    /**
142
     * Register a connection with the manager.
143
     *
144
     * @param  array  $config
145
     * @param  string $name
146
     * @return void
147
     */
148
    public function addConnection(array $config, $name = 'default')
149
    {
150
        $connections = $this->container['config']['database.connections'];
151
152
        $connections[$name] = $config;
153
154
        $this->container['config']['database.connections'] = $connections;
155
    }
156
157
    /**
158
     * Set the fetch mode for the database connections.
159
     *
160
     * @param  int $fetchMode
161
     * @return $this
162
     */
163
    public function setFetchMode($fetchMode)
164
    {
165
        $this->container['config']['database.fetch'] = $fetchMode;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Get the database manager instance.
172
     *
173
     * @return mixed
174
     */
175
    public function getDatabaseManager()
176
    {
177
        return $this->manager;
178
    }
179
180
    /**
181
     * bootEloquent
182
     *
183
     * @return void
184
     */
185
    public function bootEloquent()
186
    {
187
        ChildishModel::setConnectionResolver($this->manager);
188
    }
189
190
    /**
191
     * Dynamically pass methods to the default connection.
192
     *
193
     * @param  string $method
194
     * @param  array  $parameters
195
     * @return mixed
196
     */
197
    public static function __callStatic($method, $parameters)
198
    {
199
        return static::connection()->$method(...$parameters);
200
    }
201
}