1 <?php
2
3 /* --------------------------------------------------------------
4 AddonValueStorageFactory.inc.php 2015-11-20 gm
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 MainFactory::load_class('AddonValueStorageFactoryInterface');
14
15 /**
16 * Class AddonValueStorageFactory
17 *
18 * @category System
19 * @package AddonValue
20 */
21 class AddonValueStorageFactory implements AddonValueStorageFactoryInterface
22 {
23 /**
24 * @var CI_DB_query_builder
25 */
26 protected $db;
27
28 /**
29 * Contains the container-storage mapping used for the creation of the correct storage object.
30 *
31 * @var array
32 */
33 protected $map = array(
34 'OrderInterface' => 'OrderAddonValueStorage',
35 'OrderItemInterface' => 'OrderItemAddonValueStorage',
36 'CategoryInterface' => 'CategoryAddonValueStorage',
37 'ProductInterface' => 'ProductAddonValueStorage'
38 );
39
40
41 /**
42 * AddonValueStorageFactory Constructor
43 *
44 * @param CI_DB_query_builder $db
45 */
46 public function __construct(CI_DB_query_builder $db)
47 {
48 $this->db = $db;
49 }
50
51
52 /**
53 * Creates the correct addon value storage container object.
54 *
55 * @param AddonValueContainerInterface $container
56 *
57 * @return AbstractAddonValueStorage
58 */
59 public function createAddonValueStorageByContainerObject(AddonValueContainerInterface $container)
60 {
61 $addonValueStorage = null;
62
63 foreach($this->map as $containerClass => $storageClass)
64 {
65 if(is_a($container, $containerClass) === true)
66 {
67 $addonValueStorage = MainFactory::create($storageClass, $this->db);
68 }
69 }
70
71 if($addonValueStorage === null)
72 {
73 throw new UnexpectedValueException('No AddonValueStorage class matches the provided $container: '
74 . get_class($container));
75 }
76
77 return $addonValueStorage;
78 }
79 }