1 <?php
2
3 /* --------------------------------------------------------------
4 OrderTotalRepositoryWriter.inc.php 2015-11-10 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('OrderTotalRepositoryWriterInterface');
14
15 /**
16 * Class OrderTotalRepositoryWriter
17 *
18 * @category System
19 * @package Order
20 * @subpackage Repositories
21 */
22 class OrderTotalRepositoryWriter implements OrderTotalRepositoryWriterInterface
23 {
24 /**
25 * Query builder.
26 * @var CI_DB_query_builder
27 */
28 protected $db;
29
30 /**
31 * Table.
32 * @var string
33 */
34 protected $table = 'orders_total';
35
36 /**
37 * ID column.
38 * @var string
39 */
40 protected $key = 'orders_total_id';
41
42
43 /**
44 * OrderTotalRepositoryWriter constructor.
45 *
46 * @param CI_DB_query_builder $dbQueryBuilder Query builder.
47 */
48 public function __construct(CI_DB_query_builder $dbQueryBuilder)
49 {
50 $this->db = $dbQueryBuilder;
51 }
52
53
54 /**
55 * Inserts an order total item to an order by the given order ID.
56 *
57 * @param IdType $orderId ID of the order.
58 * @param OrderTotalInterface $orderTotal Order total item to insert.
59 *
60 * @return int ID of stored order total item.
61 */
62 public function insertIntoOrder(IdType $orderId, OrderTotalInterface $orderTotal)
63 {
64 $orderIdValue = $orderId->asInt();
65 $orderTotalArray = array(
66 'orders_id' => $orderIdValue,
67 'title' => $orderTotal->getTitle(),
68 'text' => $orderTotal->getValueText(),
69 'value' => $orderTotal->getValue(),
70 'class' => $orderTotal->getClass(),
71 'sort_order' => $orderTotal->getSortOrder()
72 );
73
74 $this->db->insert($this->table, $orderTotalArray);
75
76 return $this->db->insert_id();
77 }
78
79
80 /**
81 * Updates the passed order total item.
82 *
83 * @param StoredOrderTotalInterface $orderTotal Order total item to update.
84 *
85 * @return OrderTotalRepositoryWriter Same instance for method chaining.
86 */
87 public function update(StoredOrderTotalInterface $orderTotal)
88 {
89 $orderTotalArray = array(
90 'title' => $orderTotal->getTitle(),
91 'text' => $orderTotal->getValueText(),
92 'value' => $orderTotal->getValue(),
93 'class' => $orderTotal->getClass(),
94 'sort_order' => $orderTotal->getSortOrder()
95 );
96
97 $this->db->update($this->table, $orderTotalArray, array($this->key => $orderTotal->getOrderTotalId()));
98
99 return $this;
100 }
101 }