blob: 5e606978fe0250ddf0dd2c41859e61a77dfd8ce9 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Adldap;
4
5use Adldap\Log\EventLogger;
6use Adldap\Connections\Ldap;
7use InvalidArgumentException;
8use Adldap\Log\LogsInformation;
9use Adldap\Connections\Provider;
10use Adldap\Events\DispatchesEvents;
11use Adldap\Connections\ProviderInterface;
12use Adldap\Connections\ConnectionInterface;
13use Adldap\Configuration\DomainConfiguration;
14
15class Adldap implements AdldapInterface
16{
17 use DispatchesEvents;
18 use LogsInformation;
19 /**
20 * The default provider name.
21 *
22 * @var string
23 */
24 protected $default = 'default';
25
26 /**
27 * The connection providers.
28 *
29 * @var array
30 */
31 protected $providers = [];
32
33 /**
34 * The events to register listeners for during initialization.
35 *
36 * @var array
37 */
38 protected $listen = [
39 'Adldap\Auth\Events\*',
40 'Adldap\Query\Events\*',
41 'Adldap\Models\Events\*',
42 ];
43
44 /**
45 * {@inheritdoc}
46 */
47 public function __construct(array $providers = [])
48 {
49 foreach ($providers as $name => $config) {
50 $this->addProvider($config, $name);
51 }
52
53 if ($default = key($providers)) {
54 $this->setDefaultProvider($default);
55 }
56
57 $this->initEventLogger();
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function addProvider($config, $name = 'default', ConnectionInterface $connection = null)
64 {
65 if ($this->isValidConfig($config)) {
66 $config = new Provider($config, $connection ?? new Ldap($name));
67 }
68
69 if ($config instanceof ProviderInterface) {
70 $this->providers[$name] = $config;
71
72 return $this;
73 }
74
75 throw new InvalidArgumentException(
76 "You must provide a configuration array or an instance of Adldap\Connections\ProviderInterface."
77 );
78 }
79
80 /**
81 * Determines if the given config is valid.
82 *
83 * @param mixed $config
84 *
85 * @return bool
86 */
87 protected function isValidConfig($config)
88 {
89 return is_array($config) || $config instanceof DomainConfiguration;
90 }
91
92 /**
93 * {@inheritdoc}
94 */
95 public function getProviders()
96 {
97 return $this->providers;
98 }
99
100 /**
101 * {@inheritdoc}
102 */
103 public function getProvider($name)
104 {
105 if (array_key_exists($name, $this->providers)) {
106 return $this->providers[$name];
107 }
108
109 throw new AdldapException("The connection provider '$name' does not exist.");
110 }
111
112 /**
113 * {@inheritdoc}
114 */
115 public function setDefaultProvider($name = 'default')
116 {
117 if ($this->getProvider($name) instanceof ProviderInterface) {
118 $this->default = $name;
119 }
120 }
121
122 /**
123 * {@inheritdoc}
124 */
125 public function getDefaultProvider()
126 {
127 return $this->getProvider($this->default);
128 }
129
130 /**
131 * {@inheritdoc}
132 */
133 public function removeProvider($name)
134 {
135 unset($this->providers[$name]);
136
137 return $this;
138 }
139
140 /**
141 * {@inheritdoc}
142 */
143 public function connect($name = null, $username = null, $password = null)
144 {
145 $provider = $name ? $this->getProvider($name) : $this->getDefaultProvider();
146
147 return $provider->connect($username, $password);
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function __call($method, $parameters)
154 {
155 $provider = $this->getDefaultProvider();
156
157 if (! $provider->getConnection()->isBound()) {
158 $provider->connect();
159 }
160
161 return call_user_func_array([$provider, $method], $parameters);
162 }
163
164 /**
165 * Initializes the event logger.
166 *
167 * @return void
168 */
169 public function initEventLogger()
170 {
171 $dispatcher = static::getEventDispatcher();
172
173 $logger = $this->newEventLogger();
174
175 // We will go through each of our event wildcards and register their listener.
176 foreach ($this->listen as $event) {
177 $dispatcher->listen($event, function ($eventName, $events) use ($logger) {
178 foreach ($events as $event) {
179 $logger->log($event);
180 }
181 });
182 }
183 }
184
185 /**
186 * Returns a new event logger instance.
187 *
188 * @return EventLogger
189 */
190 protected function newEventLogger()
191 {
192 return new EventLogger(static::getLogger());
193 }
194}