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