-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserFactory.php
82 lines (71 loc) · 2.13 KB
/
UserFactory.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
<?php
namespace Biplane\Bundle\YandexDirectBundle;
use Biplane\YandexDirect\User;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* UserFactory
*
* @author Denis Vasilev <[email protected]>
*/
class UserFactory
{
private $dispatcher;
private $defaultOptions;
private $defaultUser;
private $users = array();
/**
* Constructor.
*
* @param EventDispatcherInterface $dispatcher The event dispatcher
* @param string $locale The default locale
* @param array|null $defaultUser The options of the default user or null
*/
public function __construct(EventDispatcherInterface $dispatcher, $locale, array $defaultUser = null)
{
$this->dispatcher = $dispatcher;
$this->defaultOptions = array(
'locale' => $locale
);
$this->defaultUser = $defaultUser;
}
/**
* Enables the sandbox mode.
*/
public function enableSandbox()
{
$this->defaultOptions['sandbox'] = true;
}
/**
* Disables the sandbox mode.
*/
public function disableSandbox()
{
$this->defaultOptions['sandbox'] = false;
}
/**
* Gets User object for the specific options.
*
* When the options is null will be try to create the default user.
*
* @param array|null $options The options for user
*
* @return User
*/
public function getUser(array $options = null)
{
if (null === $options) {
if (is_array($this->defaultUser)) {
return $this->getUser($this->defaultUser);
}
throw new \LogicException(
'The default user is not available, options is not defined for it.' .
' You may set options in the bundle configuration biplane_yandex_direct.user'
);
}
$key = isset($options['login']) ? $options['login'] : $options['access_token'];
if (!isset($this->users[$key])) {
$this->users[$key] = new User($options + $this->defaultOptions, $this->dispatcher);
}
return $this->users[$key];
}
}