Singleton0Wrapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 98
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateMethodBodyWithLobReturnData() 0 22 2
A generateMethodBodyWithLobFetchData() 0 16 1
A generateMethodBodyWithoutLob() 0 16 2
A getReturnTypeDeclaration() 0 9 2
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\Middle\Exception\ResultException;
8
use SetBased\Stratum\MySql\Exception\MySqlDataLayerException;
9
use SetBased\Stratum\MySql\Loader\Helper\MySqlDataTypeHelper;
10
11
/**
12
 * Class for generating a wrapper method for a stored procedure that selects 0 or 1 row having a single column only.
13
 */
14
class Singleton0Wrapper extends MysqlWrapper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * @inheritdoc
19 1
   */
20
  protected function generateMethodBodyWithLobFetchData(WrapperContext $context): void
21 1
  {
22
    $context->codeStore->append('$row = [];');
23
    $context->codeStore->append('$this->bindAssoc($stmt, $row);');
24
    $context->codeStore->append('');
25
    $context->codeStore->append('$tmp = [];');
26
    $context->codeStore->append('while (($b = $stmt->fetch()))');
27
    $context->codeStore->append('{');
28 1
    $context->codeStore->append('$new = [];');
29
    $context->codeStore->append('foreach($row as $value)');
30 1
    $context->codeStore->append('{');
31
    $context->codeStore->append('$new[] = $value;');
32 1
    $context->codeStore->append('}');
33
    $context->codeStore->append('$tmp[] = $new;');
34 1
    $context->codeStore->append('}');
35
    $context->codeStore->append('');
36
  }
37
38
  //--------------------------------------------------------------------------------------------------------------------
39
  /**
40
   * @inheritdoc
41 1
   */
42
  protected function generateMethodBodyWithLobReturnData(WrapperContext $context): void
43 1
  {
44 1
    $this->throws(MySqlDataLayerException::class);
45
    $this->throws(ResultException::class);
46 1
47
    $context->codeStore->append('if ($b===false)');
48 1
    $context->codeStore->append('{');
49
    $context->codeStore->append('throw $this->dataLayerError(\'mysqli_stmt::fetch\');');
50 1
    $context->codeStore->append('}');
51
    $context->codeStore->append('if (sizeof($tmp)>1)');
52
    $context->codeStore->append('{');
53
    $context->codeStore->append('throw new ResultException([0, 1], sizeof($tmp), $query);');
54 1
    $context->codeStore->append('}');
55
    $context->codeStore->append('');
56
57
    if ($context->phpStratumMetadata['designation']['return']===['bool'])
58
    {
59
      $context->codeStore->append('return !empty($tmp[0][0]);');
60
    }
61
    else
62 1
    {
63
      $context->codeStore->append('return $tmp[0][0] ?? null;');
64 1
    }
65 1
  }
66 1
67 1
  //--------------------------------------------------------------------------------------------------------------------
68 1
  /**
69 1
   * @inheritdoc
70 1
   */
71 1
  protected function generateMethodBodyWithoutLob(WrapperContext $context): void
72 1
  {
73 1
    $this->throws(MySqlDataLayerException::class);
74 1
    $this->throws(ResultException::class);
75 1
76 1
    if ($context->phpStratumMetadata['designation']['return']===['bool'])
77 1
    {
78
      $context->codeStore->append(sprintf("return !empty(\$this->executeSingleton0('call %s(%s)'));",
79
                                          $context->phpStratumMetadata['routine_name'],
80
                                          $this->getRoutineArgs($context)));
81
    }
82
    else
83
    {
84 1
      $context->codeStore->append(sprintf("return \$this->executeSingleton0('call %s(%s)');",
85
                                          $context->phpStratumMetadata['routine_name'],
86 1
                                          $this->getRoutineArgs($context)));
87 1
    }
88
  }
89 1
90 1
  //--------------------------------------------------------------------------------------------------------------------
91 1
  /**
92
   * @inheritdoc
93 1
   */
94
  protected function getDocBlockReturnType(WrapperContext $context): string
95 1
  {
96
    return implode('|', $context->phpStratumMetadata['php_doc']['return']);
97
  }
98
99 1
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * @inheritdoc
102
   */
103
  protected function getReturnTypeDeclaration(WrapperContext $context): string
104
  {
105
    $type = MySqlDataTypeHelper::phpTypeHintingToPhpTypeDeclaration($this->getDocBlockReturnType($context));
106
    if ($type==='')
107
    {
108
      return '';
109
    }
110
111
    return ': '.$type;
112
  }
113
114
  //--------------------------------------------------------------------------------------------------------------------
115
}
116
117
//----------------------------------------------------------------------------------------------------------------------
118