SeedCollection::get()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.3554
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
namespace TildBJ\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 TildBJ\Seeder\Seed;
29
use TildBJ\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 \TildBJ\Seeder\SeedCollection
39
{
40
    /**
41
     * seeds
42
     *
43
     * @var array $seeds
44
     */
45
    protected $seeds = [];
46
47
    /**
48
     * @var array $temp
49
     */
50
    protected $temp = [];
51
52
    /**
53
     * @var int
54
     */
55
    protected $i = 0;
56
57
    /**
58
     * @return array
59
     */
60 9
    public function toArray()
61
    {
62 9
        $return = [];
63
64
        /** @var Seed $seed */
65 9
        foreach ($this->seeds as $seeds) {
66 8
            foreach ($seeds as $key => $seed) {
67 8
                $return[$seed->getTarget()][$key] = $seed->getProperties();
68
            }
69
        }
70
71 9
        return $return;
72
    }
73
74
    /**
75
     * @param Seeder $seeder
76
     * @return array
77
     */
78 10
    public function get(Seeder $seeder)
79
    {
80 10
        if (isset($this->temp[$seeder->getClass()])) {
81 10
            $return = $this->temp[$seeder->getClass()];
82 10
            unset($this->temp[$seeder->getClass()]);
83 10
            return $return;
84
        }
85 6
        $return = [];
86 6
        foreach ($this->seeds as $title => $seeds) {
87 6
            foreach ($seeds as $key => $seed) {
88 6
                if ($title === $seeder->getClass()) {
89 6
                    $return[$key] = $seed;
90
                }
91
            }
92
        }
93
94 6
        return $return;
95
    }
96
97
    /**
98
     * each
99
     *
100
     * @param callable $function
101
     * @return $this
102
     */
103 7
    public function each(callable $function)
104
    {
105 7
        $reflection = new \ReflectionFunction($function);
106 7
        $className = get_class($reflection->getClosureThis());
107 7
        foreach ($this->seeds[$className] as $key => $seed) {
108 7
            if ($seed->isExecuted()) {
109 7
                continue;
110
            }
111 7
            $seed->isExecuted(true);
112 7
            $function($seed, \TildBJ\Seeder\Factory\FakerFactory::createFaker());
113
        }
114
115 7
        return $this;
116
    }
117
118
    /**
119
     * @param Seed $seed
120
     * @throws \Exception
121
     */
122 15
    public function attach(Seed $seed)
123
    {
124 15
        $identifier = 'NEW' . ++$this->i;
125 15
        $this->seeds[$seed->getTitle()][$identifier] = $seed;
126 15
        $this->temp[$seed->getTitle()][$identifier] = $identifier;
127 15
    }
128
129
    /**
130
     * Returns a random seed. Returns null if there is no seed or there are less seeds than you want to create
131
     * In this case you may $this->attach a new Seed.
132
     *
133
     * @param string $className
134
     * @return Seed
135
     */
136
    public function random($className, $i)
137
    {
138
        if (!isset($this->seeds[$className]) || count($this->seeds[$className]) < $i) {
139
            return null;
140
        }
141
        $random = array_rand($this->seeds[$className]);
142
        return $this->seeds[$className][$random];
143
    }
144
145
    /**
146
     * detach
147
     *
148
     * @param string $key
149
     * @return void
150
     */
151
    public function detach($key)
152
    {
153
        if (isset($this->seeds[$key])) {
154
            unset($this->seeds[$key]);
155
        }
156
    }
157
158
    /**
159
     * Checks if current position is valid
160
     *
161
     * @return bool
162
     */
163 1
    public function valid()
164
    {
165 1
        return current($this->seeds) !== false;
166
    }
167
168
    /**
169
     * Return the current element
170
     *
171
     * @return Seed
172
     */
173 4
    public function current()
174
    {
175 4
        return current($this->seeds);
176
    }
177
178
    /**
179
     * Move forward to next element
180
     *
181
     * @return Seed
182
     */
183 1
    public function next()
184
    {
185 1
        return next($this->seeds);
186
    }
187
188
    /**
189
     * Return the key of the current element
190
     *
191
     * @return Seed
192
     */
193 1
    public function key()
194
    {
195 1
        return key($this->seeds);
196
    }
197
198
    /**
199
     * Rewind the Iterator to the first element
200
     *
201
     * @return Seed
202
     */
203 1
    public function rewind()
204
    {
205 1
        return reset($this->seeds);
206
    }
207
208
    /**
209
     * Count elements of an object
210
     *
211
     * @return int
212
     */
213 1
    public function count()
214
    {
215 1
        return count($this->seeds);
216
    }
217
218
    /**
219
     * @param string $name
220
     * @return int
221
     */
222 6
    public function countByName($name)
223
    {
224 6
        if (!is_array($this->seeds[$name])) {
225 6
            return 0;
226
        }
227 6
        return count($this->seeds[$name]);
228
    }
229
230
    /**
231
     * @return void
232
     */
233 15
    public function clear()
234
    {
235 15
        $this->seeds = [];
236 15
        $this->i = 0;
237 15
    }
238
}
239