Completed
Push — master ( 5652bd...9532f5 )
by Pierre
02:48
created

Core::fromOrm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Component\Db;
4
5
use App\Component\Model\Orm\Orm;
6
use App\Component\Db\Factory;
7
8
class Core
9
{
10
    /**
11
     * connection
12
     *
13
     * @var \PDO
14
     */
15
    protected $connection;
16
17
    /**
18
     * logger
19
     *
20
     * @var \Monolog\Logger
21
     */
22
    protected $logger;
23
24
    /**
25
     * sql
26
     *
27
     * @var string
28
     */
29
    protected $sql;
30
31
    /**
32
     * $statement
33
     * @var \PDOStatement
34
     */
35
    protected $statement;
36
37
    /**
38
     * fetch mode
39
     *
40
     * @var int
41
     */
42
    protected $fetchMode = \PDO::FETCH_ASSOC;
43
44
    /**
45
     * database name
46
     *
47
     * @var string
48
     */
49
    protected $database;
50
51
    /**
52
     * rowset result
53
     *
54
     * @var array
55
     */
56
    protected $rowset;
57
58
    /**
59
     * set connection from Orm instance
60
     *
61
     * @param Orm $ormInstance
62
     * @return Core
63
     */
64
    public function fromOrm(Orm &$ormInstance): Core
65
    {
66
        $this->database = $ormInstance->getDatabase();
67
        $slot = $ormInstance->getSlot();
68
        $container = $ormInstance->getContainer();
69
        $this->logger = $container->getService(\Monolog\Logger::class);
70
        $factory = new Factory($container);
71
        $this->connection = $factory->getConnection($slot, $this->database);
72
        $this->rowset = [];
73
        return $this;
74
    }
75
76
    /**
77
     * run
78
     *
79
     * @param string $sql
80
     * @param array $bindParams
81
     * @return boolean
82
     */
83
    public function run($sql, $bindParams = [], $bindTypes = []):Core
84
    {
85
        $this->sql = $sql;
86
        try {
87
            $this->statement = $this->connection->prepare($sql);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->connection->prepare($sql) can also be of type boolean. However, the property $statement is declared as type PDOStatement. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88
            $this->statement->setFetchMode($this->fetchMode);
89
            if ($bindParams) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bindParams of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
90
                $this->bindArray($this->statement, $bindParams, $bindTypes);
0 ignored issues
show
Bug introduced by
It seems like $this->statement can also be of type boolean; however, parameter $poStatement of App\Component\Db\Core::bindArray() does only seem to accept PDOStatement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
                $this->bindArray(/** @scrutinizer ignore-type */ $this->statement, $bindParams, $bindTypes);
Loading history...
91
            }
92
        } catch (\PDOException $exc) {
93
            $this->logger->alert('Prepare failed');
94
            $this->logger->alert($exc->getMessage());
95
        }
96
        try {
97
            $this->statement->execute();
98
        } catch (\PDOException $exc) {
99
            $this->logger->alert('Execute failed');
100
            $this->logger->alert($exc->getMessage());
101
        }
102
        return $this;
103
    }
104
105
    /**
106
     * hydrate
107
     *
108
     * @return Core
109
     */
110
    public function hydrate(): Core
111
    {
112
        $this->rowset = $this->statement->fetchAll($this->fetchMode);
113
        $this->statement->closeCursor();
114
        return $this;
115
    }
116
117
    /**
118
     * return run result
119
     *
120
     * @return array
121
     */
122
    public function getRowset():array
123
    {
124
        return $this->rowset;
125
    }
126
127
    /**
128
     * bindArray
129
     *
130
     * @param \PDOStatement $poStatement
131
     * @param array $paArray
132
     * @param array $forcedTypes
133
     * @return $this
134
     */
135
    public function bindArray(\PDOStatement &$poStatement, array &$paArray, array $forcedTypes = [])
136
    {
137
        foreach ($paArray as $k => $v) {
138
            $type = (is_int($v)) ? \PDO::PARAM_INT : \PDO::PARAM_STR;
139
140
            if (isset($forcedTypes[$k])) {
141
                $type = $forcedTypes[$k];
142
                $v = ($type == \PDO::PARAM_INT) ? (int) $v : $v;
143
            }
144
            $value = is_array($v) ? serialize($v) : $v;
145
            $key =  $k;
146
            try {
147
                $poStatement->bindValue($key, $value, $type);
148
            } catch (\PDOException $exc) {
149
                $this->logger->alert(
150
                    'Sql Bind Error [' . $key . ':' . $value . ':' . $type . ']'
151
                );
152
            }
153
        }
154
        return $this;
155
    }
156
}
157