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