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 (#190)
by joseph
21:51
created

UuidFunctionPolyfill::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
 * NOTE - the native mysql 8 functions take a second parameter of true to create ordered UUIDs. The polyfill will
16
 * always create ordered UUIDs
17
 *
18
 */
19
class UuidFunctionPolyfill
20
{
21
    public const UUID_TO_BIN = 'UUID_TO_BIN';
22
23
    public const BIN_TO_UUID = 'BIN_TO_UUID';
24
25
    /**
26
     * @var \Doctrine\DBAL\Connection
27
     */
28
    private $conn;
29
    /**
30
     * @var string
31
     */
32
    private $dbName;
33
34
    public function __construct(EntityManagerInterface $entityManager)
35
    {
36
        $this->conn   = $entityManager->getConnection();
37
        $this->dbName = $entityManager->getConnection()->getParams()['dbname'];
38
    }
39
40
    public function run(): void
41
    {
42
        if (\ts\stringStartsWith($this->getVersion(), '8')) {
43
            return;
44
        }
45
        $this->checkProcedureExists(self::UUID_TO_BIN) ?: $this->createProcedureUuidToBin();
46
        $this->checkProcedureExists(self::BIN_TO_UUID) ?: $this->createProcedureBinToUuid();
47
    }
48
49
    public function getVersion(): string
50
    {
51
        $stmt = $this->conn->prepare("select version()");
52
        $stmt->execute();
53
54
        return (string)$stmt->fetchColumn();
55
    }
56
57
    public function checkProcedureExists(string $name): bool
58
    {
59
        $stmt = $this->conn->prepare("
60
        SELECT 1 
61
        FROM mysql.proc 
62
        WHERE type='FUNCTION' 
63
        AND db=? 
64
        AND specific_name=?
65
        ");
66
        $stmt->execute([$this->dbName, $name]);
67
        $result = $stmt->fetchColumn();
68
69
        return $result === '1';
70
    }
71
72
    public function createProcedureUuidToBin(): void
73
    {
74
        $this->conn->query('
75
CREATE FUNCTION ' . self::UUID_TO_BIN . '(_uuid BINARY(36), _ordered BOOL)
76
	RETURNS BINARY(16)
77
	LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
78
	BEGIN            
79
		IF _ordered
80
		THEN
81
			RETURN UNHEX(CONCAT(
82
		        SUBSTR(_uuid, 15, 4),
83
		        SUBSTR(_uuid, 10, 4),
84
		        SUBSTR(_uuid,  1, 8),
85
		        SUBSTR(_uuid, 20, 4),
86
		        SUBSTR(_uuid, 25)));
87
		ELSE
88
			RETURN UNHEX(CONCAT(
89
		    	LEFT(_uuid, 8), 
90
		    	MID(_uuid, 10, 4), 
91
		    	MID(_uuid, 15, 4), 
92
		    	MID(_uuid, 20, 4), 
93
		    	RIGHT(_uuid, 12)));         
94
		END IF;
95
	END;
96
        ');
97
    }
98
99
    public function createProcedureBinToUuid(): void
100
    {
101
        $this->conn->query("
102
CREATE FUNCTION ' . self::BIN_TO_UUID . '(_bin BINARY(16), _ordered BOOL)
103
	RETURNS BINARY(36)
104
        LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
105
    BEGIN
106
		DECLARE HEX CHAR(32);
107
		SET HEX = HEX(_bin);
108
		IF _ordered
109
		THEN
110
		    RETURN
111
		        LCASE(CONCAT_WS('-',
112
	                HEX(SUBSTR(_bin,  5, 4)),
113
	                HEX(SUBSTR(_bin,  3, 2)),
114
	                HEX(SUBSTR(_bin,  1, 2)),
115
	                HEX(SUBSTR(_bin,  9, 2)),
116
	                HEX(SUBSTR(_bin, 11))
117
		        ));
118
	   	ELSE       	
119
			RETURN 
120
				LOWER(CONCAT_WS('-',
121
					LEFT(HEX, 8), 
122
					MID(HEX, 9,4),
123
					MID(HEX, 13,4),
124
					MID(HEX, 17,4),
125
					RIGHT(HEX, 12)
126
				));
127
		   
128
	   	END IF;
129
	END;
130
        ");
131
    }
132
}
133