Callback::getData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyDictionary\DataProvider;
6
7
use EasyDictionary\Interfaces\DataProviderFilterInterface;
8
use EasyDictionary\Interfaces\DataProviderInterface;
9
10
/**
11
 * Class Callback
12
 * @package EasyDictionary\DataProvider
13
 */
14
class Callback implements DataProviderInterface
15
{
16
    protected $callback = null;
17
    protected $arguments = [];
18
19
    /**
20
     * @param array $datProviderConfig
21
     */
22 2
    public function __construct($datProviderConfig = [])
23
    {
24 2
        $this->setCallback(
25 2
            $datProviderConfig['callable'] ?? null,
26 2
            $datProviderConfig['callableArgs'] ?? []
27
        );
28 2
    }
29
30
    /**
31
     * @param callable|null $callback
32
     * @param array $arguments
33
     *
34
     * @return $this
35
     */
36 2
    public function setCallback(callable $callback = null, array $arguments = []): DataProviderInterface
37
    {
38 2
        $this->callback = $callback;
39 2
        $this->arguments = $arguments;
40
41 2
        return $this;
42
    }
43
44
    /**
45
     * @param DataProviderFilterInterface|null $filter
46
     * @return iterable
47
     */
48 2
    public function getData(DataProviderFilterInterface $filter = null): iterable
49
    {
50 2
        return is_callable($this->callback)
51 1
            ? call_user_func_array($this->callback, $this->arguments)
52 2
            : [];
53
    }
54
}
55