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    public function __call($method, $args = [])
59    {
60        try {
61            return parent::__call($method, $args);
62        } catch (\Exception $e) {
63            if ('loadFromPath' != $method && preg_match('/load(?P<accessor>[A-Za-z_0-9]+)/', $method, $matches)) {
64                $locale = $args[0] ?? 'de';
65                $instance = $this->loadAccessor($matches['accessor'], $locale);
66
67                return $instance;
68            }
69        }
70    }
71
72    protected function postConnect()
73    {
74    }
75
76    public function loadAccessor(string $name, string $locale = 'de')
77    {
78        if (isset($this->accessorClassName[$name])) {
79            if (null === $this->accessInstance[$locale][$this->accessorClassName[$name]]) {
80                $accessorClass = __NAMESPACE__ . '\\' . $this->engine . '\\' . $this->accessorClassName[$name];
81
82                $instance = new $accessorClass($this, $locale);
83                $this->accessInstance[$locale][$name] = $instance;
84                $this->accessInstance[$locale][$this->accessorNamesPlural[$name]] = $instance;
85            }
86            return $this->accessInstance[$locale][$name];
87        }
88        throw new \Exception('Invalid accessor');
89    }
90
91    abstract protected function connect(array $options);
92
93    public function getConnection()
94    {
95        return $this->pdo;
96    }
97
98    /**
99     * parameters see https://www.php.net/manual/de/pdo.query.php
100     */
101
102    public function query(...$args)
103    {
104        try {
105            return $this->pdo->query(...$args);
106        } catch (\Exception $e) {
107            throw $e;
108        }
109    }
110
111    /**
112     * parameters see https://www.php.net/manual/de/pdo.exec.php
113     */
114
115    public function exec(...$args)
116    {
117        try {
118            return $this->pdo->exec(...$args);
119        } catch (\Exception $e) {
120            throw $e;
121        }
122    }
123
124    /**
125     * parameters see https://www.php.net/manual/de/pdo.prepare.php
126     */
127
128    public function prepare(...$args)
129    {
130        try {
131            return $this->pdo->prepare(...$args);
132        } catch (\Exception $e) {
133            throw $e;
134        }
135    }
136
137    protected $transactionCount = 0;
138
139    public function beginTransaction()
140    {
141        try {
142            $this->transactionCount++;
143            if ($this->inTransaction()) {
144                return true;
145            }
146            return $this->pdo->beginTransaction();
147        } catch (\Exception $e) {
148            throw $e;
149        }
150    }
151
152    public function commit()
153    {
154        try {
155            $this->transactionCount--;
156            if ($this->transactionCount == 0 && $this->inTransaction()) {
157                return $this->pdo->commit();
158            } elseif (!$this->inTransaction()) {
159                trigger_error(__METHOD__ . ' no transaction started');
160            }
161            return true;
162        } catch (\Exception $e) {
163            throw $e;
164        }
165    }
166
167    public function rollBack()
168    {
169        try {
170            $this->transactionCount--;
171            if ($this->transactionCount == 0 && $this->inTransaction()) {
172                return $this->pdo->rollBack();
173            } elseif (!$this->inTransaction()) {
174                trigger_error(__METHOD__ . ' no transaction started');
175            }
176            return true;
177        } catch (\Exception $e) {
178            throw $e;
179        }
180    }
181
182    public function inTransaction()
183    {
184        try {
185            return $this->pdo->inTransaction();
186        } catch (\Exception $e) {
187            throw $e;
188        }
189    }
190}