1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\Utils\ClientCredentials; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials; |
13
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CredentialsMapper |
17
|
|
|
* @package Zbox\UnifiedPush\Utils\ClientCredentials |
18
|
|
|
*/ |
19
|
|
|
class CredentialsMapper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param CredentialsInterface $credentials |
23
|
|
|
* @param array $params |
24
|
|
|
* @return CredentialsInterface |
25
|
|
|
*/ |
26
|
|
|
public function mapCredentials(CredentialsInterface $credentials, array $params) |
27
|
|
|
{ |
28
|
|
|
if (empty($params)) { |
29
|
|
|
return new NullCredentials(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$attributes = $this->getCredentialAttributes($credentials); |
33
|
|
|
|
34
|
|
|
$this->validateAttributes($attributes, array_keys($params)); |
35
|
|
|
|
36
|
|
|
foreach ($attributes as $attribute) { |
37
|
|
|
$setterName = sprintf('set%s', ucfirst($attribute)); |
38
|
|
|
$credentials->$setterName($params[$attribute]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $credentials; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param CredentialsInterface $credentials |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
protected function getCredentialAttributes(CredentialsInterface $credentials) |
49
|
|
|
{ |
50
|
|
|
$reflection = new \ReflectionObject($credentials); |
51
|
|
|
$properties = $reflection->getProperties(\ReflectionProperty::IS_PRIVATE); |
52
|
|
|
|
53
|
|
|
$attributesList = array(); |
54
|
|
|
|
55
|
|
|
foreach ($properties as $property) { |
56
|
|
|
$attributesList[] = $property->getName(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $attributesList; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $required |
64
|
|
|
* @param array $acquired |
65
|
|
|
*/ |
66
|
|
|
protected function validateAttributes($required, $acquired) |
67
|
|
|
{ |
68
|
|
|
$missingAttributes = array_diff($required, $acquired); |
69
|
|
|
|
70
|
|
|
if (!empty($missingAttributes)) { |
71
|
|
|
throw new InvalidArgumentException( |
72
|
|
|
sprintf( |
73
|
|
|
'Mandatory attribute missing: %s', |
74
|
|
|
implode(', ', $missingAttributes) |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|