|
| 1 | +# How to create new import source |
| 2 | + |
| 3 | +## Source class |
| 4 | + |
| 5 | +Any custom source class must extend```AbstractSource``` |
| 6 | + |
| 7 | +This class is used to define a new type of Source to appear in the application and can be used to store the necessary configuration data |
| 8 | + |
| 9 | +```php |
| 10 | +namespace YourNameSpace\Domain\Entity; |
| 11 | + |
| 12 | +use Ergonode\SharedKernel\Domain\Aggregate\SourceId; |
| 13 | +use Ergonode\Importer\Domain\Entity\Source\AbstractSource; |
| 14 | + |
| 15 | +class YourSource extends AbstractSource |
| 16 | +{ |
| 17 | + public const TYPE = 'your-source-type'; |
| 18 | + |
| 19 | + public function __construct(SourceId $id, string $name) |
| 20 | + { |
| 21 | + parent::__construct($id, $name); |
| 22 | + } |
| 23 | + |
| 24 | + public function getType(): string |
| 25 | + { |
| 26 | + return self::TYPE; |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Source class configuration |
| 32 | + |
| 33 | +You need create Form class for configuration Your Source. |
| 34 | + |
| 35 | +Only "name" field is required, other are dependent from you needs. |
| 36 | + |
| 37 | +```php |
| 38 | +namespace YourNameSpace\Application\Form; |
| 39 | + |
| 40 | +use YourNameSpace\Application\Model\YourSourceConfigurationModel; |
| 41 | +use Symfony\Component\Form\AbstractType; |
| 42 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 43 | +use Symfony\Component\Form\FormBuilderInterface; |
| 44 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 45 | + |
| 46 | +class YourSourceConfigurationForm extends AbstractType |
| 47 | +{ |
| 48 | + /** |
| 49 | + * @param array $options |
| 50 | + */ |
| 51 | + public function buildForm(FormBuilderInterface $builder, array $options): void |
| 52 | + { |
| 53 | + $builder |
| 54 | + ->add( |
| 55 | + 'name', |
| 56 | + TextType::class, |
| 57 | + [ |
| 58 | + 'label' => 'Name', |
| 59 | + ] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function configureOptions(OptionsResolver $resolver): void |
| 64 | + { |
| 65 | + $resolver->setDefaults([ |
| 66 | + 'translation_domain' => 'importer', |
| 67 | + 'data_class' => YourSourceConfigurationModel::class, |
| 68 | + 'allow_extra_fields' => true, |
| 69 | + 'label' => 'Import settings', |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + public function getBlockPrefix(): ?string |
| 74 | + { |
| 75 | + return null; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Configuration Model class |
| 81 | + |
| 82 | +```php |
| 83 | +namespace YourNameSpace\Application\Model; |
| 84 | + |
| 85 | +use Symfony\Component\Validator\Constraints as Assert; |
| 86 | +use YourNameSpace\Domain\Entity\YourSource; |
| 87 | + |
| 88 | +class YourSourceConfigurationModel |
| 89 | +{ |
| 90 | + /** |
| 91 | + * @Assert\NotBlank() |
| 92 | + * @Assert\Length(min=2) |
| 93 | + */ |
| 94 | + public ?string $name = null; |
| 95 | + |
| 96 | + public function __construct(YourSource $source = null) |
| 97 | + { |
| 98 | + if ($source) { |
| 99 | + $this->name = $source->getName(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +For providing form need to add ```YourSourceFormFactory``` class. |
| 106 | + |
| 107 | +```php |
| 108 | +namespace YourNameSpace\Application\Factory; |
| 109 | + |
| 110 | +use Ergonode\Importer\Domain\Entity\Source\AbstractSource; |
| 111 | +use YourNameSpace\Domain\Entity\YourSource; |
| 112 | +use Symfony\Component\Form\FormFactoryInterface; |
| 113 | +use YourNamespace\Application\Form\YourSourceConfigurationForm; |
| 114 | +use Symfony\Component\Form\FormInterface; |
| 115 | +use YourNamespace\Application\Model\YourSourceConfigurationModel; |
| 116 | +use Ergonode\Importer\Application\Provider\SourceFormFactoryInterface; |
| 117 | +use Symfony\Component\HttpFoundation\Request; |
| 118 | + |
| 119 | +class YourSourceFormFactory implements SourceFormFactoryInterface |
| 120 | +{ |
| 121 | + private FormFactoryInterface $formFactory; |
| 122 | + |
| 123 | + public function __construct(FormFactoryInterface $formFactory) |
| 124 | + { |
| 125 | + $this->formFactory = $formFactory; |
| 126 | + } |
| 127 | + |
| 128 | + public function supported(string $type): bool |
| 129 | + { |
| 130 | + return YourSource::TYPE === $type; |
| 131 | + } |
| 132 | + |
| 133 | + public function create(AbstractSource $source = null): FormInterface |
| 134 | + { |
| 135 | + $model = new YourSourceConfigurationModel($source); |
| 136 | + if (null === $source) { |
| 137 | + return $this->formFactory->create(YourSourceConfigurationForm::class, $model); |
| 138 | + } |
| 139 | + |
| 140 | + return $this->formFactory->create( |
| 141 | + YourSourceConfigurationForm::class, |
| 142 | + $model, |
| 143 | + ['method' => Request::METHOD_PUT] |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Next step is to create Source Manipulation Commands and Handlers |
| 150 | + |
| 151 | +```php |
| 152 | +namespace YourNameSpace\Domain\Command; |
| 153 | + |
| 154 | +use Ergonode\Importer\Domain\Command\CreateSourceCommandInterface; |
| 155 | +use Ergonode\SharedKernel\Domain\Aggregate\SourceId; |
| 156 | +use JMS\Serializer\Annotation as JMS; |
| 157 | + |
| 158 | +class CreateYourSourceCommand implements CreateSourceCommandInterface |
| 159 | +{ |
| 160 | + /** |
| 161 | + * @JMS\Type("Ergonode\SharedKernel\Domain\Aggregate\SourceId") |
| 162 | + */ |
| 163 | + private SourceId $id; |
| 164 | + |
| 165 | + /** |
| 166 | + * @JMS\Type("string") |
| 167 | + */ |
| 168 | + private string $name; |
| 169 | + |
| 170 | + public function __construct( |
| 171 | + SourceId $id, |
| 172 | + string $name |
| 173 | + ) { |
| 174 | + $this->id = $id; |
| 175 | + $this->name = $name; |
| 176 | + } |
| 177 | + |
| 178 | + public function getId(): SourceId |
| 179 | + { |
| 180 | + return $this->id; |
| 181 | + } |
| 182 | + |
| 183 | + public function getName(): string |
| 184 | + { |
| 185 | + return $this->name; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +``` |
| 190 | + |
| 191 | +```php |
| 192 | +namespace YourNameSpace\Infrastructure\Handler; |
| 193 | + |
| 194 | +use Ergonode\Importer\Domain\Repository\SourceRepositoryInterface; |
| 195 | +use YourNameSpace\Domain\Command\CreateYourSourceCommand; |
| 196 | +use YourNameSpace\Domain\Entity\YourSource; |
| 197 | + |
| 198 | +class YourSourceSourceCommandHandler |
| 199 | +{ |
| 200 | + private SourceRepositoryInterface $repository; |
| 201 | + |
| 202 | + public function __construct(SourceRepositoryInterface $repository) |
| 203 | + { |
| 204 | + $this->repository = $repository; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @throws \Exception |
| 209 | + */ |
| 210 | + public function __invoke(CreateYourSourceCommand $command): void |
| 211 | + { |
| 212 | + $source = new YourSource( |
| 213 | + $command->getId(), |
| 214 | + $command->getName() |
| 215 | + ); |
| 216 | + |
| 217 | + $this->repository->save($source); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +``` |
| 222 | + |
| 223 | +At last add you source import processor. |
| 224 | + |
| 225 | +```php |
| 226 | +namespace YourNameSpace\Infrastructure\Processor; |
| 227 | + |
| 228 | +use Ergonode\Importer\Domain\Entity\Import; |
| 229 | +use Ergonode\Importer\Infrastructure\Processor\SourceImportProcessorInterface; |
| 230 | +use YourNameSpace\Domain\Entity\YourSource; |
| 231 | + |
| 232 | +class YourSourceImportProcess implements SourceImportProcessorInterface |
| 233 | +{ |
| 234 | + public function supported(string $type): bool |
| 235 | + { |
| 236 | + return $type === YourSource::TYPE; |
| 237 | + } |
| 238 | + |
| 239 | + public function start(Import $import): void |
| 240 | + { |
| 241 | + // here is body of your import process |
| 242 | + } |
| 243 | +} |
| 244 | +``` |
0 commit comments