Skip to content

Commit

Permalink
Merge pull request #28 from pyrorules/master
Browse files Browse the repository at this point in the history
Added block fixtures
  • Loading branch information
bitbager authored Sep 18, 2017
2 parents c5e7563 + 794b5fd commit 9244453
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 1 deletion.
87 changes: 87 additions & 0 deletions src/Fixture/HtmlBlockFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* This file was created by the developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

namespace BitBag\CmsPlugin\Fixture;

use BitBag\CmsPlugin\Entity\Block;
use BitBag\CmsPlugin\Entity\BlockInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

/**
* @author Wojciech Górski <[email protected]>
*/
final class HtmlBlockFixture extends AbstractFixture implements FixtureInterface
{
/**
* @var EntityManagerInterface
*/
private $blockManager;

/**
* @param EntityManagerInterface $blockManager
*/
public function __construct(EntityManagerInterface $blockManager)
{
$this->blockManager = $blockManager;
}

/**
* @inheritDoc
*/
public function load(array $options)
{
$block = new Block();

$block->setCode($options['code']);
$block->setType(BlockInterface::HTML_BLOCK_TYPE);

foreach ($options['translations'] as $translation) {
$block->setCurrentLocale($translation['locale']);
$block->setName($translation['name']);
$block->setContent($translation['content']);
}

$this->blockManager->persist($block);

$this->blockManager->flush();
}

/**
* @inheritDoc
*/
public function getName()
{
return 'bitbag_cms_block_html';
}

/**
* @inheritDoc
*/
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
{
$optionsNode
->children()
->scalarNode('code')->isRequired()->cannotBeEmpty()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('locale')->isRequired()->cannotBeEmpty()->end()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('content')->isRequired()->cannotBeEmpty()->end()
->end()
->end()
->end()
->end()
;
}
}
113 changes: 113 additions & 0 deletions src/Fixture/ImageBlockFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* This file was created by the developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

namespace BitBag\CmsPlugin\Fixture;

use BitBag\CmsPlugin\Entity\Block;
use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Entity\Image;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\HttpFoundation\File\File;

/**
* @author Wojciech Górski <[email protected]>
*/
final class ImageBlockFixture extends AbstractFixture implements FixtureInterface
{
/**
* @var EntityManagerInterface
*/
private $blockManager;

/**
* @var ImageUploaderInterface
*/
private $imageUploader;

/**
* @param EntityManagerInterface $blockManager
* @param ImageUploaderInterface $imageUploader
*/
public function __construct(EntityManagerInterface $blockManager, ImageUploaderInterface $imageUploader)
{
$this->blockManager = $blockManager;
$this->imageUploader = $imageUploader;
}

/**
* @inheritDoc
*/
public function load(array $options)
{
$block = new Block();

$block->setCode($options['code']);
$block->setType(BlockInterface::IMAGE_BLOCK_TYPE);

foreach ($options['translations'] as $translation) {
$block->setCurrentLocale($translation['locale']);
$block->setName($translation['name']);
$block->setLink($translation['link']);

$file = new File($translation['image']['filePath']);

$image = new Image();
$image->setFile($file);
$image->setType($translation['image']['type']);

$this->imageUploader->upload($image);

$block->setImage($image);
}

$this->blockManager->persist($block);

$this->blockManager->flush();
}

/**
* @inheritDoc
*/
public function getName()
{
return 'bitbag_cms_block_image';
}

/**
* @inheritDoc
*/
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
{
$optionsNode
->children()
->scalarNode('code')->isRequired()->cannotBeEmpty()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('locale')->isRequired()->cannotBeEmpty()->end()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('link')->isRequired()->cannotBeEmpty()->end()
->arrayNode('image')
->children()
->scalarNode('type')->end()
->scalarNode('filePath')->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
87 changes: 87 additions & 0 deletions src/Fixture/TextBlockFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* This file was created by the developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

namespace BitBag\CmsPlugin\Fixture;

use BitBag\CmsPlugin\Entity\Block;
use BitBag\CmsPlugin\Entity\BlockInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

/**
* @author Wojciech Górski <[email protected]>
*/
final class TextBlockFixture extends AbstractFixture implements FixtureInterface
{
/**
* @var EntityManagerInterface
*/
private $blockManager;

/**
* @param EntityManagerInterface $blockManager
*/
public function __construct(EntityManagerInterface $blockManager)
{
$this->blockManager = $blockManager;
}

/**
* @inheritDoc
*/
public function load(array $options)
{
$block = new Block();

$block->setCode($options['code']);
$block->setType(BlockInterface::TEXT_BLOCK_TYPE);

foreach ($options['translations'] as $translation) {
$block->setCurrentLocale($translation['locale']);
$block->setName($translation['name']);
$block->setContent($translation['content']);
}

$this->blockManager->persist($block);

$this->blockManager->flush();
}

/**
* @inheritDoc
*/
public function getName()
{
return 'bitbag_cms_block_text';
}

/**
* @inheritDoc
*/
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
{
$optionsNode
->children()
->scalarNode('code')->isRequired()->cannotBeEmpty()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('locale')->isRequired()->cannotBeEmpty()->end()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('content')->isRequired()->cannotBeEmpty()->end()
->end()
->end()
->end()
->end()
;
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ imports:
- { resource: "@BitBagCmsPlugin/Resources/config/form.yml" }
- { resource: "@BitBagCmsPlugin/Resources/config/factory.yml" }
- { resource: "@BitBagCmsPlugin/Resources/config/grids.yml" }
- { resource: "@BitBagCmsPlugin/Resources/config/twig.yml" }
- { resource: "@BitBagCmsPlugin/Resources/config/twig.yml" }
- { resource: "@BitBagCmsPlugin/Resources/config/fixture.yml" }
2 changes: 2 additions & 0 deletions src/Resources/config/fixture.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: "@BitBagCmsPlugin/Resources/config/fixture/block.yml" }
22 changes: 22 additions & 0 deletions src/Resources/config/fixture/block.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
bitbag.cms_plugin.fixture.html_block:
class: BitBag\CmsPlugin\Fixture\HtmlBlockFixture
arguments:
- '@bitbag.manager.block'
tags:
- { name: sylius_fixtures.fixture }

bitbag.cms_plugin.fixture.text_block:
class: BitBag\CmsPlugin\Fixture\TextBlockFixture
arguments:
- '@bitbag.manager.block'
tags:
- { name: sylius_fixtures.fixture }

bitbag.cms_plugin.fixture.image_block:
class: BitBag\CmsPlugin\Fixture\ImageBlockFixture
arguments:
- '@bitbag.manager.block'
- '@sylius.image_uploader'
tags:
- { name: sylius_fixtures.fixture }

0 comments on commit 9244453

Please sign in to comment.