Passed
Pull Request — master (#1)
by Fabrice
03:00
created

CallableExtractor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 76
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTraversable() 0 6 2
A __construct() 0 3 1
A extract() 0 9 3
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 underlying executable or traversable Payload
22
     *
23
     * @var callable
24
     */
25
    protected $payload;
26
27
    /**
28
     * The record collection
29
     *
30
     * @var \Traversable
31
     */
32
    protected $extracted;
33
34
    /**
35
     * @var array
36
     */
37
    protected $nodeIncrements = [
38
        'num_records' => 'num_iterate',
39
        'num_extract' => 0,
40
    ];
41
42
    /**
43
     * CallableExtractorAbstract constructor.
44
     *
45
     * @param callable $payload
46
     * @param bool     $isAReturningVal
47
     *
48
     * @throws NodalFlowException
49
     */
50
    public function __construct(callable $payload, $isAReturningVal = true)
51
    {
52
        parent::__construct($payload, $isAReturningVal, true);
53
    }
54
55
    /**
56
     * This method is vaguely similar to a valid() meta iterator
57
     * It will triggers the record collection extraction when called
58
     * and return true when records where fetched.
59
     *
60
     * This is useful when batch extracting. If your extractor
61
     * does not perform batch extract (for example if you are
62
     * just reading a file line by line), just make so this method
63
     * triggers file open and return true in case of success and false
64
     * when called again.
65
     *
66
     * @param mixed|null $param
67
     *
68
     * @return bool false in case no more records can be fetched
69
     */
70
    public function extract($param = null)
71
    {
72
        $this->extracted = \call_user_func($this->payload, $param);
73
74
        if (!is_array($this->extracted) && !($this->extracted instanceof \Traversable)) {
75
            return false;
76
        }
77
78
        return true;
79
    }
80
81
    /**
82
     * get the traversable to traverse within the Flow
83
     *
84
     * @param mixed $param
85
     *
86
     * @return \Generator
87
     */
88
    public function getTraversable($param)
89
    {
90
        $this->extract($param);
91
        $this->getCarrier()->getFlowMap()->incrementNode($this->getId(), 'num_extract');
92
        foreach ($this->extracted as $record) {
93
            yield $record;
94
        }
95
    }
96
}
97