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
|
3 |
|
static public function create( |
42
|
|
|
array $loggerConfig = [ |
43
|
|
|
'builder' => 'NullLogger', |
44
|
|
|
'config' => [], |
45
|
|
|
], |
46
|
|
|
array $clientConfig = [ |
47
|
|
|
'builder' => 'GuzzleClient', |
48
|
|
|
'config' => [], |
49
|
|
|
] |
50
|
|
|
|
51
|
|
|
) |
52
|
|
|
{ |
53
|
3 |
|
$factory = new static(); |
54
|
|
|
|
55
|
3 |
|
$factory->setClientBuilder( |
56
|
3 |
|
$factory->getBuilder($clientConfig['builder'], $factory->extractConfig($clientConfig))) |
57
|
3 |
|
->setLoggerBuilder( |
58
|
3 |
|
$factory->getBuilder($loggerConfig['builder'],$factory->extractConfig($loggerConfig))); |
59
|
|
|
|
60
|
|
|
|
61
|
3 |
|
return $factory; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $builderConfig |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
5 |
|
public function extractConfig(array $builderConfig) |
69
|
|
|
{ |
70
|
5 |
|
return isset($builderConfig['config']) ? $builderConfig['config']:[]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \FeedIo\FeedIo |
75
|
|
|
*/ |
76
|
2 |
|
public function getFeedIo() |
77
|
|
|
{ |
78
|
2 |
|
return new FeedIo( |
79
|
2 |
|
$this->clientBuilder->getClient(), |
80
|
2 |
|
$this->loggerBuilder->getLogger() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
public function getBuilder($builder, array $args = []) |
85
|
|
|
{ |
86
|
5 |
|
$class = "\\FeedIo\\Factory\\Builder\\{$builder}Builder"; |
87
|
|
|
|
88
|
5 |
|
if ( ! class_exists($class) ) { |
89
|
1 |
|
$class = $builder; |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
$reflection = new \ReflectionClass($class); |
93
|
|
|
|
94
|
5 |
|
return $reflection->newInstanceArgs($args); |
95
|
|
|
} |
96
|
|
|
|
97
|
5 |
|
public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder) |
98
|
|
|
{ |
99
|
5 |
|
$this->loggerBuilder = $loggerBuilder; |
100
|
|
|
|
101
|
5 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
public function setClientBuilder(ClientBuilderInterface $clientBuilder) |
105
|
|
|
{ |
106
|
5 |
|
$this->clientBuilder = $clientBuilder; |
107
|
|
|
|
108
|
5 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param BuilderInterface $builder |
113
|
|
|
* @return boolean true if the dependency is met |
114
|
|
|
*/ |
115
|
2 |
|
public function checkDependency(BuilderInterface $builder) |
116
|
|
|
{ |
117
|
2 |
|
if ( ! class_exists($builder->getMainClassName()) ) { |
118
|
1 |
|
$message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}"; |
119
|
1 |
|
throw new MissingDependencyException($message); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return true; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|