Passed
Pull Request — master (#1928)
by Janko
12:21
created

SequenceReset::resetSequences()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 25
nc 2
nop 0
dl 0
loc 38
ccs 0
cts 19
cp 0
crap 6
rs 9.52
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Admin\Reset;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
9
10
final class SequenceReset implements SequenceResetInterface
11
{
12
    public function __construct(
13
        private EntityManagerInterface $entityManager,
14
    ) {}
15
16
    public function resetSequences(): int
17
    {
18
        $connection = $this->entityManager->getConnection();
19
20
        $result = $connection->executeQuery(
21
            "SELECT  'SELECT SETVAL(' ||quote_literal(S.relname)|| ',
22
                        (SELECT COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1)
23
                        FROM ' ||quote_ident(T.relname)  || '));'
24
            FROM pg_class S
25
            JOIN pg_depend D
26
                ON S.oid = D.objid
27
            JOIN pg_class T
28
                ON D.refobjid = T.oid
29
            JOIN pg_attribute C
30
                ON D.refobjid = C.attrelid
31
                AND D.refobjsubid = C.attnum
32
            WHERE S.relkind = 'S'
33
            ORDER BY S.relname"
34
        );
35
36
        $count = 0;
37
38
        while ($query = $result->fetchOne()) {
39
            $connection->executeQuery(
40
                $query
41
            );
42
43
            $count++;
44
        }
45
46
        $connection->executeQuery(
47
            sprintf(
48
                "SELECT SETVAL('stu_user_id_seq', %d)",
49
                UserEnum::USER_FIRST_ID
50
            )
51
        );
52
53
        return $count;
54
    }
55
}
56