@@ 100-117 (lines=18) @@ | ||
97 | * @param CounterItemContract $itemContract |
|
98 | * @return bool |
|
99 | */ |
|
100 | public function change(array $item, CounterItemContract $itemContract): bool |
|
101 | { |
|
102 | if ($this->validate($item, ['id', 'user_id', 'count']) === false) { |
|
103 | return false; |
|
104 | } |
|
105 | ||
106 | $itemFromRedis = $this->redis->hget($this->normalizeKey((int)$item['user_id']), $item['id']); |
|
107 | ||
108 | if (!is_null($itemFromRedis)) { |
|
109 | $itemFromRedis = unserialize($itemFromRedis); |
|
110 | } else { |
|
111 | return false; |
|
112 | } |
|
113 | ||
114 | $item['count'] = $itemContract->execute((int)$itemFromRedis['count'], $item['count']); |
|
115 | $this->addRow($item); |
|
116 | return true; |
|
117 | } |
|
118 | ||
119 | /** |
|
120 | * Make Discount |
|
@@ 125-141 (lines=17) @@ | ||
122 | * @param array $item |
|
123 | * @return bool |
|
124 | */ |
|
125 | public function discount(DiscountContract $contract, array $item): bool |
|
126 | { |
|
127 | if ($this->validate($item, ['id', 'user_id', 'price']) === false) { |
|
128 | return false; |
|
129 | } |
|
130 | ||
131 | $itemFromRedis = $this->redis->hget($this->normalizeKey((int)$item['user_id']), $item['id']); |
|
132 | ||
133 | if (!is_null($itemFromRedis)) { |
|
134 | $itemFromRedis = unserialize($itemFromRedis); |
|
135 | $itemFromRedis['discount'] = $contract->make($itemFromRedis['price']); |
|
136 | $this->redis->hset($this->normalizeKey((int)$item['user_id']), $item['id'], serialize($itemFromRedis)); |
|
137 | return true; |
|
138 | } else { |
|
139 | return false; |
|
140 | } |
|
141 | } |
|
142 | ||
143 | /** |
|
144 | * Get all items to user id |