]> git.mxchange.org Git - city.git/blob - application/city/classes/factories/city_daemon/class_CityDaemonFactory.php
Next wave:
[city.git] / application / city / classes / factories / city_daemon / class_CityDaemonFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Daemon\Factory;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9 use Org\Mxchange\CoreFramework\Response\Responseable;
10
11 /**
12  * A factory class for cities
13  *
14  * @author              Roland Haeder <webmaster@ship-simu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2015, 2016 City Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.ship-simu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class CityDaemonFactory extends ObjectFactory {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Returns a singleton (registry-based) CityHelper instance
46          *
47          * @param       $requestInstance        An instance of a class with an Requestable interface
48          * @param       $responseInstance       An instance of a class with an Responseable interface
49          * @return      $cityInstance           An instance of a CityHelper class
50          * @throws      FactoryRequiredParameterException       If not all parameters are set and no instance 'city' is set.
51          */
52         public static final function createCityDaemonInstance (Requestable $requestInstance = NULL, Responseable $responseInstance = NULL) {
53                 // Get new factory instance
54                 $factoryInstance = new CityDaemonFactory();
55
56                 // If there is no handler?
57                 if (Registry::getRegistry()->instanceExists('city')) {
58                         // Get handler from registry
59                         $cityInstance = Registry::getRegistry()->getInstance('city');
60                 } elseif (($requestInstance instanceof Requestable) && ($responseInstance instanceof Responseable)) {
61                         // The default city-mode is from our configuration
62                         $cityMode = $factoryInstance->getConfigInstance()->getConfigEntry('city_default_mode');
63
64                         // Is the city 'mode' parameter set?
65                         if ($requestInstance->isRequestElementSet('mode')) {
66                                 // Then use this which overrides the config entry temporarily
67                                 $cityMode = $requestInstance->getRequestElement('mode');
68                         } else {
69                                 // Set it for easier re-usage
70                                 $requestInstance->setRequestElement('mode', $cityMode);
71                         }
72
73                         // Now convert the city-mode in a class name
74                         $className = 'Org\Mxchange\City\Daemon\Simulation\Simulation' . $factoryInstance->convertToClassName($cityMode) . 'CityDaemon';
75
76                         // Get the city instance
77                         $cityInstance = ObjectFactory::createObjectByName($className, array($requestInstance));
78
79                         // Get a registry
80                         $applicationInstance = Registry::getRegistry()->getInstance('app');
81
82                         // Set the app instance
83                         $cityInstance->setApplicationInstance($applicationInstance);
84
85                         // Add city-specific filters
86                         $cityInstance->addExtraFilters($applicationInstance->getControllerInstance(), $responseInstance);
87                 } else {
88                         // Throw an exception here
89                         throw new FactoryRequiredParameterException($factoryInstance, self::EXCEPTION_FACTORY_REQUIRE_PARAMETER);
90                 }
91
92                 // Return the instance
93                 return $cityInstance;
94         }
95 }