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