This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
/
BasketManager.php
executable file
·200 lines (173 loc) · 4.37 KB
/
BasketManager.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare (strict_types = 1);
/*
* This file is part of Laravel Basket.
*
* (c) Brian Faust <hello@basecode.sh>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Artisanry\Laravel\Basket;
use Artisanry\Basket\Contracts\Jurisdiction;
use Artisanry\Laravel\Basket\Storage\StorageInterface;
use Illuminate\Events\Dispatcher;
class BasketManager
{
/**
* The storage driver used by Basket.
*
* @var \Artisanry\Laravel\Basket\Storage\StorageInterface
*/
protected $storage;
/**
* The event dispatcher instance.
*
* @var \Illuminate\Events\Dispatcher
*/
protected $dispatcher;
/**
* The basket factory instance.
*
* @var \Artisanry\Laravel\Basket\BasketFactory
*/
protected $basket;
/**
* The basket jurisdiction.
*
* @var \Artisanry\Basket\Contracts\Jurisdiction
*/
protected $jurisdiction;
/**
* Constructor.
*
* @param \Artisanry\Laravel\Basket\StorageInterface $storage
* @param \Illuminate\Events\Dispatcher $dispatcher
*/
public function __construct(StorageInterface $storage, Dispatcher $dispatcher, Jurisdiction $jurisdiction)
{
$this->storage = $storage;
$this->dispatcher = $dispatcher;
$this->jurisdiction = $jurisdiction;
}
/**
* Returns the Basket instance identifier.
*
* @return mixed
*/
public function getInstance()
{
return $this->storage->getInstance();
}
/**
* Sets the Basket instance identifier.
*
* @param mixed $instance
*/
public function setInstance($instance)
{
$this->storage->setInstance($instance);
}
/**
* Returns the storage driver.
*
* @return mixed
*/
public function getStorage()
{
return $this->storage;
}
/**
* Sets the storage driver.
*
* @param \Artisanry\Laravel\Basket\Storage\StorageInterface $storage
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
}
/**
* Returns the event dispatcher instance.
*
* @return \Illuminate\Events\Dispatcher
*/
public function getDispatcher()
{
return $this->dispatcher;
}
/**
* Sets the event dispatcher instance.
*
* @param \Illuminate\Events\Dispatcher $dispatcher
*/
public function setDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* Returns the Basket jurisdiction.
*
* @return \Artisanry\Basket\Contracts\Jurisdiction
*/
public function getJurisdiction()
{
return $this->jurisdiction;
}
/**
* Sets the Basket jurisdiction.
*
* @param \Artisanry\Basket\Contracts\Jurisdiction $jurisdiction
*/
public function setJurisdiction(Jurisdiction $jurisdiction)
{
$this->jurisdiction = $jurisdiction;
}
/**
* Fires an event.
*
* @param string $event
* @param mixed $data
*/
public function fire($event, $data)
{
$this->dispatcher->fire("artisanry.basket.{$event}", $data);
}
/**
* Handle dynamic calls into BasketFactory.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->getFactory(), $method], $parameters);
}
/**
* Returns the basket contents.
*
* @return \Artisanry\Laravel\Basket\BasketFactory
*/
private function getFactory()
{
// return the current instance
if ($this->basket) {
return $this->basket;
}
// return the basket from session storage
if ($this->storage->has()) {
$this->basket = $this->storage->get()->setManager($this);
$this->fire('restored', $this->basket);
return $this->basket;
}
// create a new basket
if (!$this->storage->has()) {
$this->basket = (new BasketFactory())->setManager($this);
$this->storage->put($this->basket);
$this->storage->save();
$this->fire('created', $this->basket);
return $this->basket;
}
}
}