RowsWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 69
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getReturnTypeDeclaration() 0 3 1
A generateMethodBodyWithLobFetchData() 0 18 1
A generateMethodBodyWithLobReturnData() 0 8 1
A generateMethodBodyWithoutLob() 0 7 1
A getDocBlockReturnType() 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\MySqlDataLayerException;
8
use SetBased\Stratum\MySql\Exception\MySqlQueryErrorException;
9
10
/**
11
 * Class for generating a wrapper method for a stored procedure that selects 0, 1, or more rows.
12
 */
13
class RowsWrapper extends MysqlWrapper
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * @inheritdoc
18 1
   */
19
  protected function generateMethodBodyWithLobFetchData(WrapperContext $context): void
20 1
  {
21
    $this->throws(MySqlQueryErrorException::class);
22
23
    $context->codeStore->append('$row = [];');
24
    $context->codeStore->append('$this->bindAssoc($stmt, $row);');
25
    $context->codeStore->append('');
26
    $context->codeStore->append('$tmp = [];');
27 1
    $context->codeStore->append('while (($b = $stmt->fetch()))');
28
    $context->codeStore->append('{');
29 1
    $context->codeStore->append('$new = [];');
30
    $context->codeStore->append('foreach($row as $key => $value)');
31
    $context->codeStore->append('{');
32
    $context->codeStore->append('$new[$key] = $value;');
33
    $context->codeStore->append('}');
34
    $context->codeStore->append(' $tmp[] = $new;');
35
    $context->codeStore->append('}');
36 1
    $context->codeStore->append('');
37
  }
38 1
39
  //--------------------------------------------------------------------------------------------------------------------
40 1
  /**
41 1
   * @inheritdoc
42
   */
43
  protected function generateMethodBodyWithLobReturnData(WrapperContext $context): void
44
  {
45
    $this->throws(MySqlDataLayerException::class);
46
    $this->throws(MySqlQueryErrorException::class);
47
48 1
    $context->codeStore->append('if ($b===false) throw $this->dataLayerError(\'mysqli_stmt::fetch\');');
49
    $context->codeStore->append('');
50 1
    $context->codeStore->append('return $tmp;');
51
  }
52 1
53 1
  //--------------------------------------------------------------------------------------------------------------------
54 1
  /**
55 1
   * @inheritdoc
56 1
   */
57 1
  protected function generateMethodBodyWithoutLob(WrapperContext $context): void
58 1
  {
59 1
    $this->throws(MySqlQueryErrorException::class);
60 1
61 1
    $context->codeStore->append(sprintf("return \$this->executeRows('call %s(%s)');",
62 1
                                        $context->phpStratumMetadata['routine_name'],
63 1
                                        $this->getRoutineArgs($context)));
64 1
  }
65 1
66
  //--------------------------------------------------------------------------------------------------------------------
67
  /**
68
   * @inheritdoc
69
   */
70
  protected function getDocBlockReturnType(WrapperContext $context): string
71
  {
72 1
    return 'array[]';
73
  }
74 1
75 1
  //--------------------------------------------------------------------------------------------------------------------
76
  /**
77 1
   * @inheritdoc
78 1
   */
79 1
  protected function getReturnTypeDeclaration(WrapperContext $context): string
80
  {
81
    return ': array';
82
  }
83
84
  //--------------------------------------------------------------------------------------------------------------------
85
}
86
87
//----------------------------------------------------------------------------------------------------------------------
88