Skip to content

Commit

Permalink
Gift cards module
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 15, 2024
1 parent 6f576bf commit afe157e
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,5 @@ sylius_theme:
sylius_resource:
mapping:
paths:
- '%kernel.project_dir%/src/GiftCard/Entity/'
- '%kernel.project_dir%/src/Packaging/Entity/'
6 changes: 6 additions & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ doctrine:
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
GiftCard:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/GiftCard/Entity'
prefix: 'App\GiftCard\Entity'
alias: GiftCard
Packaging:
is_bundle: false
type: attribute
Expand Down
20 changes: 20 additions & 0 deletions config/packages/gift_card.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
sylius_fixtures:
suites:
gift_card:
listeners:
logger: ~
fixtures:
gift_card:
name: gift_card
options:
amount: 5

services:
App\GiftCard\:
resource: '../../src/GiftCard/*'
autowire: true
autoconfigure: true

App\GiftCard\Menu\AdminMenuListener:
tags:
- { name: kernel.event_listener, event: sylius.menu.admin.main, method: addGiftCardMenu }
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Packaging,Migrations,Tests,Kernel.php}'
exclude: '../src/{Entity,GiftCard,Packaging,Migrations,Tests,Kernel.php}'

# Controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
Expand Down
60 changes: 60 additions & 0 deletions src/GiftCard/Entity/GiftCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Entity;

use App\GiftCard\Form\Type\GiftCardAdminType;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Update;
use Sylius\Resource\Model\ResourceInterface;

#[ORM\Entity]
#[ORM\Table(name: 'app_gift_card')]
#[AsResource(section: 'admin', templatesDir: '@SyliusAdmin/Crud')]
#[Index(routePrefix: '/admin', grid: 'app_admin_gift_card')]
#[Create(routePrefix: '/admin', formType: GiftCardAdminType::class)]
#[Update(routePrefix: '/admin', formType: GiftCardAdminType::class)]
#[Delete(routePrefix: '/admin')]
class GiftCard implements ResourceInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
private int $id;

#[ORM\Column(type: 'string')]
private ?string $code;

#[ORM\Column(type: 'integer')]
private ?int $amount;

public function getId(): int
{
return $this->id;
}

public function getCode(): ?string
{
return $this->code;
}

public function setCode(?string $code): void
{
$this->code = $code;
}

public function getAmount(): ?int
{
return $this->amount;
}

public function setAmount(?int $amount): void
{
$this->amount = $amount;
}
}
49 changes: 49 additions & 0 deletions src/GiftCard/Fixture/GiftCardFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Fixture;

use App\GiftCard\Entity\GiftCard;
use Doctrine\ORM\EntityManagerInterface;
use Faker\Factory;
use Faker\Generator;
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

final class GiftCardFixture extends AbstractFixture
{
private Generator $faker;

public function __construct(private EntityManagerInterface $entityManager)
{
$this->faker = Factory::create();
}

public function load(array $options): void
{
for ($i = 0; $i < $options['amount']; ++$i) {
$giftCard = new GiftCard();
$giftCard->setCode($this->faker->uuid);
$giftCard->setAmount($this->faker->randomElement([100, 200, 500, 1000, 10000]));

$this->entityManager->persist($giftCard);
}

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

public function getName(): string
{
return 'gift_card';
}

protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
{
$optionsNode
->children()
->integerNode('amount')->isRequired()->min(0)->end()
->end()
;
}
}
23 changes: 23 additions & 0 deletions src/GiftCard/Form/Type/GiftCardAdminType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Form\Type;

use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class GiftCardAdminType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code', TextType::class)
->add('amount', MoneyType::class, [
'currency' => 'USD',
])
;
}
}
45 changes: 45 additions & 0 deletions src/GiftCard/Grid/AdminGiftCardGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Grid;

use App\GiftCard\Entity\GiftCard;
use Sylius\Bundle\GridBundle\Builder\Action\CreateAction;
use Sylius\Bundle\GridBundle\Builder\Action\DeleteAction;
use Sylius\Bundle\GridBundle\Builder\Action\UpdateAction;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\ItemActionGroup;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\MainActionGroup;
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
use Sylius\Bundle\GridBundle\Builder\Field\TwigField;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;

final class AdminGiftCardGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_admin_gift_card';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addField(StringField::create('code')->setLabel('Code'))
->addField(TwigField::create('amount', 'Admin/GiftCard/_amount.html.twig')->setLabel('Amount'))
->addActionGroup(MainActionGroup::create(
CreateAction::create(),
))
->addActionGroup(ItemActionGroup::create()
->addAction(UpdateAction::create())
->addAction(DeleteAction::create())
);
;
}

public function getResourceClass(): string
{
return GiftCard::class;
}
}
22 changes: 22 additions & 0 deletions src/GiftCard/Menu/AdminMenuListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Menu;

use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;

final class AdminMenuListener
{
public function addGiftCardMenu(MenuBuilderEvent $event): void
{
$menu = $event->getMenu();
$salesSubmenu = $menu->getChild('sales');

$salesSubmenu
->addChild('gift_cards', ['route' => 'app_admin_gift_card_index'])
->setLabel('app.ui.gift_cards')
->setLabelAttribute('icon', 'gift')
;
}
}
31 changes: 31 additions & 0 deletions src/Migrations/Version20240508104628.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240508104628 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE app_gift_card (id INT AUTO_INCREMENT NOT NULL, code VARCHAR(255) NOT NULL, amount INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE app_gift_card');
}
}
1 change: 1 addition & 0 deletions templates/Admin/GiftCard/_amount.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ data|sylius_format_money('USD', app.locale) }}
1 change: 1 addition & 0 deletions translations/messages.en.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app:
ui:
gift_cards: 'Gift Cards'
packagings: 'Packagings'

0 comments on commit afe157e

Please sign in to comment.