1 <?php
2
3 /* --------------------------------------------------------------
4 OrderTotalRepositoryReader.inc.php 2015-11-16
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('OrderTotalRepositoryReaderInterface');
14
15 /**
16 * Class OrderTotalRepositoryReader
17 *
18 * @category System
19 * @package Order
20 * @subpackage Repositories
21 */
22 class OrderTotalRepositoryReader implements OrderTotalRepositoryReaderInterface
23 {
24 /**
25 * Order total factory.
26 * @var OrderTotalFactory
27 */
28 protected $orderTotalFactory;
29
30 /**
31 * Query builder.
32 * @var CI_DB_query_builder
33 */
34 protected $db;
35
36 /**
37 * Table.
38 * @var string
39 */
40 protected $table = 'orders_total';
41
42 /**
43 * ID column.
44 * @var string
45 */
46 protected $key = 'orders_total_id';
47
48
49 /**
50 * OrderTotalRepositoryReader constructor.
51 *
52 * @param CI_DB_query_builder $db Query builder.
53 * @param OrderTotalFactoryInterface $orderTotalFactory Order total factory.
54 */
55 public function __construct(CI_DB_query_builder $db, OrderTotalFactoryInterface $orderTotalFactory)
56 {
57 $this->db = $db;
58 $this->orderTotalFactory = $orderTotalFactory;
59 }
60
61
62 /**
63 * Returns an StoredOrderTotal object by the given ID.
64 *
65 * @param IdType $orderTotalId ID of order total item.
66 *
67 * @throws UnexpectedValueException If record does not exist.
68 * @return StoredOrderTotal Fetched order total.
69 */
70 public function getTotalById(IdType $orderTotalId)
71 {
72 $data = $this->db->where($this->key, $orderTotalId->asInt())->get($this->table)->row_array();
73
74 if(empty($data))
75 {
76 throw new UnexpectedValueException('No order total record matches the provided $orderTotalId: '
77 . $orderTotalId->asInt());
78 }
79
80 return $this->_createStoredOrderTotalItem($data);
81 }
82
83
84 /**
85 * Returns a StoredOrderTotalCollection of StoredOrderTotal objects by the given order ID.
86 *
87 * @param IdType $orderOrderId ID of order item.
88 *
89 * @return StoredOrderTotalCollection Fetched order total collection.
90 */
91 public function getTotalsByOrderId(IdType $orderOrderId)
92 {
93 $storedOrderTotalItems = array();
94
95 $results = $this->db->where('orders_id', $orderOrderId->asInt())
96 ->order_by('sort_order ASC')
97 ->order_by('orders_total_id ASC')
98 ->get($this->table);
99
100 foreach($results->result_array() as $result)
101 {
102 $storedOrderTotalItems[] = $this->_createStoredOrderTotalItem($result);
103 }
104
105 $storedOrderTotalCollection = MainFactory::create('StoredOrderTotalCollection', $storedOrderTotalItems);
106
107 return $storedOrderTotalCollection;
108 }
109
110
111 /**
112 * Creates a StoredOrderTotal instance based on the given result set array.
113 *
114 * @param array $storedOrderTotalData Result set from database.
115 *
116 * @return StoredOrderTotal Created order total.
117 */
118 protected function _createStoredOrderTotalItem(array $storedOrderTotalData)
119 {
120 $storedOrderTotal = $this->orderTotalFactory->createStoredOrderTotal(new IdType($storedOrderTotalData[$this->key]));
121 $storedOrderTotal->setTitle(new StringType($storedOrderTotalData['title']));
122 $storedOrderTotal->setValueText(new StringType($storedOrderTotalData['text']));
123 $storedOrderTotal->setValue(new DecimalType($storedOrderTotalData['value']));
124 $storedOrderTotal->setClass(new StringType($storedOrderTotalData['class']));
125 $storedOrderTotal->setSortOrder(new IntType($storedOrderTotalData['sort_order']));
126
127 return $storedOrderTotal;
128 }
129 }