1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magefix\Fixture\Factory; |
4
|
|
|
|
5
|
|
|
use Mage; |
6
|
|
|
use Magefix\Exceptions\UndefinedFixtureModel; |
7
|
|
|
use Magefix\Exceptions\UnknownFixtureType; |
8
|
|
|
use Magefix\Fixture\Instanciator; |
9
|
|
|
use Magefix\Fixture\Typeable; |
10
|
|
|
use Magefix\Fixtures\Registry; |
11
|
|
|
use Magefix\Parser\ResourceLocator; |
12
|
|
|
use Magefix\Yaml\Parser as YamlParser; |
13
|
|
|
use Magefix\Fixture\Builder\AbstractBuilder as FixtureBuilder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Builder |
17
|
|
|
* |
18
|
|
|
* Provide utility method build to build Fixtures |
19
|
|
|
* |
20
|
|
|
* @package Magefix\Fixture\Factory |
21
|
|
|
* @author Carlo Tasca <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class Builder implements Typeable |
24
|
|
|
{ |
25
|
|
|
use Registry; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $fixtureType |
29
|
|
|
* @param ResourceLocator $locator |
30
|
|
|
* @param string $yamlFilename |
31
|
|
|
* @param bool|string $hook |
32
|
|
|
* |
33
|
|
|
* @return int |
34
|
|
|
* @throws UnknownFixtureType |
35
|
|
|
* @throws \Magefix\Exceptions\UnavailableHook |
36
|
|
|
*/ |
37
|
|
|
public static function build($fixtureType, ResourceLocator $locator, $yamlFilename, $hook = '') |
38
|
|
|
{ |
39
|
|
|
$parser = new YamlParser($locator, $yamlFilename); |
40
|
|
|
$fixtureData = is_array($parser->parse()) ? $parser->parse() : []; |
41
|
|
|
$fixture = Instanciator::instance($fixtureType, $fixtureData, $hook); |
42
|
|
|
$fixtureId = $fixture->build(); |
43
|
|
|
|
44
|
|
|
if ($hook) { |
45
|
|
|
self::registerFixture($fixtureType, $fixtureId, $hook); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $fixtureId; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $fixtureType |
53
|
|
|
* @param FixtureBuilder $builder |
54
|
|
|
* @param array $entities |
55
|
|
|
* @param string $hook |
56
|
|
|
* @return array |
57
|
|
|
* @throws UndefinedFixtureModel |
58
|
|
|
*/ |
59
|
|
|
public static function buildMany($fixtureType, FixtureBuilder $builder, array $entities, $hook = '') |
60
|
|
|
{ |
61
|
|
|
$many = []; |
62
|
|
|
|
63
|
|
|
$iterator = new \ArrayIterator($entities); |
64
|
|
|
|
65
|
|
|
while ($iterator->valid()) { |
66
|
|
|
$builder->throwUndefinedDataProvider($iterator->current()); |
67
|
|
|
$fixtureId = self::_buildAndRegisterFixture($fixtureType, $hook, $iterator->current()); |
68
|
|
|
$many[] = self::_getMagentoModel($iterator->current(), $fixtureId); |
69
|
|
|
|
70
|
|
|
$iterator->next(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $many; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $fixtureType |
78
|
|
|
* @param $hook |
79
|
|
|
* @param $entity |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected static function _buildAndRegisterFixture($fixtureType, $hook, $entity) |
83
|
|
|
{ |
84
|
|
|
$fixtureData = []; |
85
|
|
|
$fixtureData['fixture'] = $entity; |
86
|
|
|
|
87
|
|
|
$fixture = Instanciator::instance($fixtureType, $fixtureData, $hook); |
88
|
|
|
|
89
|
|
|
$fixtureId = $fixture->build(); |
90
|
|
|
|
91
|
|
|
self::registerFixture( |
92
|
|
|
$fixtureType, $fixtureId, $fixture->getHook() |
93
|
|
|
); |
94
|
|
|
return $fixtureId; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $entity |
99
|
|
|
* @param $fixtureId |
100
|
|
|
* @return \Mage_Core_Model_Abstract |
101
|
|
|
* @throws UndefinedFixtureModel |
102
|
|
|
*/ |
103
|
|
|
private static function _getMagentoModel($entity, $fixtureId) |
104
|
|
|
{ |
105
|
|
|
if (!isset($entity['model'])) { |
106
|
|
|
throw new UndefinedFixtureModel( |
107
|
|
|
'Magento model has not been defined. Check fixture yml.' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return Mage::getModel($entity['model'])->load($fixtureId); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|