]> git.mxchange.org Git - city.git/blob - application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php
83de450e13ca1342d65b677999223d8c77ab2977
[city.git] / application / city / classes / database / frontend / region / class_RegionInformationDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Database\Frontend\RegionInformation;
4
5 // Import application-specific stuff
6 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
7
8 // Import framework stuff
9 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
10 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Registry\Registerable;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17 use \LogicException;
18 use \BadMethodCallException;
19
20 /**
21  * A database frontend for region informations
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2015 - 2023 City Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class RegionInformationDatabaseFrontend extends BaseDatabaseFrontend implements RegionInformationFrontend, Registerable {
43         // Constants for database table names
44         const DB_TABLE_REGION_INFORMATION = 'region_data';
45
46         // Constants for database column names
47         const DB_COLUMN_REGION_ID          = 'region_id';
48         const DB_COLUMN_REGION_NAME        = 'region_name';
49         const DB_COLUMN_REGION_USER_ID     = 'region_user_id';
50
51         /**
52          * Protected constructor
53          *
54          * @return      void
55          */
56         protected function __construct () {
57                 // Call parent constructor
58                 parent::__construct(__CLASS__);
59         }
60
61         /**
62          * Creates an instance of this database frontend by a provided user class
63          *
64          * @return      $frontendInstance       An instance of the created frontend class
65          */
66         public static final function createRegionInformationDatabaseFrontend () {
67                 // Get a new instance
68                 $frontendInstance = new RegionInformationDatabaseFrontend();
69
70                 // Set (primary!) table name
71                 $frontendInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
72
73                 // Return the instance
74                 return $frontendInstance;
75         }
76
77         /**
78          * Removes non-public data from given array.
79          *
80          * @param       $data   An array with possible non-public data that needs to be removed.
81          * @return      $data   A cleaned up array with only public data.
82          */
83         public function removeNonPublicDataFromArray(array $data) {
84                 // Currently call only inner method
85                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
86                 $data = parent::removeNonPublicDataFromArray($data);
87
88                 // Return cleaned data
89                 return $data;
90         }
91
92         /**
93          * Checks whether the user has already founded a region
94          *
95          * @return      $hasFounded             Whether the user has already founded a region
96          * @throws      LogicException  If no manager instance was created
97          */
98         public function ifUserHasCreatedRegion () {
99                 // Get user instance
100                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: CALLED!');
101                 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
102
103                 // Now get a search criteria instance
104                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
105
106                 // Search for user's cities
107                 $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
108
109                 // Get a result back
110                 $resultInstance = $this->doSelectByCriteria($searchInstance);
111
112                 // Get region manager instance
113                 $managerInstance = ManagerFactory::createManagerByType('region');
114
115                 // Make sure the instance is valid
116                 if (!($managerInstance instanceof ManageableRegion)) {
117                         // Not a valid instance
118                         throw new LogicException('No manager instance created for regions', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
119                 }
120
121                 // Set result instance
122                 $managerInstance->setResultInstance($resultInstance);
123
124                 // Has it been founded?
125                 $hasFounded = $resultInstance->valid();
126
127                 // Return result
128                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: hasFounded=%d - EXIT!', intval($hasFounded)));
129                 return $hasFounded;
130         }
131
132         /**
133          * Checks whether the given region name is taken
134          *
135          * @param       $regionName             Name of region
136          * @return      $isTaken                Whether the given region name is taken
137          * @throws      InvalidArgumentException        If a parameter has an invalid value
138          * @throws      LogicException  If no manager instance was created
139          */
140         public function ifRegionExists (string $regionName) {
141                 // Pre-check name
142                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName));
143                 if (empty($regionName)) {
144                         // Throw IAE
145                         throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT);
146                 }
147
148                 // Now get a search criteria instance
149                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
150
151                 // Search for the region number one which is hard-coded the default
152                 $searchInstance->addCriteria(RegionInformationDatabaseFrontend::DB_COLUMN_REGION_NAME, $regionName);
153                 $searchInstance->setLimit(1);
154
155                 // Get a result back
156                 $resultInstance = $this->doSelectByCriteria($searchInstance);
157
158                 // Check it
159                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: resultInstance[]=%s', gettype($resultInstance)));
160                 $isTaken = $resultInstance->next();
161
162                 // Get manger instance
163                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d', intval($isTaken)));
164                 $managerInstance = ManagerFactory::createManagerByType('region');
165
166                 // Make sure the instance is valid
167                 if (!($managerInstance instanceof ManageableRegion)) {
168                         // Not a valid instance
169                         throw new LogicException(sprintf('No manager instance created for regions, regionName=%s', $regionName), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
170                 }
171
172                 // Set result instance
173                 $managerInstance->setResultInstance($resultInstance);
174
175                 // Return result
176                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: isTaken=%d - EXIT!', intval($isTaken)));
177                 return $isTaken;
178         }
179
180         /**
181          * Creates a region by given name
182          *
183          * @param       $regionName             Name of region
184          * @return      void
185          * @throws      InvalidArgumentException        If a parameter has an invalid value
186          * @throws      BadMethodCallException  If this method was invoked but a region with that name exists (user-based)
187          */
188         public function createRegionByName (string $regionName) {
189                 // Pre-check name
190                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('REGION-INFORMATION-DATABASE-WRAPPER: regionName=%s - CALLED!', $regionName));
191                 if (empty($regionName)) {
192                         // Throw IAE
193                         throw new InvalidArgumentException('Parameter "regionName" is empty', FramworkInterface::EXCEPTION_INVALID_ARGUMENT);
194                 } elseif ($this->ifRegionExists($regionName)) {
195                         // Throw BMCE
196                         throw new BadMethodCallException(sprintf('regionName=%s already exist', $regionName));
197                 }
198
199                 // Get user instance
200                 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
201
202                 // Get a dataset instance
203                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', [self::DB_TABLE_REGION_INFORMATION]);
204
205                 // Set the primary key
206                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
207
208                 // Add region name and assign user id
209                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID     , ($this->countTotalRows() + 1));
210                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME   , $regionName);
211                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
212
213                 // "Insert" this dataset instance completely into the database
214                 $this->queryInsertDataSet($dataSetInstance);
215
216                 // Post-check name
217                 assert($this->ifRegionExists($regionName));
218
219                 // Trace message
220                 /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGION-INFORMATION-DATABASE-WRAPPER: EXIT!');
221         }
222 }