Completed
Push — 2.0 ( ada449...a2425b )
by Vermeulen
01:55
created

AbstractModeles   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 134
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getTableName() 0 4 1
A getTableNameWithPrefix() 0 4 1
A getBaseKeyName() 0 4 1
A obtainApp() 0 4 1
B obtainSqlConnect() 0 37 7
1
<?php
2
3
namespace BfwSql;
4
5
use \Exception;
6
7
/**
8
 * Abstract class for all Models class
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 */
14
abstract class AbstractModeles extends \BfwSql\Sql
15
{
16
    /**
17
     * @const ERR_NO_CONNECTION_CONFIGURED Exception code if no connection
18
     * is configured.
19
     */
20
    const ERR_NO_CONNECTION_CONFIGURED = 2101001;
21
    
22
    /**
23
     * @const ERR_NEED_BASEKEYNAME_DEFINED Exception code if the baseKeyName
24
     * should be defined (multiple connection) but it's not.
25
     */
26
    const ERR_NEED_BASEKEYNAME_DEFINED = 2101002;
27
    
28
    /**
29
     * @const ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME Exception code if the
30
     * baseKeyName defined corresponding to no defined connection.
31
     */
32
    const ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME = 2101003;
33
    
34
    /**
35
     * @var $tableName The table name
36
     */
37
    protected $tableName = '';
38
    
39
    /**
40
     * @var $tableNameWithPrefix the table name with prefix
41
     */
42
    protected $tableNameWithPrefix = '';
43
    
44
    /**
45
     * @var $baseKeyName The baseKeyName to use to connection.
46
     *  Use it if they are multiple database to connect in the application.
47
     */
48
    protected $baseKeyName = '';
49
    
50
    /**
51
     * Constructor
52
     */
53
    public function __construct()
54
    {
55
        $sqlConnect = $this->obtainSqlConnect();
56
        
57
        parent::__construct($sqlConnect);
58
        $this->tableNameWithPrefix = $this->prefix.$this->tableName;
59
    }
60
    
61
    /**
62
     * Getter to property tableName
63
     * 
64
     * @return string
65
     */
66
    public function getTableName()
67
    {
68
        return $this->tableName;
69
    }
70
    
71
    /**
72
     * Getter to property tableNameWithPrefix
73
     * 
74
     * @return string
75
     */
76
    public function getTableNameWithPrefix()
77
    {
78
        return $this->tableNameWithPrefix;
79
    }
80
    
81
    /**
82
     * Getter to property baseKeyName
83
     * 
84
     * @return string
85
     */
86
    public function getBaseKeyName()
87
    {
88
        return $this->baseKeyName;
89
    }
90
    
91
    /**
92
     * Get the BFW Application
93
     * It's a dedicated method for unit test or case where App is override
94
     * 
95
     * @return \BFW\Application
96
     */
97
    protected function obtainApp()
98
    {
99
        return \BFW\Application::getInstance();
100
    }
101
    
102
    /**
103
     * Obtain SqlConnect instance for the baseKeyName
104
     * 
105
     * @return \BfwSql\SqlConnect
106
     * 
107
     * @throws \Exception If there are many connection declared and if the
108
     *  property baseKeyName is empty
109
     */
110
    protected function obtainSqlConnect()
111
    {
112
        $listBases = $this->obtainApp()
113
            ->getModuleForName('bfw-sql')
114
            ->listBases;
115
        
116
        if (count($listBases) === 0) {
117
            throw new Exception(
118
                'There is no connection configured.',
119
                self::ERR_NO_CONNECTION_CONFIGURED
120
            );
121
        }
122
        
123
        if (count($listBases) > 1 && empty($this->baseKeyName)) {
124
            throw new Exception(
125
                'There are multiple connection, '
126
                .'so the property baseKeyName must be defined',
127
                self::ERR_NEED_BASEKEYNAME_DEFINED
128
            );
129
        }
130
        
131
        if (count($listBases) > 1 && !isset($listBases[$this->baseKeyName])) {
132
            throw new Exception(
133
                'There are multiple connection, '
134
                .'but the connection '.$this->baseKeyName.' is not defined.',
135
                self::ERR_UNKNOWN_CONNECTION_FOR_BASEKEYNAME
136
            );
137
        }
138
        
139
        if (count($listBases) === 1) {
140
            $sqlConnect = reset($listBases);
141
        } else {
142
            $sqlConnect = $listBases[$this->baseKeyName];
143
        }
144
        
145
        return $sqlConnect;
146
    }
147
}
148