Lines 100.00% 38 / 38
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getType 100.00% 1 / 1 100.00% 1 / 1 1
 getDescription 100.00% 26 / 26 100.00% 1 / 1 9
 getQuery 100.00% 1 / 1 100.00% 1 / 1 1
 execute 100.00% 4 / 4 100.00% 1 / 1 2
 toArray 100.00% 5 / 5 100.00% 1 / 1 1
24readonly class SqlOperation implements OperationInterface
25{
26    public function __construct(
27        private Configuration $configuration,
28        private string $query,
29        private string $description = '',
30    ) {
31    }
32
33    public function getType(): string
34    {
35        return 'sql';
36    }
37
38    public function getDescription(): string
39    {
40        if ($this->description !== '') {
41            return $this->description;
42        }
43
44        // Generate description from query
45        $query = trim($this->query);
46        $matches = [];
47        if (stripos(haystack: $query, needle: 'CREATE TABLE') === 0) {
48            preg_match('/CREATE TABLE\s+(?:IF NOT EXISTS\s+)?(\S+)/i', $query, $matches);
49            return sprintf('Create table %s', $matches[1] ?? 'unknown');
50        }
51        if (stripos(haystack: $query, needle: 'ALTER TABLE') === 0) {
52            preg_match('/ALTER TABLE\s+(\S+)/i', $query, $matches);
53            return sprintf('Alter table %s', $matches[1] ?? 'unknown');
54        }
55        if (stripos(haystack: $query, needle: 'DROP TABLE') === 0) {
56            preg_match('/DROP TABLE\s+(?:IF EXISTS\s+)?(\S+)/i', $query, $matches);
57            return sprintf('Drop table %s', $matches[1] ?? 'unknown');
58        }
59        if (stripos(haystack: $query, needle: 'CREATE INDEX') === 0) {
60            preg_match('/CREATE INDEX\s+(?:IF NOT EXISTS\s+)?(\S+)/i', $query, $matches);
61            return sprintf('Create index %s', $matches[1] ?? 'unknown');
62        }
63        if (stripos(haystack: $query, needle: 'INSERT INTO') === 0) {
64            preg_match('/INSERT INTO\s+(\S+)/i', $query, $matches);
65            return sprintf('Insert into %s', $matches[1] ?? 'unknown');
66        }
67        if (stripos(haystack: $query, needle: 'UPDATE') === 0) {
68            preg_match('/UPDATE\s+(\S+)/i', $query, $matches);
69            return sprintf('Update %s', $matches[1] ?? 'unknown');
70        }
71        if (stripos(haystack: $query, needle: 'DELETE FROM') === 0) {
72            preg_match('/DELETE FROM\s+(\S+)/i', $query, $matches);
73            return sprintf('Delete from %s', $matches[1] ?? 'unknown');
74        }
75
76        return 'Execute SQL query';
77    }
78
79    public function getQuery(): string
80    {
81        return $this->query;
82    }
83
84    public function execute(): bool
85    {
86        try {
87            $result = $this->configuration->getDb()->query($this->query);
88            return (bool) $result;
89        } catch (\Throwable) {
90            return false;
91        }
92    }
93
94    public function toArray(): array
95    {
96        return [
97            'type' => $this->getType(),
98            'description' => $this->getDescription(),
99            'query' => $this->query,
100        ];
101    }
102}