Completed
Push — master ( 576a4c...bc864b )
by Dennis
01:49
created

SeederFactory::makeOrCreate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 3
1
<?php
2
namespace Dennis\Seeder\Factory;
3
4
/***************************************************************
5
 *
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 Dennis Römmich <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use Dennis\Seeder;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Class SeederFactory
33
 *
34
 * @package Dennis\Seeder\Factory\SeederFactory
35
 */
36
class SeederFactory implements \Dennis\Seeder\SeederFactory, \TYPO3\CMS\Core\SingletonInterface
37
{
38
    /**
39
     * @var Seeder\Faker
40
     */
41
    protected $faker;
42
43
    public function __construct(Seeder\Faker $faker)
44
    {
45
        $this->faker = $faker;
46
    }
47
48
    /**
49
     * Creates a new SeedCollection
50
     *
51
     * @param string $name
52
     * @param int $limit
53
     * @return Seeder\SeedCollection
54
     */
55
    public function create($name, $limit = 1)
56
    {
57
        return $this->makeOrCreate($name, $limit);
58
    }
59
60
    /**
61
     * @param $name
62
     * @param int $limit
63
     * @return Seeder\SeedCollection|mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Seeder\SeedCollection.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
64
     */
65
    public function make($name, $limit = 1)
66
    {
67
        return $this->makeOrCreate($name, $limit, false);
68
    }
69
70
    /**
71
     * @param $name
72
     * @param int $limit
73
     * @param bool $create
74
     */
75
    protected function makeOrCreate($name, $limit = 1, $create = true)
76
    {
77
        /** @var Seeder\Provider\TableConfiguration $tableConfiguration */
78
        $tableConfiguration = GeneralUtility::makeInstance(Seeder\Provider\TableConfiguration::class, $name);
79
        /** @var Seeder\SeedCollection $seedCollection */
80
        $seedCollection = GeneralUtility::makeInstance(Seeder\Collection\SeedCollection::class);
81
82
        for ($i = 1; $i <= $limit; $i++) {
83
            $calledClass = debug_backtrace(false, 3)[2];
84
            if ($create || !$seed = $seedCollection->random($calledClass['class'], $i)) {
85
                /** @var Seeder\Seed $seed */
86
                $seed = GeneralUtility::makeInstance(Seeder\Domain\Model\Seed::class);
87
                $seed->setTarget($name)
88
                    ->setTitle($calledClass['class'])
89
                    ->setProperties($tableConfiguration->getColumns());
90
            }
91
            $seedCollection->attach($seed);
92
        }
93
94
        return $seedCollection;
95
    }
96
}
97