|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BoxedCode\Tracking; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class TrackerFactory |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Container instance. |
|
12
|
|
|
* |
|
13
|
|
|
* @var \Illuminate\Contracts\Container\Container |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $container; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Tracker type array. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $trackers; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* TrackerFactory constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
|
28
|
|
|
* @param array $trackers |
|
29
|
|
|
*/ |
|
30
|
9 |
|
public function __construct(Container $container, array $trackers) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$this->container = $container; |
|
33
|
|
|
|
|
34
|
9 |
|
foreach ($trackers as $abstract) { |
|
35
|
9 |
|
$handle = $container[$abstract]->getHandle(); |
|
36
|
|
|
|
|
37
|
9 |
|
$this->trackers[$handle] = $abstract; |
|
38
|
9 |
|
} |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Source a model from an argument. |
|
43
|
|
|
* |
|
44
|
|
|
* @param $arg |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
5 |
|
protected function sourceModel($arg) |
|
48
|
|
|
{ |
|
49
|
5 |
|
if (is_string($arg)) { |
|
50
|
2 |
|
$arg = TrackableResourceModel::findOrFail($arg); |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
if (! $arg instanceof TrackableResourceModel) { |
|
54
|
1 |
|
$name = ('object' === gettype($arg)) ? get_class($arg) : 'object'; |
|
55
|
|
|
|
|
56
|
1 |
|
throw new InvalidArgumentException( |
|
57
|
1 |
|
"Invalid resource, must be string identifier or TrackableResourceModel. [$name]" |
|
58
|
1 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return $arg; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Destroy a tracker by id or model. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|\BoxedCode\Tracking\TrackableResourceModel $mixed |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function destroy($mixed) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$model = $this->sourceModel($mixed); |
|
72
|
|
|
|
|
73
|
2 |
|
$model->delete(); |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get a tracker by id or model. |
|
78
|
|
|
* |
|
79
|
|
|
* @param $mixed |
|
80
|
|
|
* @return string|\BoxedCode\Tracking\TrackableResourceModel $mixed |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function resource($mixed) |
|
83
|
|
|
{ |
|
84
|
3 |
|
$model = $this->sourceModel($mixed); |
|
85
|
|
|
|
|
86
|
2 |
|
$instance = $this->container->make($model->type); |
|
87
|
|
|
|
|
88
|
2 |
|
$instance->setModel($model); |
|
89
|
|
|
|
|
90
|
2 |
|
return $instance; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Dynamically pass calls to tracker instances. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $name |
|
97
|
|
|
* @param array $arguments |
|
98
|
|
|
* @return \BoxedCode\Tracking\Contracts\Tracker |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function __call($name, $arguments = []) |
|
101
|
|
|
{ |
|
102
|
1 |
|
if (array_key_exists($name, $this->trackers)) { |
|
103
|
1 |
|
$tracker = $this->container->make($this->trackers[$name]); |
|
104
|
|
|
|
|
105
|
1 |
|
$attr = $tracker->getModelAttributes($arguments); |
|
106
|
|
|
|
|
107
|
1 |
|
$tracker->setModel(TrackableResourceModel::create($attr)); |
|
108
|
|
|
|
|
109
|
1 |
|
return $tracker; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|