MySQL   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assigned() 0 6 3
A disconnecting() 0 3 1
A __construct() 0 3 1
A connecting() 0 23 2
1
<?php
2
/**
3
 * Clustered MySQL
4
 * User: moyo
5
 * Date: 02/11/2017
6
 * Time: 12:05 PM
7
 */
8
9
namespace Carno\Database\Clusters;
10
11
use Carno\Cluster\Classify\Scenes;
12
use Carno\Cluster\Contracts\Tags;
13
use Carno\Cluster\Managed;
14
use Carno\Cluster\Resources;
15
use Carno\Database\Chips\SQLDetector;
16
use Carno\Database\Chips\SQLExecutor;
17
use Carno\Database\Chips\TransactionGuard;
18
use Carno\Database\Connectors\MySQL as Connector;
19
use Carno\Database\Contracts\Executable;
20
use Carno\Database\Options\Timeouts;
21
use Carno\DSN\DSN;
22
use Carno\Net\Endpoint;
23
use Carno\Pool\Options;
24
use Carno\Pool\Pool;
25
use Carno\Promise\Promised;
26
27
abstract class MySQL extends Managed implements Executable
28
{
29
    use SQLDetector, SQLExecutor, TransactionGuard;
30
31
    /**
32
     * @var array
33
     */
34
    protected $tags = [Tags::MASTER, Tags::SLAVE];
35
36
    /**
37
     * @var string
38
     */
39
    protected $type = 'mysql';
40
41
    /**
42
     * @var int
43
     */
44
    protected $port = 3306;
45
46
    /**
47
     * @var int
48
     */
49
    protected $timeout = 8500;
50
51
    /**
52
     * MySQL constructor.
53
     * @param Resources $resources
54
     */
55
    public function __construct(Resources $resources)
56
    {
57
        $resources->initialize(Scenes::RESOURCE, $this->type, $this->server, $this);
58
    }
59
60
    /**
61
     * @param Endpoint $endpoint
62
     * @return Options
63
     */
64
    abstract protected function options(Endpoint $endpoint) : Options;
65
66
    /**
67
     * @param Endpoint $endpoint
68
     * @return Pool
69
     */
70
    protected function connecting(Endpoint $endpoint) : Pool
71
    {
72
        $vid = "{$this->type}:{$this->server}";
73
74
        $dsn = new DSN($endpoint->address()->host());
75
76
        $timeouts = new Timeouts(
77
            $dsn->option('connect', 1500),
0 ignored issues
show
Bug introduced by
It seems like $dsn->option('connect', 1500) can also be of type null; however, parameter $connect of Carno\Database\Options\Timeouts::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            /** @scrutinizer ignore-type */ $dsn->option('connect', 1500),
Loading history...
78
            $dsn->option('execute', $this->timeout)
0 ignored issues
show
Bug introduced by
It seems like $dsn->option('execute', $this->timeout) can also be of type null; however, parameter $execute of Carno\Database\Options\Timeouts::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            /** @scrutinizer ignore-type */ $dsn->option('execute', $this->timeout)
Loading history...
79
        );
80
81
        return new Pool($this->options($endpoint), static function () use ($timeouts, $dsn, $vid) {
82
            return new Connector(
83
                $dsn->host(),
84
                $dsn->port() ?: 3306,
85
                $dsn->user(),
86
                $dsn->pass(),
87
                $dsn->path(),
88
                $dsn->option('charset', 'utf8mb4'),
0 ignored issues
show
Bug introduced by
It seems like $dsn->option('charset', 'utf8mb4') can also be of type null; however, parameter $charset of Carno\Database\Connectors\MySQL::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                /** @scrutinizer ignore-type */ $dsn->option('charset', 'utf8mb4'),
Loading history...
89
                $timeouts,
90
                $vid
91
            );
92
        }, $vid);
93
    }
94
95
    /**
96
     * @param Pool $connected
97
     * @return Promised
98
     */
99
    protected function disconnecting($connected) : Promised
100
    {
101
        return $connected->shutdown();
102
    }
103
104
    /**
105
     * @param string $sql
106
     * @return Pool
107
     */
108
    protected function assigned(string $sql = '') : Pool
109
    {
110
        return $this->picking(
111
            $this->clustered() && $this->readonly($sql)
112
                ? Tags::SLAVE
113
                : Tags::MASTER
114
        );
115
    }
116
}
117