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