Issues (10)

src/Workflow.php (1 issue)

1
<?php
2
3
namespace Jackal\Copycat;
4
5
use Jackal\Copycat\Converter\ConversionMap;
6
use Jackal\Copycat\Filter\FilterMap;
7
use Jackal\Copycat\Reader\ReaderInterface;
8
use Jackal\Copycat\Sorter\SorterMap;
9
use Jackal\Copycat\Writer\WriterInterface;
10
use RuntimeException;
11
12
/**
13
 * Class Workflow
14
 * @package Jackal\Copycat
15
 */
16
class Workflow
17
{
18
    /**
19
     * @var ReaderInterface
20
     */
21
    protected $reader;
22
23
    /**
24
     * @var WriterInterface[]
25
     */
26
    protected $writers = [];
27
28
    /**
29
     * @var ConversionMap
30
     */
31
    protected $conversionMap;
32
33
    /**
34
     * @var FilterMap
35
     */
36
    protected $filterMap;
37
38
    /**
39
     * @var SorterMap
40
     */
41
    protected $sorterMap;
42
43
    /**
44
     * Workflow constructor.
45
     * @param ReaderInterface $reader
46
     */
47
    public function __construct(ReaderInterface $reader)
48
    {
49
        $this->reader = $reader;
50
        $this->conversionMap = new ConversionMap();
51
        $this->filterMap = new FilterMap();
52
        $this->sorterMap = new SorterMap();
53
    }
54
55
    /**
56
     * @param WriterInterface $writer
57
     */
58
    public function addWriter(WriterInterface $writer)
59
    {
60
        $this->writers[] = $writer;
61
    }
62
63
    /**
64
     * @param callable $converter
65
     */
66
    public function addConverter(callable $converter)
67
    {
68
        $this->conversionMap->add($converter);
69
    }
70
71
    /**
72
     * @param callable $filter
73
     */
74
    public function addFilter(callable $filter)
75
    {
76
        $this->filterMap->add($filter);
77
    }
78
79
    /**
80
     * @param callable $sorter
81
     */
82
    public function addSorter(callable $sorter)
83
    {
84
        $this->sorterMap->add($sorter);
85
    }
86
87
    /**
88
     * @param callable|null $callback
89
     */
90
    public function process(callable $callback = null)
91
    {
92
        if (!$this->writers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->writers of type Jackal\Copycat\Writer\WriterInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            throw new RuntimeException('No writers set');
94
        }
95
96
        foreach ($this->writers as $writer) {
97
            $writer->prepare();
98
        }
99
100
        if ($this->sorterMap->hasSorter()) {
101
            $values = $this->reader->all();
102
            $this->sorterMap->apply($values);
103
        } else {
104
            $values = $this->reader;
105
        }
106
107
        foreach ($values as $k => $row) {
108
            foreach ($this->writers as $writer) {
109
                $copyRow = $row;
110
111
                if ($this->filterMap->apply($copyRow)) {
112
                    $this->conversionMap->apply($copyRow);
113
                    $writer->writeItem($copyRow);
114
                }
115
            }
116
117
            if ($callback) {
118
                if (isset($copyRow)) {
119
                    $callback($copyRow);
120
                }
121
            }
122
        }
123
124
        foreach ($this->writers as $writer) {
125
            $writer->finish();
126
        }
127
    }
128
}
129