Passed
Push — master ( 8bcaf5...500a63 )
by Glynn
03:09 queued 42s
created

Connection::configureWpdb()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 31
rs 8.8333
cc 7
nc 12
nop 1
1
<?php
2
3
namespace Pixie;
4
5
use wpdb;
6
use Exception;
7
use Viocon\Container;
8
use Pixie\AliasFacade;
9
use Pixie\EventHandler;
10
use Pixie\QueryBuilder\QueryBuilderHandler;
11
12
class Connection
13
{
14
    /** Config keys */
15
    public const CLONE_WPDB        = 'clone_wpdb';
16
    public const PREFIX            = 'prefix';
17
    public const SHOW_ERRORS       = 'show_errors';
18
    public const USE_WPDB_PREFIX   = 'use_wpdb_prefix';
19
20
    /**
21
     * @var Container
22
     */
23
    protected $container;
24
25
    /**
26
     * @var string
27
     */
28
    protected $adapter;
29
30
    /**
31
     * @var array<string, mixed>
32
     */
33
    protected $adapterConfig;
34
35
    /**
36
     * @var wpdb
37
     */
38
    protected $dbInstance;
39
40
    /**
41
     * @var Connection|null
42
     */
43
    protected static $storedConnection;
44
45
    /**
46
     * @var EventHandler
47
     */
48
    protected $eventHandler;
49
50
    /**
51
     * @param wpdb                 $wpdb
52
     * @param array<string, mixed>  $adapterConfig
53
     * @param string|null           $alias
54
     * @param Container|null        $container
55
     */
56
    public function __construct(
57
        wpdb $wpdb,
58
        array $adapterConfig = [],
59
        ?string $alias = null,
60
        ?Container $container = null
61
    ) {
62
        $this->setAdapterConfig($adapterConfig);
63
        $this->dbInstance = $this->configureWpdb($wpdb);
64
65
        $this->container    = $container ?? new Container();
66
        $this->eventHandler = $this->container->build(EventHandler::class);
67
68
        if ($alias) {
69
            $this->createAlias($alias);
70
        }
71
72
        // Preserve the first database connection with a static property
73
        if (!static::$storedConnection) {
74
            static::$storedConnection = $this;
75
        }
76
    }
77
78
    /**
79
     * Configures the instance of WPDB based on adaptor config values.
80
     *
81
     * @param \wpdb $wpdb
82
     * @return \wpdb
83
     */
84
    protected function configureWpdb(wpdb $wpdb): wpdb
85
    {
86
        // Maybe clone instance.
87
        if (
88
            array_key_exists(self::CLONE_WPDB, $this->adapterConfig)
89
            && true === $this->adapterConfig[self::CLONE_WPDB]
90
        ) {
91
            $wpdb = clone $wpdb;
92
        }
93
94
        // Maybe set the prefix to WPDB's.
95
        if (
96
            array_key_exists(self::USE_WPDB_PREFIX, $this->adapterConfig)
97
            && 0 < \mb_strlen($this->adapterConfig[self::USE_WPDB_PREFIX])
98
        ) {
99
            $this->adapterConfig[self::PREFIX] = $wpdb->prefix;
100
        }
101
102
        // Maybe configure errors
103
        if (array_key_exists(self::SHOW_ERRORS, $this->adapterConfig)) {
104
            // Based in its value.
105
            if (true === (bool) $this->adapterConfig[self::SHOW_ERRORS]) {
106
                $wpdb->show_errors(true);
107
                $wpdb->suppress_errors(false);
108
            } else {
109
                $wpdb->show_errors(false);
110
                $wpdb->suppress_errors(true);
111
            }
112
        }
113
114
        return $wpdb;
115
    }
116
117
    /**
118
     * Create an easily accessible query builder alias
119
     *
120
     * @param string $alias
121
     */
122
    public function createAlias(string $alias): void
123
    {
124
        class_alias(AliasFacade::class, $alias);
125
        $builder = $this->container->build(QueryBuilderHandler::class, [$this]);
126
        AliasFacade::setQueryBuilderInstance($builder);
127
    }
128
129
    /**
130
     * Returns an instance of Query Builder
131
     */
132
    public function getQueryBuilder(): QueryBuilderHandler
133
    {
134
        return $this->container->build(QueryBuilderHandler::class, [$this]);
135
    }
136
137
    /**
138
     * @param wpdb $wpdb
139
     *
140
     * @return $this
141
     */
142
    public function setDbInstance(wpdb $wpdb)
143
    {
144
        $this->dbInstance = $wpdb;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return wpdb
151
     */
152
    public function getDbInstance()
153
    {
154
        return $this->dbInstance;
155
    }
156
157
    /**
158
     * @param array<string, mixed> $adapterConfig
159
     *
160
     * @return $this
161
     */
162
    public function setAdapterConfig(array $adapterConfig)
163
    {
164
        $this->adapterConfig = $adapterConfig;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return array<string, mixed>
171
     */
172
    public function getAdapterConfig()
173
    {
174
        return $this->adapterConfig;
175
    }
176
177
    /**
178
     * @return Container
179
     */
180
    public function getContainer()
181
    {
182
        return $this->container;
183
    }
184
185
    /**
186
     * @return EventHandler
187
     */
188
    public function getEventHandler()
189
    {
190
        return $this->eventHandler;
191
    }
192
193
    /**
194
     * Returns the initial instance created.
195
     *
196
     * @return Connection
197
     *
198
     * @throws Exception If connection not already established
199
     */
200
    public static function getStoredConnection()
201
    {
202
        if (null === static::$storedConnection) {
203
            throw new Exception('No initial instance of Connection created');
204
        }
205
206
        return static::$storedConnection;
207
    }
208
}
209