Skip to content

Commit

Permalink
Merge pull request #176 from BitBagCommerce/dev
Browse files Browse the repository at this point in the history
Add channel awareness to media
  • Loading branch information
bitbager authored Jul 16, 2018
2 parents 4f48af4 + 5eb2a30 commit 3f81238
Show file tree
Hide file tree
Showing 30 changed files with 91 additions and 86 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

**Merged pull requests:**

- Dev [\#175](https://github.com/BitBagCommerce/SyliusCmsPlugin/pull/175) ([bitbager](https://github.com/bitbager))
- Dev [\#174](https://github.com/BitBagCommerce/SyliusCmsPlugin/pull/174) ([bitbager](https://github.com/bitbager))
- Prepare 2.0.0 release [\#172](https://github.com/BitBagCommerce/SyliusCmsPlugin/pull/172) ([bitbager](https://github.com/bitbager))

## [v2.0.0-rc.2](https://github.com/BitBagCommerce/SyliusCmsPlugin/tree/v2.0.0-rc.2) (2018-07-13)
Expand Down
13 changes: 13 additions & 0 deletions spec/Entity/MediaSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\HttpFoundation\File\File;
Expand Down Expand Up @@ -87,4 +88,16 @@ function it_associates_sections(SectionInterface $firstSection, SectionInterface

$this->hasSection($firstSection)->shouldReturn(false);
}

function it_associates_channels(ChannelInterface $firstChannel, ChannelInterface $secondChannel): void
{
$this->addChannel($firstChannel);
$this->hasChannel($firstChannel)->shouldReturn(true);

$this->hasChannel($secondChannel)->shouldReturn(false);

$this->removeChannel($firstChannel);

$this->hasChannel($firstChannel)->shouldReturn(false);
}
}
12 changes: 0 additions & 12 deletions spec/Entity/SectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,4 @@ function it_allows_access_via_properties(): void
$this->setCode('blog');
$this->getCode()->shouldReturn('blog');
}

function it_associates_channels(ChannelInterface $firstChannel, ChannelInterface $secondChannel): void
{
$this->addChannel($firstChannel);
$this->hasChannel($firstChannel)->shouldReturn(true);

$this->hasChannel($secondChannel)->shouldReturn(false);

$this->removeChannel($firstChannel);

$this->hasChannel($firstChannel)->shouldReturn(false);
}
}
17 changes: 8 additions & 9 deletions src/Controller/Action/Admin/ImportDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Translation\TranslatorInterface;

final class ImportDataAction
Expand All @@ -34,8 +34,8 @@ final class ImportDataAction
/** @var FormFactoryInterface */
private $formFactory;

/** @var Session */
private $session;
/** @var FlashBagInterface */
private $flashBag;

/** @var TranslatorInterface */
private $translator;
Expand All @@ -46,13 +46,13 @@ final class ImportDataAction
public function __construct(
ImportProcessorInterface $importProcessor,
FormFactoryInterface $formFactory,
Session $session,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
ViewHandler $viewHandler
) {
$this->importProcessor = $importProcessor;
$this->formFactory = $formFactory;
$this->session = $session;
$this->flashBag = $flashBag;
$this->translator = $translator;
$this->viewHandler = $viewHandler;
}
Expand All @@ -65,7 +65,6 @@ public function __invoke(Request $request): Response
$form->handleRequest($request);

