1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Oryzone PHPoAuthUserData package <https://github.com/Oryzone/PHPoAuthUserData>. |
5
|
|
|
* |
6
|
|
|
* (c) Oryzone, developed by Luciano Mammino <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OAuth\UserData; |
13
|
|
|
|
14
|
|
|
use OAuth\Common\Service\ServiceInterface; |
15
|
|
|
use OAuth\UserData\Exception\InvalidExtractorException; |
16
|
|
|
use OAuth\UserData\Exception\UndefinedExtractorException; |
17
|
|
|
use OAuth\UserData\Extractor\ExtractorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ExtractorFactory |
21
|
|
|
* @package OAuth\UserData |
22
|
|
|
*/ |
23
|
|
|
class ExtractorFactory implements ExtractorFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array $extractorsMap |
27
|
|
|
*/ |
28
|
|
|
protected $extractorsMap; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param array $extractorsMap |
34
|
|
|
*/ |
35
|
|
|
public function __construct($extractorsMap = array()) |
36
|
|
|
{ |
37
|
|
|
$this->extractorsMap = $extractorsMap; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function get(ServiceInterface $service) |
44
|
|
|
{ |
45
|
|
|
// Check in extractors map |
46
|
|
|
$serviceFullyQualifiedClass = get_class($service); |
47
|
|
|
if (isset($this->extractorsMap[$serviceFullyQualifiedClass])) { |
48
|
|
|
$extractorsClass = $this->extractorsMap[$serviceFullyQualifiedClass]; |
49
|
|
|
} else { |
50
|
|
|
$extractorsClass = $this->searchExtractorClassInLib($serviceFullyQualifiedClass); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null === $extractorsClass) { |
54
|
|
|
throw new UndefinedExtractorException($service, array_keys($this->extractorsMap)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->buildExtractor($service, $extractorsClass); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Adds a new extractor to the extractorsMap |
62
|
|
|
* |
63
|
|
|
* @param string $serviceFullyQualifiedClass |
64
|
|
|
* @param string $extractorClass |
65
|
|
|
*/ |
66
|
|
|
public function addExtractorMapping($serviceFullyQualifiedClass, $extractorClass) |
67
|
|
|
{ |
68
|
|
|
$this->extractorsMap[$serviceFullyQualifiedClass] = $extractorClass; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Search a mapping on the fly by inspecting the library code |
73
|
|
|
* |
74
|
|
|
* @param string $serviceFullyQualifiedClass |
75
|
|
|
* @return null|string |
76
|
|
|
*/ |
77
|
|
|
protected function searchExtractorClassInLib($serviceFullyQualifiedClass) |
78
|
|
|
{ |
79
|
|
|
$parts = explode('\\', $serviceFullyQualifiedClass); |
80
|
|
|
$className = $parts[sizeof($parts) - 1]; |
81
|
|
|
|
82
|
|
|
$extractorClass = sprintf('\OAuth\UserData\Extractor\%s', $className); |
83
|
|
|
if (class_exists($extractorClass)) { |
84
|
|
|
return $extractorClass; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ServiceInterface $service |
92
|
|
|
* @param string $extractorClass |
93
|
|
|
* @return ExtractorInterface |
94
|
|
|
* @throws Exception\InvalidExtractorException |
95
|
|
|
*/ |
96
|
|
|
protected function buildExtractor(ServiceInterface $service, $extractorClass) |
97
|
|
|
{ |
98
|
|
|
$extractor = new $extractorClass; |
99
|
|
|
|
100
|
|
|
if (!$extractor instanceof ExtractorInterface) { |
101
|
|
|
throw new InvalidExtractorException($extractorClass); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$extractor->setService($service); |
105
|
|
|
|
106
|
|
|
return $extractor; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|