Passed
Pull Request — dev (#98)
by Wilmer
09:27 queued 02:32
created

CommandPDOMysql::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.0294

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
crap 9.0294
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Cache\QueryCache;
9
use Yiisoft\Db\Command\Command;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\ConvertException;
12
use Yiisoft\Db\Exception\Exception;
13
use Yiisoft\Db\Exception\InvalidConfigException;
14
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
final class CommandPDOMysql extends Command
17
{
18 342
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
19
    {
20 342
        parent::__construct($queryCache);
21
    }
22
23 171
    public function queryBuilder(): QueryBuilderInterface
24
    {
25 171
        return $this->db->getQueryBuilder();
26
    }
27
28
    /**
29
     * @throws Exception|InvalidConfigException|PDOException
30
     */
31 162
    public function prepare(?bool $forRead = null): void
32
    {
33 162
        if (isset($this->pdoStatement)) {
34 5
            $this->bindPendingParams();
35
36 5
            return;
37
        }
38
39 162
        $sql = $this->getSql() ?? '';
40
41 162
        if ($this->db->getTransaction()) {
42
            /** master is in a transaction. use the same connection. */
43 5
            $forRead = false;
44
        }
45
46 162
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
47 156
            $pdo = $this->db->getSlavePdo();
48
        } else {
49 59
            $pdo = $this->db->getMasterPdo();
50
        }
51
52
        try {
53 162
            $this->pdoStatement = $pdo?->prepare($sql);
54 162
            $this->bindPendingParams();
55
        } catch (PDOException $e) {
56
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
57
            /** @var array|null */
58
            $errorInfo = $e->errorInfo ?? null;
59
60
            throw new Exception($message, $errorInfo, $e);
61
        }
62
    }
63
64 2
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
65
    {
66
        return [
67
            __CLASS__,
68
            $method,
69
            $fetchMode,
70 2
            $this->db->getDriver()->getDsn(),
71 2
            $this->db->getDriver()->getUsername(),
72
            $rawSql,
73
        ];
74
    }
75
76 161
    protected function internalExecute(?string $rawSql): void
77
    {
78 161
        $attempt = 0;
79
80 161
        while (true) {
81
            try {
82
                if (
83 161
                    ++$attempt === 1
84 161
                    && $this->isolationLevel !== null
85 161
                    && $this->db->getTransaction() === null
86
                ) {
87
                    $this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
88
                } else {
89 161
                    $this->pdoStatement?->execute();
90
                }
91 158
                break;
92 23
            } catch (PDOException $e) {
93 23
                $rawSql = $rawSql ?: $this->getRawSql();
94 23
                $e = (new ConvertException($e, $rawSql))->run();
95
96 23
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
97 23
                    throw $e;
98
                }
99
            }
100
        }
101
    }
102
}
103