if ($request->isMethod('POST') && $form->isSubmitted()) {
$flashBag = $this->session->getFlashBag();

if ($form->isValid()) {
/** @var UploadedFile $file */
Expand All @@ -75,12 +74,12 @@ public function __invoke(Request $request): Response
try {
$this->importProcessor->process($resourceName, $file->getPathname());

$flashBag->set('success', $this->translator->trans('bitbag_sylius_cms_plugin.ui.successfully_imported'));
$this->flashBag->set('success', $this->translator->trans('bitbag_sylius_cms_plugin.ui.successfully_imported'));
} catch (ImportFailedException $exception) {
$flashBag->set('error', $exception->getMessage());
$this->flashBag->set('error', $exception->getMessage());
}
} else {
$flashBag->set('error', rtrim(implode($this->getFormErrors($form), ', '), ', '));
$this->flashBag->set('error', rtrim(implode($this->getFormErrors($form), ', '), ', '));
}

return new RedirectResponse($referer);
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/ImageDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function download(string $url): File
{
$pathInfo = pathinfo($url);
$extension = $pathInfo['extension'];

$path = sys_get_temp_dir() . md5(random_bytes(10)) . '.' . $extension;

$this->filesystem->dumpFile($path, file_get_contents($url));

return new File($path);
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Media implements MediaInterface
use ToggleableTrait;
use SectionableTrait;
use ProductsAwareTrait;
use ChannelsAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand Down Expand Up @@ -52,6 +53,7 @@ public function __construct()
$this->initializeTranslationsCollection();
$this->initializeSectionsCollection();
$this->initializeProductsCollection();
$this->initializeChannelsCollection();
}

public function getId(): ?int
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/MediaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;
Expand All @@ -22,7 +23,8 @@ interface MediaInterface extends
TranslatableInterface,
ToggleableInterface,
ProductsAwareInterface,
SectionableInterface
SectionableInterface,
ChannelsAwareInterface
{
public const IMAGE_TYPE = 'image';
public const VIDEO_TYPE = 'video';
Expand Down
2 changes: 0 additions & 2 deletions src/Entity/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

class Section implements SectionInterface
{
use ChannelsAwareTrait;
use TranslatableTrait {
__construct as private initializeTranslationsCollection;
}
Expand All @@ -31,7 +30,6 @@ class Section implements SectionInterface
public function __construct()
{
$this->initializeTranslationsCollection();
$this->initializeChannelsCollection();
}

public function getId(): ?int
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/SectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;

interface SectionInterface extends ResourceInterface, TranslatableInterface, ChannelsAwareInterface
interface SectionInterface extends ResourceInterface, TranslatableInterface
{
public function getCode(): ?string;

Expand Down
7 changes: 7 additions & 0 deletions src/Fixture/Factory/MediaFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use BitBag\SyliusCmsPlugin\MediaProvider\ProviderInterface;
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\HttpFoundation\File\File;

Expand All @@ -30,6 +31,9 @@ final class MediaFixtureFactory implements FixtureFactoryInterface
/** @var FactoryInterface */
private $mediaTranslationFactory;

/** @var ChannelContextInterface */
private $channelContext;

/** @var MediaProviderResolverInterface */
private $mediaProviderResolver;

Expand All @@ -45,13 +49,15 @@ final class MediaFixtureFactory implements FixtureFactoryInterface
public function __construct(
FactoryInterface $mediaFactory,
FactoryInterface $mediaTranslationFactory,
ChannelContextInterface $channelContext,
MediaProviderResolverInterface $mediaProviderResolver,
MediaRepositoryInterface $mediaRepository,
ProductsAssignerInterface $productsAssigner,
SectionsAssignerInterface $sectionsAssigner
) {
$this->mediaFactory = $mediaFactory;
$this->mediaTranslationFactory = $mediaTranslationFactory;
$this->channelContext = $channelContext;
$this->mediaProviderResolver = $mediaProviderResolver;
$this->mediaRepository = $mediaRepository;
$this->productsAssigner = $productsAssigner;
Expand Down Expand Up @@ -86,6 +92,7 @@ private function createMedia(string $code, array $mediaData): void
$media->setCode($code);
$media->setEnabled($mediaData['enabled']);
$media->setFile(new File($mediaData['path']));
$media->addChannel($this->channelContext->getChannel());

$this->mediaProviderResolver->resolveProvider($media)->upload($media);

Expand Down
9 changes: 1 addition & 8 deletions src/Fixture/Factory/SectionFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface;
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class SectionFixtureFactory implements FixtureFactoryInterface
Expand All @@ -29,19 +28,14 @@ final class SectionFixtureFactory implements FixtureFactoryInterface
/** @var SectionRepositoryInterface */
private $sectionRepository;

/** @var ChannelContextInterface */
private $channelContext;

public function __construct(
FactoryInterface $sectionFactory,
FactoryInterface $sectionTranslationFactory,
SectionRepositoryInterface $sectionRepository,
ChannelContextInterface $channelContext
SectionRepositoryInterface $sectionRepository
) {
$this->sectionFactory = $sectionFactory;
$this->sectionTranslationFactory = $sectionTranslationFactory;
$this->sectionRepository = $sectionRepository;
$this->channelContext = $channelContext;
}

public function load(array $data): void
Expand All @@ -58,7 +52,6 @@ public function load(array $data): void
$section = $this->sectionFactory->createNew();

$section->setCode($code);
$section->addChannel($this->channelContext->getChannel());

foreach ($fields['translations'] as $localeCode => $translation) {
/** @var SectionTranslationInterface $sectionTranslation */
Expand Down
7 changes: 7 additions & 0 deletions src/Form/Type/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusCmsPlugin\Form\Type;

use BitBag\SyliusCmsPlugin\Form\Type\Translation\MediaTranslationType;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductAutocompleteChoiceType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
Expand Down Expand Up @@ -59,6 +60,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'bitbag_sylius_cms_plugin.ui.products',
'multiple' => true,
])
->add('channels', ChannelChoiceType::class, [
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
'required' => false,
'multiple' => true,
'expanded' => true,
])
->add('translations', ResourceTranslationsType::class, [
'entry_type' => MediaTranslationType::class,
])
Expand Down
5 changes: 0 additions & 5 deletions src/Form/Type/SectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('translations', ResourceTranslationsType::class, [
'entry_type' => SectionTranslationType::class,
])
->add('channels', ChannelChoiceType::class, [
'multiple' => true,
'expanded' => true,
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
])
;
}

Expand Down
19 changes: 10 additions & 9 deletions src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public function createListQueryBuilder(): QueryBuilder
;
}

public function findOneEnabledByCode(string $code): ?MediaInterface
public function findOneEnabledByCode(string $code, string $channelCode): ?MediaInterface
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channel')
->where('o.code = :code')
->andWhere('o.enabled = true')
->setParameter('code', $code)
Expand All @@ -36,30 +37,30 @@ public function findOneEnabledByCode(string $code): ?MediaInterface
;
}

public function findBySectionCode(string $sectionCode, string $localeCode): array
public function findBySectionCode(string $sectionCode, string $channelCode): array
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.channels', 'channel')
->innerJoin('o.sections', 'section')
->andWhere('translation.locale = :localeCode')
->where('channel.code = :channelCode')
->andWhere('section.code = :sectionCode')
->andWhere('o.enabled = true')
->setParameter('localeCode', $localeCode)
->setParameter('channelCode', $channelCode)
->setParameter('sectionCode', $sectionCode)
->getQuery()
->getResult()
;
}

