|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory\Entity\Content\Component\Content; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\Content\Content; |
|
9
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\AbstractFactory; |
|
10
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Daniel West <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
final class ContentFactory extends AbstractFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \GuzzleHttp\Client |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
4 |
|
public function __construct( |
|
23
|
|
|
ObjectManager $manager, |
|
24
|
|
|
ValidatorInterface $validator, |
|
25
|
|
|
Client $client |
|
26
|
|
|
) { |
|
27
|
4 |
|
parent::__construct($manager, $validator); |
|
28
|
4 |
|
$this->client = $client; |
|
29
|
4 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
1 |
|
private static function getGuzzleOps(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
1 |
|
'connect_timeout' => 3, |
|
38
|
|
|
'read_timeout' => 2, |
|
39
|
|
|
'timeout' => 5 |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
1 |
|
private function getLipsumContent(): string |
|
47
|
|
|
{ |
|
48
|
1 |
|
$url = 'http://loripsum.net/api/' . implode('/', $this->ops['lipsum']); |
|
49
|
|
|
try { |
|
50
|
1 |
|
$res = $this->client->request( |
|
51
|
1 |
|
'GET', |
|
52
|
1 |
|
$url, |
|
53
|
1 |
|
self::getGuzzleOps() |
|
54
|
|
|
); |
|
55
|
1 |
|
return (string) $res->getBody(); |
|
56
|
|
|
} catch (RequestException $e) { |
|
57
|
|
|
return vsprintf( |
|
58
|
|
|
'<p><b>Request Exception</b>: %s<br/><small><a href="%s">%s</a></small></p>', |
|
59
|
|
|
[ |
|
60
|
|
|
$e->getMessage(), |
|
61
|
|
|
$url, |
|
62
|
|
|
$url |
|
63
|
|
|
] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function create(?array $ops = null): Content |
|
72
|
|
|
{ |
|
73
|
3 |
|
$component = new Content(); |
|
74
|
3 |
|
$this->init($component, $ops, ['lipsum', 'content']); |
|
75
|
|
|
|
|
76
|
3 |
|
if (\is_string($this->ops['content'])) { |
|
77
|
2 |
|
$component->setContent($this->ops['content']); |
|
78
|
|
|
} else { |
|
79
|
1 |
|
$component->setContent($this->getLipsumContent()); |
|
80
|
|
|
} |
|
81
|
3 |
|
$this->validate($component); |
|
82
|
|
|
|
|
83
|
3 |
|
return $component; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public static function defaultOps(): array |
|
90
|
|
|
{ |
|
91
|
4 |
|
return array_merge( |
|
92
|
4 |
|
AbstractFactory::COMPONENT_CLASSES, |
|
93
|
|
|
[ |
|
94
|
4 |
|
'lipsum' => ['5', 'medium', 'headers', 'code', 'decorate', 'link', 'bq', 'ul', 'ol'], |
|
95
|
|
|
'content' => null |
|
96
|
|
|
] |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|