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