Completed
Pull Request — master (#1)
by Fabrice
06:03 queued 02:44
created

CallableExtractor::extract()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Extractors;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\Nodes\PayloadNodeAbstract;
14
15
/**
16
 * Class ExtractorAbstract
17
 */
18
class CallableExtractor extends PayloadNodeAbstract implements ExtractorInterface
19
{
20
    /**
21
     * The record collection
22
     *
23
     * @var \Traversable
24
     */
25
    protected $extracted;
26
27
    /**
28
     * @var array
29
     */
30
    protected $nodeIncrements = [
31
        'num_records' => 'num_iterate',
32
        'num_extract' => 0,
33
    ];
34
35
    /**
36
     * CallableExtractorAbstract constructor.
37
     *
38
     * @param callable $payload
39
     * @param bool     $isAReturningVal
40
     *
41
     * @throws NodalFlowException
42
     */
43
    public function __construct(callable $payload, $isAReturningVal = true)
44
    {
45
        parent::__construct($payload, $isAReturningVal, true);
46
    }
47
48
    /**
49
     * This method is vaguely similar to a valid() meta iterator
50
     * It will triggers the record collection extraction when called
51
     * and return true when records where fetched.
52
     *
53
     * This is useful when batch extracting. If your extractor
54
     * does not perform batch extract (for example if you are
55
     * just reading a file line by line), just make so this method
56
     * triggers file open and return true in case of success and false
57
     * when called again.
58
     *
59
     * @param mixed|null $param
60
     *
61
     * @return bool false in case no more records can be fetched
62
     */
63
    public function extract($param = null)
64
    {
65
        $this->extracted = \call_user_func($this->payload, $param);
0 ignored issues
show
Bug introduced by
It seems like $this->payload can also be of type object; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

65
        $this->extracted = \call_user_func(/** @scrutinizer ignore-type */ $this->payload, $param);
Loading history...
66
67
        if (!is_array($this->extracted) && !($this->extracted instanceof \Traversable)) {
68
            return false;
69
        }
70
71
        return true;
72
    }
73
74
    /**
75
     * get the traversable to traverse within the Flow
76
     *
77
     * @param mixed $param
78
     *
79
     * @return \Generator
80
     */
81
    public function getTraversable($param)
82
    {
83
        $this->extract($param);
84
        $this->getCarrier()->getFlowMap()->incrementNode($this->getId(), 'num_extract');
85
        foreach ($this->extracted as $record) {
86
            yield $record;
87
        }
88
    }
89
}
90