Skip to content

Commit

Permalink
Merge pull request Sylius#81 from bitbager/master
Browse files Browse the repository at this point in the history
[Shop] Present the admin data in the store
  • Loading branch information
bitbager authored Nov 13, 2017
2 parents d452ce8 + 616d027 commit 984c025
Show file tree
Hide file tree
Showing 78 changed files with 1,354 additions and 427 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ before_script:
script:
- composer validate --strict

- bin/phpspec run
- bin/phpunit
# - bin/phpspec run
# - bin/phpunit
- bin/behat --strict -vvv --no-interaction || bin/behat --strict -vvv --no-interaction --rerun --tags="~@todo"

after_failure:
Expand Down
67 changes: 67 additions & 0 deletions src/Controller/BlockController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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].
*/

declare(strict_types=1);

namespace BitBag\CmsPlugin\Controller;

use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Mikołaj Król <[email protected]>
*/
final class BlockController extends ResourceController
{
/**
* @param Request $request
*
* @return Response
*/
public function renderBlockAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

$this->isGrantedOr403($configuration, ResourceActions::SHOW);

$code = $request->get('code');
$blockResourceResolver = $this->get('bitbag.resolver.block_resource');

$block = $blockResourceResolver->findOrLog($code);

if (null === $block) {
return new Response();
}

$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $block);

$view = View::create($block);
$blockTemplateResolver = $this->get('bitbag.resolver.block_template');
$template = $blockTemplateResolver->resolveTemplate($block);

if ($configuration->isHtmlRequest()) {
$view
->setTemplate($template)
->setTemplateVar($this->metadata->getName())
->setData([
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $block,
$this->metadata->getName() => $block,
])
;
}

return $this->viewHandler->handle($configuration, $view);
}
}
2 changes: 1 addition & 1 deletion src/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Block implements BlockInterface
{
use ToggleableTrait;
use SectionableTrait;
use ProductableTrait;
use ProductsAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/BlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface BlockInterface extends
ResourceInterface,
TranslatableInterface,
ToggleableInterface,
ProductableInterface,
ProductsAwareInterface,
SectionableInterface
{
const TEXT_BLOCK_TYPE = 'text';
Expand Down
2 changes: 0 additions & 2 deletions src/Entity/FrequentlyAskedQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface
{
use SectionableTrait;
use ToggleableTrait,
TranslatableTrait {
__construct as private initializeTranslationsCollection;
Expand All @@ -44,7 +43,6 @@ class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface

public function __construct()
{
$this->initializeSectionsCollection();
$this->initializeTranslationsCollection();
}

Expand Down
10 changes: 7 additions & 3 deletions src/Entity/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\CmsPlugin\Entity;

use Sylius\Component\Resource\Model\TimestampableTrait;
use Sylius\Component\Resource\Model\ToggleableTrait;
use Sylius\Component\Resource\Model\TranslatableTrait;
use Sylius\Component\Resource\Model\TranslationInterface;
Expand All @@ -22,9 +23,10 @@
*/
class Page implements PageInterface
{
use SectionableTrait;
use ToggleableTrait;
use ProductableTrait;
use ProductsAwareTrait;
use SectionableTrait;
use TimestampableTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand All @@ -41,9 +43,11 @@ class Page implements PageInterface

public function __construct()
{
$this->initializeTranslationsCollection();
$this->initializeProductsCollection();
$this->initializeSectionsCollection();
$this->initializeTranslationsCollection();

$this->createdAt = new \DateTime();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Entity/PageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\CmsPlugin\Entity;

use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TimestampableInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;

Expand All @@ -24,8 +25,9 @@ interface PageInterface extends
ResourceInterface,
TranslatableInterface,
ToggleableInterface,
ProductableInterface,
SectionableInterface
ProductsAwareInterface,
SectionableInterface,
TimestampableInterface
{
/**
* @return null|string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Mikołaj Król <[email protected]>
*/
interface ProductableInterface
interface ProductsAwareInterface
{
public function initializeProductsCollection(): void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* @author Mikołaj Król <[email protected]>
*/
trait ProductableTrait
trait ProductsAwareTrait
{
/**
* @var Collection|ProductInterface[]
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Section implements SectionInterface
}

/**
* @var int
* @var null|int
*/
protected $id;

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/SectionTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public function setName(?string $name): void
{
$this->name = $name;
}
}
}
88 changes: 11 additions & 77 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,111 +12,43 @@

namespace BitBag\CmsPlugin\Fixture;

use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Entity\BlockTranslationInterface;
use BitBag\CmsPlugin\Entity\BlockImage;
use BitBag\CmsPlugin\Factory\BlockFactoryInterface;
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\CmsPlugin\Fixture\Factory\FixtureFactoryInterface;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @author Mikołaj Król <[email protected]>
*/
final class BlockFixture extends AbstractFixture implements FixtureInterface
{
/**
* @var BlockFactoryInterface
* @var FixtureFactoryInterface
*/
private $blockFactory;
private $blockFixtureFactory;

/**
* @var FactoryInterface
* @param FixtureFactoryInterface $blockFixtureFactory
*/
private $blockTranslationFactory;

/**
* @var BlockRepositoryInterface
*/
private $blockRepository;

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

/**
* @param BlockFactoryInterface $blockFactory
* @param FactoryInterface $blockTranslationFactory
* @param BlockRepositoryInterface $blockRepository
* @param ImageUploaderInterface $imageUploader
*/
public function __construct(
BlockFactoryInterface $blockFactory,
FactoryInterface $blockTranslationFactory,
BlockRepositoryInterface $blockRepository,
ImageUploaderInterface $imageUploader
)
public function __construct(FixtureFactoryInterface $blockFixtureFactory)
{
$this->blockFactory = $blockFactory;
$this->blockTranslationFactory = $blockTranslationFactory;
$this->blockRepository = $blockRepository;
$this->imageUploader = $imageUploader;
$this->blockFixtureFactory = $blockFixtureFactory;
}

/**
* {@inheritdoc}
*/
public function load(array $options): void
{
foreach ($options['blocks'] as $code => $fields) {

if (null !== $this->blockRepository->findOneBy(['code' => $code])) {
continue;
}

$type = $fields['type'];
$block = $this->blockFactory->createWithType($type);

$block->setCode($code);
$block->setEnabled($fields['enabled']);

foreach ($fields['translations'] as $localeCode => $translation) {
/** @var BlockTranslationInterface $blockTranslation */
$blockTranslation = $this->blockTranslationFactory->createNew();

$blockTranslation->setLocale($localeCode);
$blockTranslation->setName($translation['name']);
$blockTranslation->setContent($translation['content']);

if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
$image = new BlockImage();
$path = $translation['image_path'];
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg');

$image->setFile($uploadedImage);
$blockTranslation->setImage($image);

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

$block->addTranslation($blockTranslation);
}

$this->blockRepository->add($block);
}
$this->blockFixtureFactory->load($options['custom']);
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'bitbag_cms_block';
return 'block';
}

/**
Expand All @@ -126,9 +58,11 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
{
$optionsNode
->children()
->arrayNode('blocks')
->arrayNode('custom')
->prototype('array')
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->booleanNode('last_four_products')->defaultFalse()->end()
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('translations')
Expand Down
Loading

0 comments on commit 984c025

Please sign in to comment.