Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
PDOAccess
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 13
1190
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 __call
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 postConnect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 loadAccessor
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 connect
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 getConnection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 query
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 exec
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 prepare
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 beginTransaction
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 commit
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 rollBack
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 inTransaction
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BO\Zmsdldb;
4
5abstract class PDOAccess extends AbstractAccess
6{
7    /**
8     * @SuppressWarnings(PHPMD.LongVariable)
9     */
10    protected $accessorClassName = [];
11
12    protected $accessorNamesPlural = [
13        'Authority' => 'Authorities',
14        'Borough' => 'Boroughs',
15        'Link' => 'Links',
16        'Location' => 'Locations',
17        'Office' => 'Offices',
18        'Service' => 'Services',
19        'Setting' => 'Settings',
20        'Topic' => 'Topics'
21    ];
22
23    protected $pdo;
24
25    protected $engine = 'SQLite';
26
27
28    public function __construct(array $options)
29    {
30        try {
31            $parts = explode("\\", static::class);
32            $this->engine = str_replace('Access', '', end($parts));
33
34            $accessorNameKeys = array_keys($this->accessorNamesPlural);
35            $accessorClassName = array_flip($this->accessorNamesPlural);
36
37            $this->accessorClassName = array_merge(
38                $accessorClassName,
39                array_combine($accessorNameKeys, $accessorNameKeys)
40            );
41            $this->accessorNamesPlural = array_merge(
42                $this->accessorNamesPlural,
43                array_flip($this->accessorNamesPlural)
44            );
45
46            if (isset($options['pdoConnection']) && $options['pdoConnection'] instanceof \PDO) {
47                $this->pdo = $options['pdoConnection'];
48            } else {
49                $this->connect($options);
50            }
51            $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
52            $this->postConnect();
53        } catch (\Exception $e) {
54            throw $e;
55        }
56    }
57
58    #[\Override]
59    public function __call($method, $args = [])
60    {
61        try {
62            return parent::__call($method, $args);
63        } catch (\Exception $e) {
64            if ('loadFromPath' != $method && preg_match('/load(?P<accessor>[A-Za-z_0-9]+)/', $method, $matches)) {
65                $locale = $args[0] ?? 'de';
66                $instance = $this->loadAccessor($matches['accessor'], $locale);
67
68                return $instance;
69            }
70        }
71    }
72
73    protected function postConnect()
74    {
75    }
76
77    public function loadAccessor(string $name, string $locale = 'de')
78    {
79        if (isset($this->accessorClassName[$name])) {
80            if (null === $this->accessInstance[$locale][$this->accessorClassName[$name]]) {
81                $accessorClass = __NAMESPACE__ . '\\' . $this->engine . '\\' . $this->accessorClassName[$name];
82
83                $instance = new $accessorClass($this, $locale);
84                $this->accessInstance[$locale][$name] = $instance;
85                $this->accessInstance[$locale][$this->accessorNamesPlural[$name]] = $instance;
86            }
87            return $this->accessInstance[$locale][$name];
88        }
89        throw new \Exception('Invalid accessor');
90    }
91
92    abstract protected function connect(array $options);
93
94    public function getConnection()
95    {
96        return $this->pdo;
97    }
98
99    /**
100     * parameters see https://www.php.net/manual/de/pdo.query.php
101     */
102
103    public function query(...$args)
104    {
105        try {
106            return $this->pdo->query(...$args);
107        } catch (\Exception $e) {
108            throw $e;
109        }
110    }
111
112    /**
113     * parameters see https://www.php.net/manual/de/pdo.exec.php
114     */
115
116    public function exec(...$args)
117    {
118        try {
119            return $this->pdo->exec(...$args);
120        } catch (\Exception $e) {
121            throw $e;
122        }
123    }
124
125    /**
126     * parameters see https://www.php.net/manual/de/pdo.prepare.php
127     */
128
129    public function prepare(...$args)
130    {
131        try {
132            return $this->pdo->prepare(...$args);
133        } catch (\Exception $e) {
134            throw $e;
135        }
136    }
137
138    protected $transactionCount = 0;
139
140    public function beginTransaction()
141    {
142        try {
143            $this->transactionCount++;
144            if ($this->inTransaction()) {
145                return true;
146            }
147            return $this->pdo->beginTransaction();
148        } catch (\Exception $e) {
149            throw $e;
150        }
151    }
152
153    public function commit()
154    {
155        try {
156            $this->transactionCount--;
157            if ($this->transactionCount == 0 && $this->inTransaction()) {
158                return $this->pdo->commit();
159            } elseif (!$this->inTransaction()) {
160                trigger_error(__METHOD__ . ' no transaction started');
161            }
162            return true;
163        } catch (\Exception $e) {
164            throw $e;
165        }
166    }
167
168    public function rollBack()
169    {
170        try {
171            $this->transactionCount--;
172            if ($this->transactionCount == 0 && $this->inTransaction()) {
173                return $this->pdo->rollBack();
174            } elseif (!$this->inTransaction()) {
175                trigger_error(__METHOD__ . ' no transaction started');
176            }
177            return true;
178        } catch (\Exception $e) {
179            throw $e;
180        }
181    }
182
183    public function inTransaction()
184    {
185        try {
186            return $this->pdo->inTransaction();
187        } catch (\Exception $e) {
188            throw $e;
189        }
190    }
191}