1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
7
|
|
|
use kalanis\UploadPerPartes\Responses; |
8
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
9
|
|
|
use kalanis\UploadPerPartes\Uploader\Config; |
10
|
|
|
use kalanis\UploadPerPartes\Uploader\LangFactory; |
11
|
|
|
use kalanis\UploadPerPartes\UploadException; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Factory |
17
|
|
|
* @package kalanis\UploadPerPartes\Target |
18
|
|
|
* Responses from server to client |
19
|
|
|
*/ |
20
|
|
|
class Factory |
21
|
|
|
{ |
22
|
|
|
use TLang; |
23
|
|
|
|
24
|
|
|
protected LangFactory $langFactory; |
25
|
|
|
protected ?ContainerInterface $container; |
26
|
|
|
|
27
|
18 |
|
public function __construct(LangFactory $langFactory, ?ContainerInterface $container) |
28
|
|
|
{ |
29
|
18 |
|
$this->langFactory = $langFactory; |
30
|
18 |
|
$this->container = $container; |
31
|
18 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Config $config |
35
|
|
|
* @throws UploadException |
36
|
|
|
* @return Interfaces\IOperations |
37
|
|
|
*/ |
38
|
18 |
|
public function getTarget(Config $config): Interfaces\IOperations |
39
|
|
|
{ |
40
|
18 |
|
$this->setUppLang($this->langFactory->getLang($config)); |
41
|
|
|
|
42
|
18 |
|
if (is_object($config->target)) { |
43
|
14 |
|
return $this->checkObject($config->target); |
44
|
|
|
} |
45
|
4 |
|
if (is_string($config->target)) { |
46
|
|
|
// ok, now is that a path to a local storage or remote one? |
47
|
3 |
|
if (filter_var($config->target, FILTER_VALIDATE_URL)) { |
48
|
2 |
|
return $this->initRemote($config->target); |
49
|
|
|
} else { |
50
|
|
|
// in this case the target is locally available storage, probably the filesystem |
51
|
1 |
|
return new Local\Processing($config, $this->getUppLang()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
1 |
|
throw new UploadException($this->getUppLang()->uppTargetNotSet()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param object $variant |
59
|
|
|
* @throws UploadException |
60
|
|
|
* @return Interfaces\IOperations |
61
|
|
|
*/ |
62
|
14 |
|
protected function checkObject(object $variant): Interfaces\IOperations |
63
|
|
|
{ |
64
|
14 |
|
if ($variant instanceof Interfaces\IOperations) { |
65
|
13 |
|
return $variant; |
66
|
|
|
} |
67
|
1 |
|
throw new UploadException($this->getUppLang()->uppTargetIsWrong(get_class($variant))); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $url |
72
|
|
|
* @throws UploadException |
73
|
|
|
* @return Interfaces\IOperations |
74
|
|
|
*/ |
75
|
2 |
|
protected function initRemote(string $url): Interfaces\IOperations |
76
|
|
|
{ |
77
|
|
|
// now - have we PSR implementation or we shall use internals? |
78
|
|
|
if ( |
79
|
2 |
|
$this->container |
80
|
2 |
|
&& $this->container->has('\Psr\Http\Client\ClientInterface') |
81
|
2 |
|
&& $this->container->has('\Psr\Http\Message\RequestInterface') |
82
|
|
|
) { // MUST be as string! |
83
|
|
|
// use psr |
84
|
|
|
/** @var \Psr\Http\Client\ClientInterface $client */ |
85
|
1 |
|
$client = $this->container->get('\Psr\Http\Client\ClientInterface'); |
86
|
|
|
/** @var \Psr\Http\Message\RequestInterface $request */ |
87
|
1 |
|
$request = $this->container->get('\Psr\Http\Message\RequestInterface'); |
88
|
1 |
|
return new Remote\Psr( |
89
|
1 |
|
$client, |
90
|
1 |
|
new Remote\Psr\Request( |
91
|
1 |
|
$request, |
92
|
1 |
|
$this->remoteConfig($url) |
93
|
|
|
), |
94
|
1 |
|
new Remote\Psr\Response(new Responses\Factory($this->getUppLang()), $this->getUppLang()) |
95
|
|
|
); |
96
|
|
|
} else { |
97
|
|
|
// use internals |
98
|
1 |
|
return new Remote\Internals( |
99
|
1 |
|
new Remote\Internals\Client(), |
100
|
1 |
|
new Remote\Internals\Request( |
101
|
1 |
|
$this->remoteConfig($url), |
102
|
1 |
|
new Remote\Internals\RequestData() |
103
|
|
|
), |
104
|
1 |
|
new Remote\Internals\Response(new Responses\Factory($this->getUppLang()), $this->getUppLang()) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $url |
111
|
|
|
* @throws UploadException |
112
|
|
|
* @return Remote\Config |
113
|
|
|
*/ |
114
|
2 |
|
protected function remoteConfig(string $url): Remote\Config |
115
|
|
|
{ |
116
|
2 |
|
$conf = new Remote\Config(); |
117
|
2 |
|
$parsed = parse_url($url); |
118
|
2 |
|
if (false === $parsed) { |
119
|
|
|
// @codeCoverageIgnoreStart |
120
|
|
|
throw new UploadException($this->getUppLang()->uppTargetIsWrong($url)); |
121
|
|
|
} |
122
|
|
|
// @codeCoverageIgnoreEnd |
123
|
2 |
|
$parsed = (array) $parsed; |
124
|
2 |
|
$conf->targetHost = empty($parsed['host']) ? $conf->targetHost : strval($parsed['host']); |
125
|
2 |
|
$conf->targetPort = empty($parsed['port']) ? $conf->targetPort : intval($parsed['port']); |
126
|
2 |
|
$conf->pathPrefix = empty($parsed['path']) ? $conf->pathPrefix : strval($parsed['path']); |
127
|
2 |
|
return $conf; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|