Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Associated.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Fixture\DataSource;
10 use Magento\Mtf\Fixture\FixtureFactory;
11 use Magento\Mtf\Fixture\FixtureInterface;
12 use Magento\Mtf\Fixture\InjectableFixture;
13 use Magento\Mtf\Repository\RepositoryFactory;
14 
18 class Associated extends DataSource
19 {
29  public function __construct(
30  RepositoryFactory $repositoryFactory,
31  FixtureFactory $fixtureFactory,
32  array $data,
33  array $params = []
34  ) {
35  $this->params = $params;
36  $this->data = isset($data['dataset'])
37  ? $repositoryFactory->get($this->params['repository'])->get($data['dataset'])
38  : $data;
39 
40  $this->data['products'] = (isset($data['products']) && !is_array($data['products']))
41  ? explode(',', $data['products'])
42  : $this->data['products'];
43 
44  foreach ($this->data['products'] as $key => $product) {
45  if (!($product instanceof FixtureInterface)) {
46  list($fixture, $dataset) = explode('::', $product);
48  $product = $fixtureFactory->createByCode($fixture, ['dataset' => $dataset]);
49  }
50  if (!$product->hasData('id')) {
51  $product->persist();
52  }
53  $this->data['products'][$key] = $product;
54  }
55 
56  $assignedProducts = &$this->data['assigned_products'];
57  foreach (array_keys($assignedProducts) as $key) {
58  $assignedProducts[$key]['name'] = $this->data['products'][$key]->getName();
59  $assignedProducts[$key]['id'] = $this->data['products'][$key]->getId();
60  $assignedProducts[$key]['position'] = $key + 1;
61  }
62  }
63 }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18