GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#185)
by joseph
33:47
created

UuidFunctionPolyfill::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
crap 1.008
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Schema;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
/**
8
 * @see http://mysql.rjweb.org/doc.php/uuid
9
 * @see https://mysqlserverteam.com/mysql-8-0-uuid-support/
10
 *
11
 * This class handles ensuring that we have functions for converting to and from binary UUID formats in MySQL
12
 *
13
 * In MySQL8, these functions come built in, for below this version though we need to make our own
14
 */
15
class UuidFunctionPolyfill
16
{
17
    public const UUID_TO_BIN = 'UUID_TO_BIN';
18
19
    public const BIN_TO_UUID = 'BIN_TO_UUID';
20
21
    /**
22
     * @var \Doctrine\DBAL\Connection
23
     */
24
    private $conn;
25
    /**
26
     * @var string
27
     */
28
    private $dbName;
29
30 1
    public function __construct(EntityManagerInterface $entityManager)
31
    {
32 1
        $this->conn   = $entityManager->getConnection();
33 1
        $this->dbName = $entityManager->getConnection()->getParams()['dbname'];
34 1
    }
35
36 1
    public function run(): void
37
    {
38 1
        if (\ts\stringStartsWith($this->getVersion(), '8')) {
39
            return;
40
        }
41 1
        $this->checkProcedureExists(self::UUID_TO_BIN) ?: $this->createProcedureUuidToBin();
42 1
        $this->checkProcedureExists(self::BIN_TO_UUID) ?: $this->createProcedureBinToUuid();
43 1
    }
44
45 1
    public function getVersion(): string
46
    {
47 1
        $stmt = $this->conn->prepare("select version()");
48 1
        $stmt->execute();
49
50 1
        return (string)$stmt->fetchColumn();
51
    }
52
53 1
    public function checkProcedureExists(string $name): bool
54
    {
55 1
        $stmt = $this->conn->prepare("
56
        SELECT 1 
57
        FROM mysql.proc 
58
        WHERE type='FUNCTION' 
59
        AND db=? 
60
        AND specific_name=?
61
        ");
62 1
        $stmt->execute([$this->dbName, $name]);
63 1
        $result = $stmt->fetchColumn();
64
65 1
        return $result === '1';
66
    }
67
68 1
    public function createProcedureUuidToBin(): void
69
    {
70 1
        $this->conn->query('
71 1
            CREATE FUNCTION ' . self::UUID_TO_BIN . '(_uuid BINARY(36))
72
                RETURNS BINARY(16)
73
                LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
74
            RETURN
75
                UNHEX(CONCAT(
76
                    SUBSTR(_uuid, 15, 4),
77
                    SUBSTR(_uuid, 10, 4),
78
                    SUBSTR(_uuid,  1, 8),
79
                    SUBSTR(_uuid, 20, 4),
80
                    SUBSTR(_uuid, 25) ));
81
        ');
82 1
    }
83
84 1
    public function createProcedureBinToUuid(): void
85
    {
86 1
        $this->conn->query('
87 1
        CREATE FUNCTION ' . self::BIN_TO_UUID . "(_bin BINARY(16))
88
            RETURNS BINARY(36)
89
            LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
90
        RETURN
91
            LCASE(CONCAT_WS('-',
92
                HEX(SUBSTR(_bin,  5, 4)),
93
                HEX(SUBSTR(_bin,  3, 2)),
94
                HEX(SUBSTR(_bin,  1, 2)),
95
                HEX(SUBSTR(_bin,  9, 2)),
96
                HEX(SUBSTR(_bin, 11))
97
                     ));
98
        ");
99 1
    }
100
}
101