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.
Passed
Pull Request — master (#154)
by joseph
27:09
created

UuidFunctionPolyfill   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 62
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createProcedureBinToUuid() 0 4 1
A run() 0 4 3
A checkProcedureExists() 0 13 1
A createProcedureUuidToBin() 0 4 1
A __construct() 0 4 1
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 3
    public function __construct(EntityManagerInterface $entityManager)
31
    {
32 3
        $this->conn   = $entityManager->getConnection();
33 3
        $this->dbName = $entityManager->getConnection()->getParams()['dbname'];
34 3
    }
35
36 3
    public function run(): void
37
    {
38 3
        $this->checkProcedureExists(self::UUID_TO_BIN) ?: $this->createProcedureUuidToBin();
39 3
        $this->checkProcedureExists(self::BIN_TO_UUID) ?: $this->createProcedureBinToUuid();
40 3
    }
41
42 3
    public function checkProcedureExists(string $name): bool
43
    {
44 3
        $stmt = $this->conn->prepare("
45
        SELECT 1 
46
        FROM mysql.proc 
47
        WHERE type='FUNCTION' 
48
        AND db=? 
49
        AND specific_name=?
50
        ");
51 3
        $stmt->execute([$this->dbName, $name]);
52 3
        $result = $stmt->fetchColumn();
53
54 3
        return $result === '1';
55
    }
56
57 3
    public function createProcedureUuidToBin(): void
58
    {
59 3
        $this->conn->query('
60 3
            CREATE FUNCTION ' . self::UUID_TO_BIN . '(_uuid BINARY(36))
61
                RETURNS BINARY(16)
62
                LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
63
            RETURN
64
                UNHEX(CONCAT(
65
                    SUBSTR(_uuid, 15, 4),
66
                    SUBSTR(_uuid, 10, 4),
67
                    SUBSTR(_uuid,  1, 8),
68
                    SUBSTR(_uuid, 20, 4),
69
                    SUBSTR(_uuid, 25) ));
70
        ');
71 3
    }
72
73 3
    public function createProcedureBinToUuid(): void
74
    {
75 3
        $this->conn->query('
76 3
        CREATE FUNCTION ' . self::BIN_TO_UUID . "(_bin BINARY(16))
77
            RETURNS BINARY(36)
78
            LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
79
        RETURN
80
            LCASE(CONCAT_WS('-',
81
                HEX(SUBSTR(_bin,  5, 4)),
82
                HEX(SUBSTR(_bin,  3, 2)),
83
                HEX(SUBSTR(_bin,  1, 2)),
84
                HEX(SUBSTR(_bin,  9, 2)),
85
                HEX(SUBSTR(_bin, 11))
86
                     ));
87
        ");
88 3
    }
89
}
90