Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
17 / 17 |
|
71.43% |
10 / 14 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Folder | |
100.00% |
22 / 22 |
|
100.00% |
17 / 17 |
|
71.43% |
10 / 14 |
|
100.00% |
5 / 5 |
12.33 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __call | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scriptPath | |
100.00% |
6 / 6 |
|
100.00% |
9 / 9 |
|
50.00% |
4 / 8 |
|
100.00% |
1 / 1 |
6.00 | |||
| getScript | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Quma; |
| 6 | |
| 7 | use RuntimeException; |
| 8 | |
| 9 | /** |
| 10 | * @api |
| 11 | * @psalm-no-seal-properties |
| 12 | * @psalm-no-seal-methods |
| 13 | */ |
| 14 | class Folder |
| 15 | { |
| 16 | protected Database $db; |
| 17 | protected string $folder; |
| 18 | |
| 19 | public function __construct(Database $db, string $folder) |
| 20 | { |
| 21 | Util::assertPathSegment($folder, 'SQL folder name'); |
| 22 | |
| 23 | $this->db = $db; |
| 24 | $this->folder = $folder; |
| 25 | } |
| 26 | |
| 27 | public function __get(string $key): Script |
| 28 | { |
| 29 | return $this->getScript($key); |
| 30 | } |
| 31 | |
| 32 | /** @param array<array-key, mixed> $args */ |
| 33 | public function __call(string $key, array $args): Query |
| 34 | { |
| 35 | $script = $this->getScript($key); |
| 36 | |
| 37 | return $script->invoke(...$args); |
| 38 | } |
| 39 | |
| 40 | protected function scriptPath(string $key, bool $isTemplate): bool|string |
| 41 | { |
| 42 | $ext = $isTemplate ? '.tpql' : '.sql'; |
| 43 | |
| 44 | foreach ($this->db->getSqlDirs() as $path) { |
| 45 | $result = $path . DIRECTORY_SEPARATOR . $this->folder . DIRECTORY_SEPARATOR . $key . $ext; |
| 46 | |
| 47 | if (is_file($result)) { |
| 48 | return $result; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | protected function getScript(string $key): Script |
| 56 | { |
| 57 | Util::assertPathSegment($key, 'SQL script name'); |
| 58 | |
| 59 | $script = $this->scriptPath($key, false); |
| 60 | |
| 61 | if (is_string($script)) { |
| 62 | $loaded = $this->db->loadScript($script, false); |
| 63 | |
| 64 | return new Script($this->db, $loaded, false); |
| 65 | } |
| 66 | |
| 67 | $dynStmt = $this->scriptPath($key, true); |
| 68 | |
| 69 | if (is_string($dynStmt)) { |
| 70 | $loaded = $this->db->loadScript($dynStmt, true); |
| 71 | |
| 72 | return new Script($this->db, $loaded, true); |
| 73 | } |
| 74 | |
| 75 | throw new RuntimeException('SQL script does not exist'); |
| 76 | } |
| 77 | } |