Passed
Push — master ( e0c5e8...7926db )
by P.R.
04:00
created

FunctionWrapper::writeResultHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Wrapper;
5
6
use SetBased\Stratum\Middle\Exception\ResultException;
7
use SetBased\Stratum\MySql\Exception\MySqlDataLayerException;
8
use SetBased\Stratum\MySql\Helper\DataTypeHelper;
9
10
/**
11
 * Class for generating a wrapper method for a stored function.
12
 */
13
class FunctionWrapper extends Wrapper
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * @inheritdoc
18
   */
19 1
  protected function getDocBlockReturnType(): string
20
  {
21 1
    return $this->routine['return'];
22
  }
23
24
  //--------------------------------------------------------------------------------------------------------------------
25
  /**
26
   * @inheritdoc
27
   */
28 1
  protected function getReturnTypeDeclaration(): string
29
  {
30 1
    $type = DataTypeHelper::phpTypeHintingToPhpTypeDeclaration($this->getDocBlockReturnType());
31
32 1
    if ($type==='') return '';
33
34 1
    return ': '.$type;
35
  }
36
37
  //--------------------------------------------------------------------------------------------------------------------
38
  /**
39
   * @inheritdoc
40
   */
41 1
  protected function writeResultHandler(): void
42
  {
43 1
    $this->throws(MySqlDataLayerException::class);
44 1
    $this->throws(ResultException::class);
45
46 1
    if ($this->routine['return']=='bool')
47
    {
48 1
      $this->codeStore->append(sprintf("return !empty(\$this->executeSingleton0('select %s(%s)'));",
49 1
                                       $this->routine['routine_name'],
50 1
                                       $this->getRoutineArgs()));
51
    }
52
    else
53
    {
54 1
      $this->codeStore->append(sprintf("return \$this->executeSingleton0('select %s(%s)');",
55 1
                                       $this->routine['routine_name'],
56 1
                                       $this->getRoutineArgs()));
57
    }
58 1
  }
59
60
  //--------------------------------------------------------------------------------------------------------------------
61
  /**
62
   * @inheritdoc
63
   */
64
  protected function writeRoutineFunctionLobFetchData(): void
65
  {
66
    $this->codeStore->append('$ret = $this->mysqli->affected_rows;');
67
    $this->codeStore->append('');
68
  }
69
70
  //--------------------------------------------------------------------------------------------------------------------
71
  /**
72
   * @inheritdoc
73
   */
74
  protected function writeRoutineFunctionLobReturnData(): void
75
  {
76
    if ($this->routine['return']=='bool')
77
    {
78
      $this->codeStore->append('return !empty($ret);');
79
    }
80
    else
81
    {
82
      $this->codeStore->append('return $ret;');
83
    }
84
  }
85
86
  //--------------------------------------------------------------------------------------------------------------------
87
}
88
89
//----------------------------------------------------------------------------------------------------------------------
90