Completed
Pull Request — master (#2)
by Pol
04:12
created

Partitioner::export()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpartition;
6
7
use drupol\phpartition\Contract\PartitionerInterface;
8
use drupol\phpartition\Contract\Weightable;
9
use drupol\phpartition\Partition\Partition;
10
use drupol\phpartition\Partition\PartitionItemFactory;
11
use drupol\phpartition\Partitions\Partitions;
12
use drupol\phpartition\Partitions\PartitionsFactory;
13
14
/**
15
 * Class Partitioner.
16
 */
17
abstract class Partitioner implements PartitionerInterface
18
{
19
    /**
20
     * @var iterable
21
     */
22
    private $dataset;
23
24
    /**
25
     * @var \drupol\phpartition\Partitions\Partitions
26
     */
27
    private $lastRun;
28
29
    /**
30
     * @var PartitionItemFactory
31
     */
32
    private $partitionItemFactory;
33
34
    /**
35
     * @var PartitionsFactory
36
     */
37
    private $partitionsFactory;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    final public function export(int $chunks = 1)
43
    {
44
        return \array_map(
45
            static function (Partition $partition) {
46
                return \array_values($partition->exportArrayCopy());
47
            },
48
            $this
49
                ->run($chunks)
50
                ->getLastRun()
51
                ->partitions()
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getDataset()
59
    {
60
        return $this->dataset;
61
    }
62
63
    /**
64
     * @return \drupol\phpartition\Partition\Partition
65
     */
66
    final public function getDatasetPartition()
67
    {
68
        $partition = new Partition();
69
70
        foreach ($this->getDataset() as $data) {
71
            $partition->append($this->toPartitionItem($data));
72
        }
73
74
        return $partition;
75
    }
76
77
    /**
78
     * @return \drupol\phpartition\Partitions\Partitions
79
     */
80
    final public function getLastRun()
81
    {
82
        return $this->lastRun;
83
    }
84
85
    /**
86
     * @return \drupol\phpartition\Partition\PartitionItemFactory
87
     */
88
    public function getPartitionItemFactory()
89
    {
90
        return $this->partitionItemFactory ?? new PartitionItemFactory();
91
    }
92
93
    /**
94
     * @return \drupol\phpartition\Partitions\PartitionsFactory
95
     */
96
    public function getPartitionsFactory()
97
    {
98
        return $this->partitionsFactory ?? new PartitionsFactory();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    final public function run(int $chunks = 1)
105
    {
106
        $partitions = $this->getPartitionsFactory()::create($chunks);
107
108
        $dataPartition = $this->getDatasetPartition();
109
110
        $this->fillPartitions($partitions, $dataPartition, $chunks);
111
112
        $this->lastRun = $partitions;
113
114
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    final public function setDataset(iterable $dataset)
121
    {
122
        $this->dataset = $dataset;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param mixed $originalItem
129
     *
130
     * @return \drupol\phpartition\Contract\Weightable
131
     */
132
    public function toPartitionItem($originalItem): Weightable
133
    {
134
        return $this->getPartitionItemFactory()::create($originalItem);
135
    }
136
137
    /**
138
     * @param \drupol\phpartition\Partitions\Partitions $partitions
139
     * @param Partition $dataset
140
     * @param int $chunks
141
     */
142
    abstract protected function fillPartitions(Partitions $partitions, Partition $dataset, int $chunks): void;
143
}
144