public function findByProductCode(string $productCode, string $localeCode): array
public function findByProductCode(string $productCode, string $channelCode): array
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.channels', 'channel')
->innerJoin('o.products', 'product')
->andWhere('translation.locale = :localeCode')
->where('channel.code = :channelCode')
->andWhere('product.code = :productCode')
->andWhere('o.enabled = true')
->setParameter('localeCode', $localeCode)
->setParameter('channelCode', $channelCode)
->setParameter('productCode', $productCode)
->getQuery()
->getResult()
Expand Down
6 changes: 3 additions & 3 deletions src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface MediaRepositoryInterface extends RepositoryInterface
{
public function createListQueryBuilder(): QueryBuilder;

public function findOneEnabledByCode(string $code): ?MediaInterface;
public function findOneEnabledByCode(string $code, string $channelCode): ?MediaInterface;

public function findBySectionCode(string $sectionCode, string $localeCode): array;
public function findBySectionCode(string $sectionCode, string $channelCode): array;

public function findByProductCode(string $productCode, string $localeCode): array;
public function findByProductCode(string $productCode, string $channelCode): array;
}
5 changes: 1 addition & 4 deletions src/Repository/SectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ private function createTranslationBasedQueryBuilder(?string $locale = null): Que
return $queryBuilder;
}

public function findOneByCode(string $code, ?string $localeCode, string $channelCode): ?SectionInterface
public function findOneByCode(string $code, ?string $localeCode): ?SectionInterface
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.channels', 'channels')
->where('translation.locale = :localeCode')
->andWhere('o.code = :code')
->andWhere('channels.code = :channelCode')
->setParameter('code', $code)
->setParameter('localeCode', $localeCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getOneOrNullResult()
;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/SectionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public function createListQueryBuilder(): QueryBuilder;

public function findByNamePart(string $phrase, ?string $locale = null): array;

public function findOneByCode(string $code, ?string $localeCode, string $channelCode): ?SectionInterface;
public function findOneByCode(string $code, ?string $localeCode): ?SectionInterface;
}
Loading

0 comments on commit 3f81238

Please sign in to comment.