1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo; |
12
|
|
|
|
13
|
|
|
use FeedIo\Factory\MissingDependencyException; |
14
|
|
|
use FeedIo\Factory\LoggerBuilderInterface; |
15
|
|
|
use FeedIo\Factory\ClientBuilderInterface; |
16
|
|
|
use FeedIo\Factory\BuilderInterface; |
17
|
|
|
|
18
|
|
|
class Factory |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Psr\Log\LoggerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \FeedIo\Adapter\ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \FeedIo\Factory\ClientBuilderInterface |
33
|
|
|
*/ |
34
|
|
|
protected $clientBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \FeedIo\Factory\LoggerBuilderInterface |
38
|
|
|
*/ |
39
|
|
|
protected $loggerBuilder; |
40
|
|
|
|
41
|
4 |
|
public static function create( |
42
|
|
|
array $loggerConfig = [ |
43
|
|
|
'builder' => 'NullLogger', |
44
|
|
|
'config' => [], |
45
|
|
|
], |
46
|
|
|
array $clientConfig = [ |
47
|
|
|
'builder' => 'GuzzleClient', |
48
|
|
|
'config' => [], |
49
|
|
|
] |
50
|
|
|
|
51
|
|
|
) { |
52
|
4 |
|
$factory = new static(); |
53
|
|
|
|
54
|
4 |
|
$factory->setClientBuilder( |
55
|
4 |
|
$factory->getBuilder($clientConfig['builder'], $factory->extractConfig($clientConfig)) |
56
|
4 |
|
) |
57
|
4 |
|
->setLoggerBuilder( |
58
|
4 |
|
$factory->getBuilder($loggerConfig['builder'], $factory->extractConfig($loggerConfig)) |
59
|
4 |
|
); |
60
|
|
|
|
61
|
|
|
|
62
|
4 |
|
return $factory; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function setClientBuilder(ClientBuilderInterface $clientBuilder) |
66
|
|
|
{ |
67
|
6 |
|
$this->clientBuilder = $clientBuilder; |
68
|
|
|
|
69
|
6 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $builder |
74
|
|
|
* @param array $args |
75
|
|
|
* |
76
|
|
|
* @return object |
77
|
|
|
*/ |
78
|
6 |
|
public function getBuilder($builder, array $args = []) |
79
|
|
|
{ |
80
|
6 |
|
$class = "\\FeedIo\\Factory\\Builder\\{$builder}Builder"; |
81
|
|
|
|
82
|
6 |
|
if (!class_exists($class)) { |
83
|
1 |
|
$class = $builder; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
6 |
|
$reflection = new \ReflectionClass($class); |
87
|
|
|
|
88
|
|
|
// Pass args only if constructor has |
89
|
6 |
|
return $reflection->newInstanceArgs([$args]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $builderConfig |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
6 |
|
public function extractConfig(array $builderConfig) |
98
|
|
|
{ |
99
|
6 |
|
return isset($builderConfig['config']) ? $builderConfig['config'] : []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \FeedIo\FeedIo |
104
|
|
|
*/ |
105
|
3 |
|
public function getFeedIo() |
106
|
1 |
|
{ |
107
|
3 |
|
return new FeedIo( |
108
|
3 |
|
$this->clientBuilder->getClient(), |
109
|
3 |
|
$this->loggerBuilder->getLogger() |
110
|
3 |
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param LoggerBuilderInterface $loggerBuilder |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
6 |
|
public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder) |
119
|
|
|
{ |
120
|
6 |
|
$this->loggerBuilder = $loggerBuilder; |
121
|
|
|
|
122
|
6 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param BuilderInterface $builder |
127
|
|
|
* |
128
|
|
|
* @return boolean true if the dependency is met |
129
|
|
|
*/ |
130
|
2 |
|
public function checkDependency(BuilderInterface $builder) |
131
|
|
|
{ |
132
|
2 |
|
if (!class_exists($builder->getMainClassName())) { |
133
|
1 |
|
$message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}"; |
134
|
1 |
|
throw new MissingDependencyException($message); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|