Passed
Push — master ( 2e64cc...304ce4 )
by P.R.
03:51
created

MySqlWorker::writeTwoPhases()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 2
dl 0
loc 20
ccs 7
cts 11
cp 0.6364
crap 4.7691
rs 9.9
c 0
b 0
f 0
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\Exception\MySqlConnectFailedException;
9
use SetBased\Stratum\MySql\Exception\MySqlDataLayerException;
10
use SetBased\Stratum\MySql\MySqlDataLayer;
11
use SetBased\Stratum\MySql\MySqlDefaultConnector;
12
use SetBased\Stratum\MySql\MySqlMetaDataLayer;
13
14
/**
15
 * Base class for commands which needs a connection to a MySQL instance.
16
 */
17
class MySqlWorker
18
{
19
  //--------------------------------------------------------------------------------------------------------------------
20
  /**
21
   * The meta data layer.
22
   *
23
   * @var MySqlMetaDataLayer
24
   */
25
  protected $dl;
26
27
  /**
28
   * The output object.
29
   *
30
   * @var StratumStyle
31
   */
32
  protected $io;
33
34
  /**
35
   * The settings from the PhpStratum configuration file.
36
   *
37
   * @var Config
38
   */
39
  protected $settings;
40
41
  //--------------------------------------------------------------------------------------------------------------------
42
  /**
43
   * Object constructor.
44
   *
45
   * @param Config       $settings The settings from the PhpStratum configuration file.
46
   * @param StratumStyle $io       The output object.
47
   */
48 1
  public function __construct(Config $settings, StratumStyle $io)
49
  {
50 1
    $this->settings = $settings;
51 1
    $this->io       = $io;
52 1
  }
53
54
  //--------------------------------------------------------------------------------------------------------------------
55
  /**
56
   * Disconnects from MySQL instance.
57
   */
58 1
  public function disconnect()
59
  {
60 1
    if ($this->dl!==null)
61
    {
62 1
      $this->dl->disconnect();
63 1
      $this->dl = null;
64
    }
65 1
  }
66
67
  //--------------------------------------------------------------------------------------------------------------------
68
  /**
69
   * Connects to a MySQL instance.
70
   *
71
   * @throws MySqlConnectFailedException
72
   * @throws MySqlDataLayerException
73
   */
74 1
  protected function connect(): void
75
  {
76 1
    $host     = $this->settings->manString('database.host');
77 1
    $user     = $this->settings->manString('database.user');
78 1
    $password = $this->settings->manString('database.password');
79 1
    $database = $this->settings->manString('database.database');
80 1
    $port     = $this->settings->manInt('database.port', 3306);
81
82 1
    $connector = new MySqlDefaultConnector($host, $user, $password, $database, $port);
83 1
    $dataLayer = new MySqlDataLayer($connector);
84 1
    $dataLayer->connect();
85
86 1
    $this->dl = new MySqlMetaDataLayer($dataLayer, $this->io);
87 1
  }
88
89
  //--------------------------------------------------------------------------------------------------------------------
90
}
91
92
//----------------------------------------------------------------------------------------------------------------------
93