-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrackerFactory.php
93 lines (72 loc) · 2.05 KB
/
TrackerFactory.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
<?php
namespace Strego\GoogleBundle;
use Strego\GoogleBundle\Model\Tracker;
class TrackerFactory
{
/**
* @var array
*/
private $trackerConfigs = array();
private $defaultTracker;
/**
* @var array;
*/
private $trackers = array();
/**
* @param array $connections
*/
public function __construct(array $trackerConfigs = array(), $defaultTracker)
{
$this->trackerConfigs = $trackerConfigs;
$this->defaultTracker = $defaultTracker;
}
/**
* @param string $name
* @throws \RuntimeException if there are no connections
* @throws \InvalidArgumentException if the given connection name is unknown
* @return Tracker
*/
public function getTracker($name = null)
{
if ($name == null) {
$name = $this->getDefaultTracker();
}
if (count($this->trackerConfigs) == 0) {
throw new \RuntimeException('No trackers found');
}
if (!array_key_exists($name, $this->trackerConfigs)) {
throw new \InvalidArgumentException(sprintf('Unknown tracker %s', $name));
}
if (!array_key_exists($name, $this->trackers)) {
$trackerConfig = $this->trackerConfigs[$name];
$this->trackers[$name] = $this->createTracker($name, $trackerConfig);
}
return $this->trackers[$name];
}
public function getTrackers()
{
$trackers = array();
foreach ($this->trackerConfigs as $name => $conf) {
$trackers[$name] = $this->getTracker($name);
}
return $trackers;
}
/**
* Create a tracker by config
*
* @param array $config
*
* @return Tracker
*/
public function createTracker($name, $config = null)
{
$tracker = new Tracker($config['accountId']);
unset($config['accountId']);
$config['name'] = $name;
$tracker->setConfig($config);
return $tracker;
}
public function getDefaultTracker(){
return $this->defaultTracker;
}
}