1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShopBundle\Form\Admin\Configure\AdvancedParameters\Import;
28
29use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface;
30use PrestaShop\PrestaShop\Core\Import\Configuration\ImportConfigInterface;
31use Symfony\Component\Form\FormBuilderInterface;
32
33/**
34 * Class ImportFormHandler defines a form handler of import forms.
35 */
36class ImportFormHandler implements ImportFormHandlerInterface
37{
38    /**
39     * Form builder.
40     *
41     * @var FormBuilderInterface
42     */
43    private $formBuilder;
44
45    /**
46     * Hook dispatcher.
47     *
48     * @var HookDispatcherInterface
49     */
50    private $hookDispatcher;
51
52    /**
53     * @var ImportFormDataProviderInterface
54     */
55    private $formDataProvider;
56    /**
57     * @var string
58     */
59    private $hookName;
60
61    /**
62     * @param FormBuilderInterface $formBuilder
63     * @param HookDispatcherInterface $hookDispatcher
64     * @param ImportFormDataProviderInterface $formDataProvider
65     * @param string $hookName
66     */
67    public function __construct(
68        FormBuilderInterface $formBuilder,
69        HookDispatcherInterface $hookDispatcher,
70        ImportFormDataProviderInterface $formDataProvider,
71        $hookName
72    ) {
73        $this->formBuilder = $formBuilder;
74        $this->hookDispatcher = $hookDispatcher;
75        $this->formDataProvider = $formDataProvider;
76        $this->hookName = $hookName;
77    }
78
79    /**
80     * {@inheritdoc}
81     */
82    public function getForm(ImportConfigInterface $importConfig)
83    {
84        $this->formBuilder->setData($this->formDataProvider->getData($importConfig));
85        $this->hookDispatcher->dispatchWithParameters(
86            "action{$this->hookName}Form",
87            [
88                'form_builder' => $this->formBuilder,
89            ]
90        );
91
92        return $this->formBuilder->getForm();
93    }
94
95    /**
96     * {@inheritdoc}
97     */
98    public function save(array $data)
99    {
100        $errors = $this->formDataProvider->setData($data);
101
102        $this->hookDispatcher->dispatchWithParameters(
103            "action{$this->hookName}Save",
104            [
105                'errors' => &$errors,
106                'form_data' => $data,
107            ]
108        );
109
110        return $errors;
111    }
112}
113