Passed
Push — develop ( 53a66f...07e04a )
by Daniel
05:35
created

ContentFactory::getLipsumContent()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.3931

Importance

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