1 <?php
2 /* --------------------------------------------------------------
3 OrderItemAttributeRepositoryFactory.inc.php 2015-11-17
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('OrderItemAttributeRepositoryFactoryInterface');
13
14 /**
15 * Class OrderItemAttributeRepositoryFactory
16 *
17 * @category System
18 * @package Order
19 * @subpackage Factories
20 */
21 class OrderItemAttributeRepositoryFactory implements OrderItemAttributeRepositoryFactoryInterface
22 {
23 /**
24 * Repository array.
25 * Default: ['attribute' => null, 'property' => null]
26 *
27 * @var array
28 */
29 protected $repositoryArray = array('attribute' => null, 'property' => null);
30
31
32 /**
33 * Instance of query builder which is passed to the reader, writer and deleter.
34 *
35 * @var Ci_Db_query_builder
36 */
37 protected $db;
38
39
40 /**
41 * OrderItemAttributeRepositoryFactory constructor.
42 *
43 * @param CI_DB_query_builder $dbQueryBuilder Query builder.
44 */
45 public function __construct(CI_DB_query_builder $dbQueryBuilder)
46 {
47 $this->db = $dbQueryBuilder;
48 }
49
50
51 /**
52 * Creates an order item attribute repository by the given class name.
53 *
54 * @param string $className Name of the attribute class.
55 *
56 * @return OrderItemAttributeRepository Order item attribute repository instance.
57 *
58 * @throws InvalidArgumentException On invalid argument.
59 */
60 public function createRepositoryByAttributeClass($className)
61 {
62 if(strtolower($className) === 'attribute')
63 {
64 $this->_setRepositoryArrayAttributeIfNull();
65
66 return $this->repositoryArray['attribute'];
67 }
68 elseif(strtolower($className) === 'property')
69 {
70 $this->_setRepositoryArrayPropertyIfNull();
71
72 return $this->repositoryArray['property'];
73 }
74 else
75 {
76 throw new \InvalidArgumentException('Passed argument ' . $className . ' is not valid!');
77 }
78 }
79
80
81 /**
82 * Creates an order item attribute repository by the given object type.
83 *
84 * @param OrderItemAttributeInterface $itemAttribute Order item attribute.
85 *
86 * @return OrderItemAttributeRepository Order item attribute repository instance.
87 * @throws InvalidArgumentException On invalid argument.
88 */
89 public function createRepositoryByAttributeObject(OrderItemAttributeInterface $itemAttribute)
90 {
91 if($itemAttribute instanceof OrderItemAttribute)
92 {
93 $this->_setRepositoryArrayAttributeIfNull();
94
95 return $this->repositoryArray['attribute'];
96 }
97 elseif($itemAttribute instanceof OrderItemProperty)
98 {
99 $this->_setRepositoryArrayPropertyIfNull();
100
101 return $this->repositoryArray['property'];
102 }
103 else
104 {
105 throw new \InvalidArgumentException('Passed argument is not valid!');
106 }
107 }
108
109
110 /**
111 * Creates an array which contain all repository of type OrderItemAttributeRepositoryInterface.
112 *
113 * @return OrderItemAttributeRepository[]
114 */
115 public function createRepositoryArray()
116 {
117 $this->_setRepositoryArrayAttributeIfNull();
118 $this->_setRepositoryArrayPropertyIfNull();
119
120 return $this->repositoryArray;
121 }
122
123
124 /**
125 * Creates an OrderItemAttributeRepository object and assigns it to the key 'attribute'
126 * of the $repositoryArray array, when its current value is null.
127 */
128 protected function _setRepositoryArrayAttributeIfNull()
129 {
130 if(null === $this->repositoryArray['attribute'])
131 {
132 $factory = MainFactory::create('OrderItemAttributeFactory');
133
134 $reader = MainFactory::create('OrderItemAttributeRepositoryReader', $this->db,
135 $factory);
136 $writer = MainFactory::create('OrderItemAttributeRepositoryWriter', $this->db);
137 $deleter = MainFactory::create('OrderItemAttributeRepositoryDeleter', $this->db);
138 $this->repositoryArray['attribute'] = MainFactory::create('OrderItemAttributeRepository', $reader, $writer,
139 $deleter);
140 }
141 }
142
143
144 /**
145 * Creates an OrderItemPropertyRepository object and assigns it to the key 'property'
146 * of the $repositoryArray array, when its current value is null.
147 */
148 protected function _setRepositoryArrayPropertyIfNull()
149 {
150 if(null === $this->repositoryArray['property'])
151 {
152 $factory = MainFactory::create('OrderItemPropertyFactory');
153
154 $reader = MainFactory::create('OrderItemPropertyRepositoryReader', $this->db,
155 $factory);
156 $writer = MainFactory::create('OrderItemPropertyRepositoryWriter', $this->db);
157 $deleter = MainFactory::create('OrderItemPropertyRepositoryDeleter', $this->db);
158 $this->repositoryArray['property'] = MainFactory::create('OrderItemPropertyRepository', $reader, $writer,
159 $deleter);
160 }
161 }
162 }
163