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