MapWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 68
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocBlockReturnType() 0 3 1
A generateMethodBodyWithLobReturnData() 0 3 1
A generateMethodBodyWithoutLob() 0 19 1
A generateMethodBodyWithLobFetchData() 0 10 1
A getReturnTypeDeclaration() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Wrapper;
5
6
use SetBased\Stratum\Common\Wrapper\Helper\WrapperContext;
7
use SetBased\Stratum\MySql\Exception\MySqlQueryErrorException;
8
9
/**
10
 * Class for generating a wrapper method for a stored procedure that selects 0 or more rows with 2 columns. The rows are
11
 * returned as an array the first column are the keys and the second column are the values.
12
 */
13
class MapWrapper extends MysqlWrapper
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * @inheritdoc
18 1
   */
19
  protected function generateMethodBodyWithLobFetchData(WrapperContext $context): void
20 1
  {
21
    $context->codeStore->append('$result = $stmt->get_result();');
22
    $context->codeStore->append('$ret = [];');
23
    $context->codeStore->append('while (($row = $result->fetch_array(MYSQLI_NUM)))');
24
    $context->codeStore->append('{');
25
    $context->codeStore->append('$ret[$row[0]] = $row[1];');
26
    $context->codeStore->append('}');
27 1
    $context->codeStore->append('$result->free();');
28
    $context->codeStore->append('');
29 1
  }
30
31
  //--------------------------------------------------------------------------------------------------------------------
32
  /**
33
   * @inheritdoc
34
   */
35
  protected function generateMethodBodyWithLobReturnData(WrapperContext $context): void
36 1
  {
37
    $context->codeStore->append('return $ret;');
38 1
  }
39
40 1
  //--------------------------------------------------------------------------------------------------------------------
41
  /**
42 1
   * @inheritdoc
43 1
   */
44 1
  protected function generateMethodBodyWithoutLob(WrapperContext $context): void
45 1
  {
46 1
    $this->throws(MySqlQueryErrorException::class);
47 1
48 1
    $context->codeStore->append(sprintf("\$result = \$this->query('call %s(%s)');",
49
                                        $context->phpStratumMetadata['routine_name'],
50
                                        $this->getRoutineArgs($context)));
51
    $context->codeStore->append('$ret = [];');
52
    $context->codeStore->append('while (($row = $result->fetch_array(MYSQLI_NUM)))');
53
    $context->codeStore->append('{');
54
    $context->codeStore->append('$ret[$row[0]] = $row[1];');
55 1
    $context->codeStore->append('}');
56
    $context->codeStore->append('$result->free();');
57 1
    $context->codeStore->append('if ($this->mysqli->more_results())');
58 1
    $context->codeStore->append('{');
59 1
    $context->codeStore->append('$this->mysqli->next_result();');
60 1
    $context->codeStore->append('}');
61 1
    $context->codeStore->append('');
62
    $context->codeStore->append('return $ret;');
63
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * @inheritdoc
68 1
   */
69
  protected function getDocBlockReturnType(WrapperContext $context): string
70 1
  {
71
    return 'array';
72
  }
73
74
  //--------------------------------------------------------------------------------------------------------------------
75
  /**
76
   * @inheritdoc
77
   */
78
  protected function getReturnTypeDeclaration(WrapperContext $context): string
79
  {
80
    return ': array';
81
  }
82
83
  //--------------------------------------------------------------------------------------------------------------------
84
}
85
86
//----------------------------------------------------------------------------------------------------------------------
87