Passed
Push — develop ( 96281a...e6f82b )
by Daniel
05:53 queued 26s
created

ContentFactory::getIgnoreOps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Component\Content\Content;
9
use Silverback\ApiComponentBundle\Factory\Entity\Content\Component\AbstractComponentFactory;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
12
/**
13
 * @author Daniel West <[email protected]>
14
 */
15
final class ContentFactory extends AbstractComponentFactory
16
{
17
    /**
18
     * @var \GuzzleHttp\Client
19
     */
20
    private $client;
21
22
    public function __construct(
23
        ObjectManager $manager,
24
        ValidatorInterface $validator,
25
        Client $client
26
    ) {
27
        parent::__construct($manager, $validator);
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    private static function getGuzzleOps(): array
35
    {
36
        return [
37
            'connect_timeout' => 3,
38
            'read_timeout' => 2,
39
            'timeout' => 5
40
        ];
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    private function getLipsumContent(): string
47
    {
48
        $url = 'http://loripsum.net/api/' . implode('/', $this->ops['lipsum']);
49
        try {
50
            $res = $this->client->request(
51
                'GET',
52
                $url,
53
                self::getGuzzleOps()
54
            );
55
            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
    public function create(?array $ops = null): Content
72
    {
73
        $component = new Content();
74
        $this->init($component, $ops);
75
76
        if (\is_string($this->ops['content'])) {
77
            $component->setContent($this->ops['content']);
78
        } else {
79
            $component->setContent($this->getLipsumContent());
80
        }
81
        $this->validate($component);
82
83
        return $component;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public static function defaultOps(): array
90
    {
91
        return array_merge(
92
            parent::defaultOps(),
93
            [
94
                'lipsum' => ['5', 'medium', 'headers', 'code', 'decorate', 'link', 'bq', 'ul', 'ol'],
95
                'content' => null
96
            ]
97
        );
98
    }
99
100
    /**
101
     *
102
     */
103
    protected static function getIgnoreOps(): array
104
    {
105
        return array_merge(parent::getIgnoreOps(), ['lipsum', 'content']);
106
    }
107
}
108