TdbmDbProvider::getClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace TheCodingMachine\TDBM\MetaHydrator;
3
4
5
use MetaHydrator\Database\DBProvider;
6
use MetaHydrator\Exception\DBException;
7
use TheCodingMachine\TDBM\NoBeanFoundException;
8
use TheCodingMachine\TDBM\TDBMService;
9
10
class TdbmDbProvider implements DBProvider
11
{
12
    /**
13
     * @var TDBMService
14
     */
15
    private $tdbmService;
16
17
    public function __construct(TDBMService $tdbmService)
18
    {
19
        $this->tdbmService = $tdbmService;
20
    }
21
22
    /**
23
     * This method should:
24
     * - return null if NO value corresponding to primary keys is passed
25
     * - throw a DBException if missing some primary keys, or object not found
26
     * - return the found bean otherwise
27
     *
28
     * @param string $table
29
     * @param array $params
30
     * @return mixed|null
31
     *
32
     * @throws DBException
33
     */
34
    public function getObject(string $table, array $params)
35
    {
36
        $primaryKeys = $this->tdbmService->_getPrimaryKeysFromObjectData($table, $params);
37
        if (empty($primaryKeys)) {
38
            return null;
39
        }
40
41
        try {
42
            return $this->tdbmService->findObjectByPk($table, $params);
43
        } catch (NoBeanFoundException $e) {
44
            throw new DBException($e->getMessage(), 0, $e);
45
        }
46
    }
47
48
    /**
49
     * This method should return the bean class name corresponding to given table
50
     *
51
     * @param string $table
52
     * @return string
53
     *
54
     * @throws DBException
55
     */
56
    public function getClassName(string $table)
57
    {
58
        return $this->tdbmService->getBeanClassName($table);
59
    }
60
}
61