]> git.mxchange.org Git - city.git/blobdiff - application/city/classes/database/frontend/city_entities/sections/class_CitySectionsDatabaseFrontend.php
Continued:
[city.git] / application / city / classes / database / frontend / city_entities / sections / class_CitySectionsDatabaseFrontend.php
index 4044201b3153414b9e5ae435d1cb0fa606734cbd..f8608118d05fbbde90d3680763241cc67b1f2561 100644 (file)
@@ -6,8 +6,12 @@ namespace Org\Mxchange\City\Database\Frontend\Sections;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Registry\Registerable;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A database frontend for city sections. Sections are the smalles entity of a
  * city. They can be connected with each other and form a lot. Therefore only
@@ -21,7 +25,7 @@ use Org\Mxchange\CoreFramework\Registry\Registerable;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2015, 2016 City Developer Team
+ * @copyright  Copyright (c) 2015 - 2023 City Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -122,8 +126,16 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
         *
         * @param       $cityId         City id to check
         * @return      $isFound        Whether the city id is found
+        * @throws      InvalidArgumentException        If a parameter as an invalid value
         */
        public function ifCityHasSections (int $cityId) {
+               // Check parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: cityId=%d - CALLED!', $cityId));
+               if ($cityId < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('cityId=%d is not a valid id', $cityId), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Get search instance
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
@@ -140,9 +152,11 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                $resultInstance = $this->doSelectByCriteria($searchInstance);
 
                // Check if there is one entry
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: resultInstance=%s', $resultInstance->__toString()));
                $isFound = $resultInstance->next();
 
                // Return result
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: isFound=%d - EXIT!', intval($isFound)));
                return $isFound;
        }
 
@@ -151,11 +165,20 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
         *
         * @param       $cityId         City id to check
         * @return      $ids            Sections ids from initial expansion
+        * @throws      InvalidArgumentException        If a parameter as an invalid value
+        * @throws      BadMethodCallException  If the city has already sections but this method was invoked
         * @todo        Add handling of water types to make a more cooler map
         */
        public function doInitialCityExpansion (int $cityId) {
-               // Make sure this city has no sections
-               assert(!$this->ifCityHasSections($cityId));
+               // Check parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: cityId=%d - CALLED!', $cityId));
+               if ($cityId < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('cityId=%d is not a valid id', $cityId), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif ($this->ifCityHasSections($cityId)) {
+                       // Already has sections
+                       throw new BadMethodCallException(sprintf('cityId=%d has already sections', $cityId), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
+               }
 
                /*
                 * "Cache" max "radius" for initial city expansion. It is not a real
@@ -175,10 +198,8 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                // Calculate total sections
                $totalSections = (($radius + 1) * ($radius + 1) * $maxUp * ($maxDown + 1));
 
-               // Debug message:
-               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: radius=' . $radius . ',maxUp=' . $maxUp . ',maxDown=' . $maxDown . ',totalSections=' . $totalSections);
-
                // Get data set instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: radius=%d,maxUp=%d,maxDown=%d,totalSections=%d', $radius, $maxUp, $maxDown, $totalSections));
                $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_CITY_SECTIONS));
 
                // Add values for "zero point"
@@ -199,7 +220,7 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                $dataSetInstance->addCriteria(self::DB_COLUMN_SECTION_RESERVED          , self::IS_NOT_RESERVED);
 
                // Set primary key to 'city_id'/'section_id'
-               $dataSetInstance->setPrimaryKeyCombined(array(self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID));
+               $dataSetInstance->setPrimaryKeyCombined([self::DB_COLUMN_CITY_ID, self::DB_COLUMN_SECTION_ID]);
 
                // Entry id for counting each entry
                $dataSetInstance->setUniqueKey(self::DB_COLUMN_ENTRY_ID);
@@ -208,10 +229,10 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                $this->queryInsertDataSet($dataSetInstance);
 
                // Set section id to 2 as 1 is already initialized + init array
-               $sections = array();
+               $sections = [];
 
                // Output message to ask for user's patience ...
-               self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Writing ' . $totalSections . ' sections for city ' . $cityId . ' ... (this may takes some time)');
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('CITY-SECTIONS-DATABASE-FRONTEND: Writing %d sections for city %d ... (this may takes some time)', $totalSections, $cityId));
 
                // Expand half of it to north/south (north=positive, south=negative)
                for ($north = 1; $north < round($radius / 2); $north++) {
@@ -219,19 +240,17 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                        for ($west = 1; $west < round($radius / 2); $west++) {
                                // Expand up/down (including "zero point")
                                for ($z = $maxDown; $z < ($maxUp + 1); $z++) {
-                                       // Debug message
-                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
-
                                        // Fill array up with south/east/down ids
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: north=' . $north . ',west=' . $west . ',z=' . $z);
                                        $sections[($north * -1)][($west * -1)][$z]['type'] = self::SECTION_TYPE_EMPTY_LAND;
                                        $sections[($north * -1)][($west * -1)][$z]['sub']  = self::SECTION_SUB_TYPE_GRASS;
 
                                        // Fill up array with north/west/up ids (only air)
                                        $sections[$north][$west][$z]['type'] = self::SECTION_TYPE_AIR;
                                        $sections[$north][$west][$z]['sub']  = self::SECTION_SUB_TYPE_AIR;
-                               } // END - for
-                       } // END - for
-               } // END - for
+                               }
+                       }
+               }
 
                // Init section id with 2 as 1 is the "zero point"
                $sectionId = 2;
@@ -242,10 +261,8 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
                        foreach ($sectionX as $y => $sectionY) {
                                // Loop through up/down values
                                foreach ($sectionY as $z => $sectionData) {
-                                       // Debug message
-                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
-
                                        // Set all coordinates for positive directions
+                                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ',z=' . $z . ',sectionId=' . $sectionId);
                                        $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_ID        , $sectionId);
                                        $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_X, $x);
                                        $dataSetInstance->setCriteria(self::DB_COLUMN_SECTION_POSITION_Y, $y);
@@ -258,15 +275,15 @@ class CitySectionsDatabaseFrontend extends BaseDatabaseFrontend implements CityS
 
                                        // Count id up
                                        $sectionId++;
-                               } // END - foreach
+                               }
 
                                // Debug message
                                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ',y=' . $y . ' has been written.');
-                       } // END - foreach
+                       }
 
                        // Debug message
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: x=' . $x . ' has been written.');
-               } // END - foreach
+               }
 
                // Thank you for waiting! :-)
                self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: A total of ' . $totalSections . ' sections has been written for city id ' . $cityId . '.');