-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.php
141 lines (119 loc) · 3.39 KB
/
Cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Beeblebrox3\DevShop;
class Cart
{
protected $session = null;
protected $cart = null;
protected $repository = null;
protected $devs = array();
protected $hydrated = false;
protected $cupoms = array (
'SHIPIT' => array (
'discount_value' => 0,
'discount_percentage' => 20,
'description' => 'Cupom de 20% de desconto',
),
);
protected $cupom = '';
public function __construct()
{
$sessionFactory = new \Aura\Session\SessionFactory;
$this->session = $sessionFactory->newInstance($_COOKIE);
$this->cart = $this->session->getSegment('Beeblebrox3\DevShop\Cart');
$this->cupom = $this->cart->get('cupom', '');
}
/**
*
* @param \Beeblebrox3\DevShop\Repositories\Developer $developer
* @param float $amount
*/
public function addDev(Repositories\Developer $developer, $amount = 1)
{
$items = $this->getItems();
$items[$developer->login] = (float) $amount;
$this->cart->set('items', $items);
}
/**
* @param string $developerLogin
*/
public function delDev($developerLogin)
{
$items = $this->getItems();
$developerLogin = (string) $developerLogin;
unset($items[$developerLogin]);
$this->cart->set('items', $items);
}
/**
* @return array
*/
private function getItems()
{
return $this->cart->get('items', array());
}
private function hydrateFromRepository()
{
$items = $this->getItems();
$devs = array();
$devsFromRepo = Repositories\Developer::whereIn('login', array_keys($items))->get();
foreach ($devsFromRepo as $dev) {
$devs[] = new CartItem($dev, $items[$dev->login]);
}
$this->devs = $devs;
$this->hydrated = true;
}
public function getCartItems()
{
$this->hydrateFromRepository();
return $this->devs;
}
public function asArray()
{
$items = $this->getCartItems();
$response = array(
'items' => array(),
'price' => 0,
'discount' => 0,
'total_price' => 0,
);
foreach ($items as $item) {
$response['items'][] = $item->toArray();
$response['price'] += $item->totalPrice();
}
$response['discount'] = $this->calcCupomDiscount($response['price']);
$response['total_price'] = round($response['price'] - $response['discount'], 2);
return $response;
}
/**
*
* @param int|float $price
* @return int|float
*/
private function calcCupomDiscount($price)
{
if (!$this->cupom || !isset($this->cupoms[$this->cupom])) {
return 0;
}
$cupom = $this->cupoms[$this->cupom];
if ($cupom['discount_value']) {
return $cupom['discount_value'];
} elseif ($cupom['discount_percentage']) {
return $price * $cupom['discount_percentage'] / 100;
} else {
return 0;
}
}
public function setCupom($code)
{
$code = (string) $code;
if (!isset($this->cupoms[$code])) {
return false;
}
$this->cupom = $code;
$this->cart->set('cupom', $this->cupom);
return true;
}
public function clean()
{
$this->cart->clear();
}
}