]> git.mxchange.org Git - city.git/blob - application/city/classes/manager/city/class_CityManager.php
Continued:
[city.git] / application / city / classes / manager / city / class_CityManager.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Manager\City;
4
5 // Import application-specific stuff
6 use Org\Mxchange\City\Database\Frontend\Information\CityInformationDatabaseFrontend;
7 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
8
9 // Import framework stuff
10 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
11 use Org\Mxchange\CoreFramework\Factory\Database\Frontend\DatabaseFrontendFactory;
12 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
13 use Org\Mxchange\CoreFramework\Request\Requestable;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
15
16 /**
17  * A City manager
18  *
19  * @author              Roland Haeder <webmaster@ship-simu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2015 - 2023 City Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.ship-simu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37  */
38 class CityManager extends BaseManager implements ManageableCity {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of this class
51          *
52          * @return      $managerInstance        An instance of a ManageableCity class
53          */
54         public final static function createCityManager () {
55                 // Get new instance
56                 $managerInstance = new CityManager();
57
58                 // Get database frontend
59                 $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('city_info_db_frontend_class');
60
61                 // And set it here
62                 $managerInstance->setFrontendInstance($frontendInstance);
63
64                 // Return the prepared instance
65                 return $managerInstance;
66         }
67
68         /**
69          * Checks whether the current user has already founded a city
70          *
71          * @return      $isFounded      Whether the current user has already founded a city
72          */
73         public function isCityAlreadyFounded () {
74                 // Check if the currently set user has already founded a city
75                 $isFounded = $this->getFrontendInstance()->ifUserHasFoundedCity();
76
77                 // Return result
78                 return $isFounded;
79         }
80
81         /**
82          * Checks whether the given city name is already taken
83          *
84          * @para        $cityName       Name of city
85          * @return      $isTaken        Whether the given city name is already taken
86          */
87         public function ifCityNameExists (string $cityName) {
88                 // Check if the given city name is taken
89                 $isTaken = $this->getFrontendInstance()->ifCityExists($cityName);
90
91                 // Return result
92                 return $isTaken;
93         }
94
95         /**
96          * Founds the first city. A dummy region will also be created
97          *
98          * @return      void
99          */
100         public function foundFirstCity () {
101                 // Check on request instance and 'city_name' element
102                 assert(FrameworkBootstrap::getRequestInstance() instanceof Requestable);
103                 assert(FrameworkBootstrap::getRequestInstance()->isRequestElementSet(CityInformationDatabaseFrontend::DB_COLUMN_CITY_NAME));
104
105                 // Get city name
106                 $cityName = FrameworkBootstrap::getRequestInstance()->getRequestElement(CityInformationDatabaseFrontend::DB_COLUMN_CITY_NAME);
107
108                 // Some pre-checks
109                 assert(!$this->isCityAlreadyFounded());
110                 assert(!$this->ifCityNameExists($cityName));
111
112                 // Get region manager
113                 $managerInstance = ManagerFactory::createManagerByType('region');
114
115                 // The manager instance should be valid and no region should be created
116                 assert($managerInstance instanceof ManageableRegion);
117                 assert(!$managerInstance->ifUserHasCreatedRegion());
118
119                 // Create first region and get back whole result
120                 $regionResultInstance = $managerInstance->createFirstRegion();
121
122                 // Get current entry
123                 $regionData = $regionResultInstance->current();
124
125                 // Add region id from it
126                 FrameworkBootstrap::getRequestInstance()->setRequestElement(CityInformationDatabaseFrontend::DB_COLUMN_CITY_REGION_ID, $regionData[RegionInformationDatabaseFrontend::DB_COLUMN_REGION_ID]);
127
128                 // Then create the first city
129                 $this->getFrontendInstance()->createCityByRequest(FrameworkBootstrap::getRequestInstance());
130         }
131
132         /**
133          * Renders the city map and forwards the output to the helper instance.
134          *
135          * @param       $helperInstance         An instance of a HelpableTemplate class
136          * @return      void
137          */
138         public function renderCityMap (HelpableTemplate $helperInstance) {
139                 DebugMiddleware::getSelfInstance()->partialStub('Please implement this method.');
140         }
141 }