Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

ContentFactory::defaultOps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Factory\Fixtures\Component;
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\Entity\Content\AbstractContent;
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
    {
28
        parent::__construct($manager, $validator);
29
        $this->client = $client;
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    private static function getGuzzleOps(): array
36
    {
37
        return [
38
            'connect_timeout' => 3,
39
            'read_timeout' => 2,
40
            'timeout' => 5
41
        ];
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    private function getLipsumContent(): string
48
    {
49
        $url = 'http://loripsum.net/api/' . implode('/', $this->ops['lipsum']);
50
        try {
51
            $res = $this->client->request(
52
                'GET',
53
                $url,
54
                self::getGuzzleOps()
55
            );
56
            return (string) $res->getBody();
57
        } catch (RequestException $e) {
58
            return vsprintf(
59
                '<p><b>Request Exception</b>: %s<br/><small><a href="%s">%s</a></small></p>',
60
                [
61
                    $e->getMessage(),
62
                    $url,
63
                    $url
64
                ]
65
            );
66
        }
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function create(?array $ops = null, ?AbstractContent $owner = null): Content
73
    {
74
        $component = new Content();
75
        $this->init($component, $ops);
76
77
        if (\is_string($this->ops['content'])) {
78
            $component->setContent($this->ops['content']);
79
        } else {
80
            $component->setContent($this->getLipsumContent());
81
        }
82
        $this->validate($component);
83
84
        return $component;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public static function defaultOps(): array
91
    {
92
        return array_merge(
93
            parent::defaultOps(),
94
            [
95
                'lipsum' => ['5', 'medium', 'headers', 'code', 'decorate', 'link', 'bq', 'ul', 'ol'],
96
                'content' => null
97
            ]
98
        );
99
    }
100
}
101