Completed
Push — master ( 64aeb7...576a4c )
by Dennis
01:45
created

SeedCollection::attach()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 7
nop 1
1
<?php
2
namespace Dennis\Seeder\Collection;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 Dennis Römmich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Dennis\Seeder\Seed;
29
use Dennis\Seeder\Seeder;
30
31
/**
32
 * SeedCollection
33
 *
34
 * @author Dennis Römmich<[email protected]>
35
 * @copyright Copyright belongs to the respective authors
36
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
37
 */
38
class SeedCollection implements \Dennis\Seeder\SeedCollection
39
{
40
    /**
41
     * seeds
42
     *
43
     * @var array $seeds
44
     */
45
    protected $seeds = [];
46
47
    /**
48
     * Used Keys
49
     *
50
     * @var array
51
     */
52
    protected $used = [];
53
54
    /**
55
     * @var int
56
     */
57
    protected $i = 0;
58
59
    /**
60
     * @var int
61
     */
62
    public $amount = 0;
63
64
    /**
65
     * @return array
66
     */
67
    public function toArray()
68
    {
69
        $return = [];
70
71
        /** @var Seed $seed */
72
        foreach ($this->seeds as $seeds) {
73
            foreach ($seeds as $key => $seed) {
74
                $return[$seed->getTarget()][$key] = $seed->getProperties();
75
            }
76
        }
77
78
        return $return;
79
    }
80
81
    /**
82
     * @param Seeder $seeder
83
     * @return array
84
     */
85
    public function get(Seeder $seeder)
86
    {
87
        $return = [];
88
        $i = 1;
89
        $this->used = [];
90
        foreach ($this->seeds as $title => $seeds) {
91
            foreach ($seeds as $key => $seed) {
92
                if ($title === get_class($seeder)) {
93
                    if ($this->isUsed($key) || $i > $this->amount) {
94
                        continue;
95
                    }
96
                    $i++;
97
                    $this->used[$key] = 1;
98
                    $return[$key] = $seed;
99
                }
100
            }
101
        }
102
103
        return $return;
104
    }
105
106
    /**
107
     * @param string $key
108
     * @return bool
109
     */
110
    public function isUsed($key)
111
    {
112
        return isset($this->used[$key]);
113
    }
114
115
    /**
116
     * each
117
     *
118
     * @param callable $function
119
     * @return $this
120
     */
121
    public function each(callable $function)
122
    {
123
        $reflection = new \ReflectionFunction($function);
124
        $className = get_class($reflection->getClosureThis());
125
        foreach ($this->seeds[$className] as $key => $seed) {
126
            if ($this->isUsed($key)) {
127
                continue;
128
            }
129
            $function($seed, \Dennis\Seeder\Factory\FakerFactory::createFaker());
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * @param Seed $seed
137
     * @throws \Exception
138
     */
139
    public function attach(Seed $seed)
140
    {
141
        $attached = false;
142
        if (!is_array($this->seeds[$seed->getTitle()])) {
143
            $this->seeds[$seed->getTitle()]['NEW' . ++$this->i] = $seed;
144
            return null;
145
        }
146
        foreach ($this->seeds[$seed->getTitle()] as $attachedSeed) {
147
            if ($attachedSeed === $seed) {
148
                $attached = true;
149
            }
150
        }
151
        if ($attached === false) {
152
            $this->seeds[$seed->getTitle()]['NEW' . ++$this->i] = $seed;
153
        }
154
    }
155
156
    /**
157
     * Returns a random seed. Returns null if there is no seed or there are less seeds than you want to create
158
     * In this case you may $this->attach a new Seed.
159
     *
160
     * @param string $className
161
     * @return Seed
162
     */
163
    public function random($className, $i)
164
    {
165
        if (!isset($this->seeds[$className]) || count($this->seeds[$className]) < $i) {
166
            return null;
167
        }
168
        $random = array_rand($this->seeds[$className]);
169
        return $this->seeds[$className][$random];
170
    }
171
172
    /**
173
     * detach
174
     *
175
     * @param string $key
176
     * @return void
177
     */
178
    public function detach($key)
179
    {
180
        if (isset($this->seeds[$key])) {
181
            unset($this->seeds[$key]);
182
        }
183
    }
184
185
    /**
186
     * Checks if current position is valid
187
     *
188
     * @return bool
189
     */
190
    public function valid()
191
    {
192
        return current($this->seeds) !== false;
193
    }
194
195
    /**
196
     * Return the current element
197
     *
198
     * @return Seed
199
     */
200
    public function current()
201
    {
202
        return current($this->seeds);
203
    }
204
205
    /**
206
     * Move forward to next element
207
     *
208
     * @return Seed
209
     */
210
    public function next()
211
    {
212
        return next($this->seeds);
213
    }
214
215
    /**
216
     * Return the key of the current element
217
     *
218
     * @return Seed
219
     */
220
    public function key()
221
    {
222
        return key($this->seeds);
223
    }
224
225
    /**
226
     * Rewind the Iterator to the first element
227
     *
228
     * @return Seed
229
     */
230
    public function rewind()
231
    {
232
        return reset($this->seeds);
233
    }
234
235
    /**
236
     * Count elements of an object
237
     *
238
     * @return int
239
     */
240
    public function count()
241
    {
242
        return count($this->seeds);
243
    }
244
245
    /**
246
     * @return void
247
     */
248
    public function clear()
249
    {
250
        $this->seeds = [];
251
    }
252
}
253