Passed
Push — master ( 3e0ae5...66dbef )
by Melech
01:47 queued 20s
created

PdoManager::query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\Manager\Abstract;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
17
use PDO;
18
use Valkyrja\Container\Manager\Contract\Container;
19
use Valkyrja\Orm\Entity\Contract\Entity;
20
use Valkyrja\Orm\Manager\Contract\Manager as Contract;
21
use Valkyrja\Orm\QueryBuilder\Factory\Contract\QueryBuilderFactory;
22
use Valkyrja\Orm\QueryBuilder\Factory\SqlQueryBuilderFactory;
23
use Valkyrja\Orm\Repository\Contract\Repository;
24
use Valkyrja\Orm\Repository\Repository as DefaultRepository;
25
use Valkyrja\Orm\Statement\Contract\Statement;
26
use Valkyrja\Orm\Statement\PdoStatement;
27
use Valkyrja\Orm\Throwable\Exception\RuntimeException;
28
29
use function is_bool;
30
use function is_string;
31
32
/**
33
 * Abtract Class PdoManager.
34
 *
35
 * @author Melech Mizrachi
36
 */
37
abstract class PdoManager implements Contract
38
{
39
    public function __construct(
40
        protected PDO $pdo,
41
        protected Container $container,
42
    ) {
43
    }
44
45
    /**
46
     * @inheritDoc
47
     *
48
     * @template T of Entity
49
     *
50
     * @param class-string<T> $entity The entity
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
51
     *
52
     * @return Repository<T>
53
     */
54
    #[Override]
55
    public function createRepository(string $entity): Repository
56
    {
57
        $repositoryClass = $entity::getRepository()
58
            ?? DefaultRepository::class;
59
60
        /** @var Repository<T> $repository */
61
        $repository = $this->container->get($repositoryClass, [$this, $entity]);
62
63
        return $repository;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    #[Override]
70
    public function createQueryBuilder(): QueryBuilderFactory
71
    {
72
        return new SqlQueryBuilderFactory();
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    #[Override]
79
    public function beginTransaction(): bool
80
    {
81
        return $this->pdo->beginTransaction();
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    #[Override]
88
    public function inTransaction(): bool
89
    {
90
        return $this->pdo->inTransaction();
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    #[Override]
97
    public function ensureTransaction(): void
98
    {
99
        if (! $this->inTransaction()) {
100
            $this->beginTransaction();
101
        }
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    #[Override]
108
    public function prepare(string $query): Statement
109
    {
110
        $statement = $this->pdo->prepare($query);
111
112
        if (is_bool($statement)) {
0 ignored issues
show
introduced by
The condition is_bool($statement) is always false.
Loading history...
113
            throw new RuntimeException('Statement preparation has failed');
114
        }
115
116
        return new PdoStatement(statement: $statement);
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    #[Override]
123
    public function query(string $query): Statement
124
    {
125
        $statement = $this->pdo->prepare($query);
126
127
        if (is_bool($statement)) {
0 ignored issues
show
introduced by
The condition is_bool($statement) is always false.
Loading history...
128
            throw new RuntimeException('Statement query has failed');
129
        }
130
131
        return new PdoStatement(statement: $statement);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    #[Override]
138
    public function commit(): bool
139
    {
140
        return $this->pdo->commit();
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    #[Override]
147
    public function rollback(): bool
148
    {
149
        return $this->pdo->rollBack();
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    #[Override]
156
    public function lastInsertId(string|null $table = null, string|null $idField = null): string
157
    {
158
        /** @var non-empty-string|false $lastInsertId */
159
        $lastInsertId = $this->pdo->lastInsertId();
160
161
        return is_string($lastInsertId)
0 ignored issues
show
introduced by
The condition is_string($lastInsertId) is always true.
Loading history...
162
            ? $lastInsertId
163
            : throw new RuntimeException('No last insert id found');
164
    }
165
}
166