Passed
Push — develop ( 83a19a...5dcd0c )
by Daniel
05:43
created

ContentFactory::getIgnoreOps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Content\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 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']);
0 ignored issues
show
Unused Code introduced by
The call to Silverback\ApiComponentB...omponentFactory::init() has too many arguments starting with array('lipsum', 'content'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $this->/** @scrutinizer ignore-call */ 
75
               init($component, $ops, ['lipsum', 'content']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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
            parent::defaultOps(),
93
            [
94 4
                'lipsum' => ['5', 'medium', 'headers', 'code', 'decorate', 'link', 'bq', 'ul', 'ol'],
95
                'content' => null
96
            ]
97
        );
98
    }
99
100
    /**
101
     *
102
     */
103 3
    protected static function getIgnoreOps(): array
104
    {
105 3
        return array_merge(parent::getIgnoreOps(), ['lipsum']);
106
    }
107
}
108