MySqlDataLayerException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Exception;
5
6
use SetBased\Stratum\Middle\Exception\DataLayerException;
7
8
/**
9
 * Exception for situations where the execution of s SQL query has failed.
10
 */
11
class MySqlDataLayerException extends \RuntimeException implements DataLayerException
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * The error code value of the error ($mysqli->errno).
16
   *
17
   * @var int
18
   */
19
  protected int $errno;
20
21
  /**
22
   * Description of the last error ($mysqli->error).
23
   *
24
   * @var string
25
   */
26
  protected string $error;
27
28
  /**
29
   * The method.
30
   *
31
   * @var string
32
   */
33
  protected string $method;
34
35
  //--------------------------------------------------------------------------------------------------------------------
36
  /**
37
   * Object constructor.
38
   *
39
   * @param int    $errno  The error code value of the error ($mysqli->errno).
40
   * @param string $error  Description of the last error ($mysqli->error).
41
   * @param string $method The name of the executed method.
42
   */
43 8
  public function __construct(int $errno, string $error, string $method)
44
  {
45 8
    $this->errno  = $errno;
46 8
    $this->error  = $error;
47 8
    $this->method = $method;
48
49 8
    parent::__construct($this->implodeMessage($this->composerMessage()));
50
  }
51
52
  //--------------------------------------------------------------------------------------------------------------------
53
  /**
54
   * Returns the error code of the error.
55
   */
56
  public function getErrno(): int
57
  {
58
    return $this->errno;
59
  }
60
  //--------------------------------------------------------------------------------------------------------------------
61
  /**
62
   * Returns the description of the error.
63
   */
64
  public function getError(): string
65
  {
66
    return $this->error;
67
  }
68
69
  //--------------------------------------------------------------------------------------------------------------------
70
  /**
71
   * @inheritdoc
72
   */
73
  public function getName(): string
74
  {
75
    return 'MySQL Error';
76
  }
77
78
  //--------------------------------------------------------------------------------------------------------------------
79
  /**
80
   * Composes the message of this exception as array of lines.
81
   */
82
  protected function composerMessage(): array
83
  {
84
    return array_merge($this->splitIntoTwoColumns('MySQL Errno', (string)$this->errno),
85
                       $this->splitIntoTwoColumns('Error', $this->error),
86
                       $this->splitIntoTwoColumns('Method', $this->method));
87
  }
88 4
89
  //--------------------------------------------------------------------------------------------------------------------
90 4
  /**
91 4
   * Splits a string (may contain multiple lines) into columns: The first columns will contain a header, the second
92 4
   * column the lines of the string.
93
   *
94
   * @param string $header The header.
95
   * @param string $string The string possible with multiple lines.
96
   */
97
  protected function splitIntoTwoColumns(string $header, string $string): array
98
  {
99
    $lines = [];
100
101
    $parts = explode(PHP_EOL, trim($string));
102
    foreach ($parts as $i => $part)
103
    {
104
      $lines[] = [($i===0 ? $header : ''), $part];
105 8
    }
106
107 8
    return $lines;
108
  }
109 8
110 8
  //--------------------------------------------------------------------------------------------------------------------
111
  /**
112 8
   * Implodes an array with lines of an error message to a string. Each line of the message consists out of two
113
   * columns.
114
   *
115 8
   * @param array $lines The lines of the error message.
116
   */
117
  private function implodeMessage(array $lines): string
118
  {
119
    $max = 0;
120
    foreach ($lines as $line)
121
    {
122
      $max = max($max, mb_strlen($line[0]));
123
    }
124
125
    $format = sprintf('%%-%ds: %%s', $max);
126
    $tmp    = [''];
127 8
    foreach ($lines as $line)
128
    {
129 8
      $tmp[] = sprintf($format, $line[0], $line[1]);
130 8
    }
131
132 8
    return implode(PHP_EOL, $tmp);
133
  }
134
135 8
  //--------------------------------------------------------------------------------------------------------------------
136 8
}
137 8
138
//----------------------------------------------------------------------------------------------------------------------
139