MySqlWorker   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 67
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 13 1
A __construct() 0 4 1
A disconnect() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Backend;
5
6
use SetBased\Stratum\Backend\Config;
7
use SetBased\Stratum\Backend\StratumStyle;
8
use SetBased\Stratum\MySql\MySqlDataLayer;
9
use SetBased\Stratum\MySql\MySqlDefaultConnector;
10
use SetBased\Stratum\MySql\MySqlMetadataLayer;
11
12
/**
13
 * Base class for commands which needs a connection to a MySQL or MariaDB instance.
14
 */
15
class MySqlWorker
16
{
17
  //--------------------------------------------------------------------------------------------------------------------
18
  /**
19
   * The metadata layer.
20
   *
21
   * @var MySqlMetadataLayer|null
22
   */
23
  protected ?MySqlMetadataLayer $dl;
24
25
  /**
26
   * The output object.
27
   *
28
   * @var StratumStyle
29
   */
30
  protected StratumStyle $io;
31
32
  /**
33
   * The settings from the PhpStratum configuration file.
34
   *
35
   * @var Config
36
   */
37
  protected Config $settings;
38
39
  //--------------------------------------------------------------------------------------------------------------------
40
  /**
41
   * Object constructor.
42
   *
43
   * @param Config       $settings The settings from the PhpStratum configuration file.
44
   * @param StratumStyle $io       The output object.
45
   */
46
  public function __construct(Config $settings, StratumStyle $io)
47
  {
48 1
    $this->settings = $settings;
49
    $this->io       = $io;
50 1
  }
51 1
52
  //--------------------------------------------------------------------------------------------------------------------
53
  /**
54
   * Disconnects from MySQL instance.
55
   */
56
  public function disconnect(): void
57
  {
58 1
    if ($this->dl!==null)
59
    {
60 1
      $this->dl->disconnect();
61
      $this->dl = null;
62 1
    }
63 1
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * Connects to a MySQL instance.
68
   */
69
  protected function connect(): void
70
  {
71
    $host     = $this->settings->manString('database.host');
72
    $user     = $this->settings->manString('database.user');
73
    $password = $this->settings->manString('database.password');
74 1
    $database = $this->settings->manString('database.database');
75
    $port     = $this->settings->manInt('database.port', 3306);
76 1
77 1
    $connector = new MySqlDefaultConnector($host, $user, $password, $database, $port);
78 1
    $dataLayer = new MySqlDataLayer($connector);
79 1
    $dataLayer->connect();
80 1
81
    $this->dl = new MySqlMetadataLayer($dataLayer, $this->io);
82 1
  }
83 1
84 1
  //--------------------------------------------------------------------------------------------------------------------
85
}
86 1
87
//----------------------------------------------------------------------------------------------------------------------
88