|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ExternalFeedParser; |
|
4
|
|
|
|
|
5
|
|
|
use ExternalFeedParser\Contracts\Converter; |
|
6
|
|
|
use ExternalFeedParser\Contracts\Provider; |
|
7
|
|
|
use ExternalFeedParser\Contracts\PullProcessor; |
|
8
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
class FeedParseManager |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Resolved providers. |
|
17
|
|
|
*/ |
|
18
|
|
|
protected array $providers = []; |
|
19
|
|
|
|
|
20
|
6 |
|
public function __construct( |
|
21
|
|
|
protected Application $app |
|
22
|
|
|
) { |
|
23
|
6 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get a provider instance by name. |
|
27
|
|
|
*/ |
|
28
|
6 |
|
public function provider(string $name): Provider |
|
29
|
|
|
{ |
|
30
|
6 |
|
return $this->providers[$name] = $this->get($name); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Attempt to get the provider from the local cache. |
|
35
|
|
|
*/ |
|
36
|
6 |
|
protected function get(string $name): Provider |
|
37
|
|
|
{ |
|
38
|
6 |
|
return $this->providers[$name] ?? $this->resolve($name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Resolve the given provider. |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
6 |
|
protected function resolve(string $name): Provider |
|
47
|
|
|
{ |
|
48
|
6 |
|
$config = $this->getConfig($name); |
|
49
|
|
|
|
|
50
|
6 |
|
if (is_null($config)) { |
|
51
|
1 |
|
throw new InvalidArgumentException("Feed parser provider [{$name}] is not defined."); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
$class = $this->app['config']['external-feed-parser.provider_class']; |
|
55
|
|
|
|
|
56
|
5 |
|
if (!is_a($class, Provider::class, true)) { |
|
57
|
1 |
|
throw new InvalidArgumentException("Feed provider has wrong class [{$class}]."); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$pullClass = Arr::get($config, 'pull.class'); |
|
61
|
4 |
|
$pullArgs = Arr::get($config, 'pull.options', []); |
|
62
|
|
|
|
|
63
|
4 |
|
if (!is_a($pullClass, PullProcessor::class, true)) { |
|
64
|
1 |
|
throw new InvalidArgumentException("Pull processor has wrong class [{$pullClass}]."); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
$convertClass = Arr::get($config, 'convert.class'); |
|
68
|
3 |
|
$convertArgs = Arr::get($config, 'convert.options', []); |
|
69
|
|
|
|
|
70
|
3 |
|
if (!is_a($convertClass, Converter::class, true)) { |
|
71
|
1 |
|
throw new InvalidArgumentException("Converter has wrong class [{$convertClass}]."); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @psalm-suppress UndefinedClass */ |
|
75
|
2 |
|
return new $class( |
|
76
|
|
|
/** @psalm-suppress UndefinedClass */ |
|
77
|
2 |
|
new $pullClass(...$pullArgs), |
|
78
|
|
|
/** @psalm-suppress UndefinedClass */ |
|
79
|
2 |
|
new $convertClass(...$convertArgs), |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the provider configuration. |
|
85
|
|
|
*/ |
|
86
|
6 |
|
protected function getConfig(string $name): ?array |
|
87
|
|
|
{ |
|
88
|
6 |
|
return $this->app['config']["services.jobs-feeds.{$name}"] |
|
89
|
6 |
|
?? $this->app['config']["external-feed-parser.jobs-feeds.{$name}"]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|