3 namespace Org\Mxchange\City\Manager\City;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
12 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
31 class CityManager extends BaseManager implements ManageableCity {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this class
45 * @return $managerInstance An instance of a ManageableCity class
47 public final static function createCityManager () {
49 $managerInstance = new CityManager();
51 // Get database wrapper
52 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('city_info_db_wrapper_class');
55 $managerInstance->setWrapperInstance($wrapperInstance);
57 // Return the prepared instance
58 return $managerInstance;
62 * Checks whether the current user has already founded a city
64 * @return $isFounded Whether the current user has already founded a city
66 public function isCityAlreadyFounded () {
67 // Check if the currently set user has already founded a city
68 $isFounded = $this->getWrapperInstance()->ifUserHasFoundedCity();
75 * Checks whether the given city name is already taken
77 * @para $cityName Name of city
78 * @return $isTaken Whether the given city name is already taken
80 public function ifCityNameExists ($cityName) {
81 // Check if the given city name is taken
82 $isTaken = $this->getWrapperInstance()->ifCityExists($cityName);
89 * Founds the first city. A dummy region will also be created
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));
99 $cityName = $this->getRequestInstance()->getRequestElement(CityInformationDatabaseWrapper::DB_COLUMN_CITY_NAME);
102 assert(!$this->isCityAlreadyFounded());
103 assert(!$this->ifCityNameExists($cityName));
105 // Get region manager
106 $managerInstance = ManagerFactory::createManagerByType('region');
108 // The manager instance should be valid and no region should be created
109 assert($managerInstance instanceof ManageableRegion);
110 assert(!$managerInstance->ifUserHasCreatedRegion());
112 // Create first region and get back whole result
113 $regionResultInstance = $managerInstance->createFirstRegion();
116 $regionData = $regionResultInstance->current();
118 // Add region id from it
119 $this->getRequestInstance()->setRequestElement(CityInformationDatabaseWrapper::DB_COLUMN_CITY_REGION_ID, $regionData[RegionInformationDatabaseWrapper::DB_COLUMN_REGION_ID]);
121 // Then create the first city
122 $this->getWrapperInstance()->createCityByRequest($this->getRequestInstance());
126 * Renders the city map and forwards the output to the helper instance.
128 * @param $helperInstance An instance of a HelpableTemplate class
131 public function renderCityMap (HelpableTemplate $helperInstance) {
132 $this->partialStub('Please implement this method.');