]> git.mxchange.org Git - city.git/blob - application/city/classes/factories/city_daemon/class_CityDaemonFactory.php
Continued:
[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\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
8
9 /**
10  * A factory class for cities
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 CityDaemonFactory extends ObjectFactory {
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          * Returns a singleton (registry-based) CityHelper instance
44          *
45          * @param       $requestInstance        An instance of a class with an Requestable interface
46          * @param       $responseInstance       An instance of a class with an Responseable interface
47          * @return      $cityInstance           An instance of a CityHelper class
48          * @throws      FactoryRequiredParameterException       If not all parameters are set and no instance 'city' is set.
49          */
50         public static final function createCityDaemonInstance (Requestable $requestInstance = NULL, Responseable $responseInstance = NULL) {
51                 // Get new factory instance
52                 $factoryInstance = new CityDaemonFactory();
53
54                 // If there is no handler?
55                 if (Registry::getRegistry()->instanceExists('city')) {
56                         // Get handler from registry
57                         $cityInstance = Registry::getRegistry()->getInstance('city');
58                 } elseif (($requestInstance instanceof Requestable) && ($responseInstance instanceof Responseable)) {
59                         // The default city-mode is from our configuration
60                         $cityMode = $factoryInstance->getConfigInstance()->getConfigEntry('city_default_mode');
61
62                         // Is the city 'mode' parameter set?
63                         if ($requestInstance->isRequestElementSet('mode')) {
64                                 // Then use this which overrides the config entry temporarily
65                                 $cityMode = $requestInstance->getRequestElement('mode');
66                         } else {
67                                 // Set it for easier re-usage
68                                 $requestInstance->setRequestElement('mode', $cityMode);
69                         }
70
71                         // Now convert the city-mode in a class name
72                         $className = 'Simulation' . $factoryInstance->convertToClassName($cityMode) . 'CityDaemon';
73
74                         // Get the city instance
75                         $cityInstance = ObjectFactory::createObjectByName($className, array($requestInstance));
76
77                         // Get a registry
78                         $applicationInstance = Registry::getRegistry()->getInstance('app');
79
80                         // Set the app instance
81                         $cityInstance->setApplicationInstance($applicationInstance);
82
83                         // Add city-specific filters
84                         $cityInstance->addExtraFilters($applicationInstance->getControllerInstance(), $responseInstance);
85                 } else {
86                         // Throw an exception here
87                         throw new FactoryRequiredParameterException($factoryInstance, self::EXCEPTION_FACTORY_REQUIRE_PARAMETER);
88                 }
89
90                 // Return the instance
91                 return $cityInstance;
92         }
93 }