| @@ -1,57 +0,0 @@ | |||||
| <?php | |||||
| declare(strict_types=1); | |||||
| namespace DoctrineMigrations; | |||||
| use Doctrine\DBAL\Schema\Schema; | |||||
| use Doctrine\Migrations\AbstractMigration; | |||||
| /** | |||||
| * Auto-generated Migration: Please modify to your needs! | |||||
| */ | |||||
| final class Version20240304164555 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 comment (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, posting_id INT NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_9474526C7E3C61F9 (owner_id), INDEX IDX_9474526C9AE985F6 (posting_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('CREATE TABLE contact (id INT AUTO_INCREMENT NOT NULL, partner_id INT NOT NULL, image_id INT DEFAULT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, birthday DATE DEFAULT NULL, position VARCHAR(255) DEFAULT NULL, phone VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_4C62E6389393F8FE (partner_id), INDEX IDX_4C62E6383DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('CREATE TABLE media_object (id INT AUTO_INCREMENT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('CREATE TABLE partner (id INT AUTO_INCREMENT NOT NULL, logo_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, street VARCHAR(255) DEFAULT NULL, street_no VARCHAR(255) DEFAULT NULL, zip VARCHAR(255) DEFAULT NULL, city VARCHAR(255) DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, website VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_312B3E16F98F144A (logo_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('CREATE TABLE posting (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, partner_id INT NOT NULL, contact_id INT DEFAULT NULL, headline VARCHAR(255) NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_BD275D737E3C61F9 (owner_id), INDEX IDX_BD275D739393F8FE (partner_id), INDEX IDX_BD275D73E7A1254A (contact_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('CREATE TABLE `user` (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, roles JSON NOT NULL COMMENT \'(DC2Type:json)\', password VARCHAR(255) NOT NULL, active TINYINT(1) NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C7E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id)'); | |||||
| $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C9AE985F6 FOREIGN KEY (posting_id) REFERENCES posting (id)'); | |||||
| $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6389393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); | |||||
| $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6383DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id) ON DELETE SET NULL'); | |||||
| $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16F98F144A FOREIGN KEY (logo_id) REFERENCES media_object (id) ON DELETE SET NULL'); | |||||
| $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D737E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id)'); | |||||
| $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D739393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); | |||||
| $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D73E7A1254A FOREIGN KEY (contact_id) REFERENCES contact (id)'); | |||||
| } | |||||
| public function down(Schema $schema): void | |||||
| { | |||||
| // this down() migration is auto-generated, please modify it to your needs | |||||
| $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C7E3C61F9'); | |||||
| $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C9AE985F6'); | |||||
| $this->addSql('ALTER TABLE contact DROP FOREIGN KEY FK_4C62E6389393F8FE'); | |||||
| $this->addSql('ALTER TABLE contact DROP FOREIGN KEY FK_4C62E6383DA5256D'); | |||||
| $this->addSql('ALTER TABLE partner DROP FOREIGN KEY FK_312B3E16F98F144A'); | |||||
| $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D737E3C61F9'); | |||||
| $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D739393F8FE'); | |||||
| $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D73E7A1254A'); | |||||
| $this->addSql('DROP TABLE comment'); | |||||
| $this->addSql('DROP TABLE contact'); | |||||
| $this->addSql('DROP TABLE media_object'); | |||||
| $this->addSql('DROP TABLE partner'); | |||||
| $this->addSql('DROP TABLE posting'); | |||||
| $this->addSql('DROP TABLE `user`'); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,39 @@ | |||||
| <?php | |||||
| declare(strict_types=1); | |||||
| namespace DoctrineMigrations; | |||||
| use Doctrine\DBAL\Schema\Schema; | |||||
| use Doctrine\Migrations\AbstractMigration; | |||||
| /** | |||||
| * Auto-generated Migration: Please modify to your needs! | |||||
| */ | |||||
| final class Version20240305152901 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 product (id INT AUTO_INCREMENT NOT NULL, image_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D34A04AD3DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); | |||||
| $this->addSql('ALTER TABLE product ADD CONSTRAINT FK_D34A04AD3DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id)'); | |||||
| $this->addSql('ALTER TABLE user ADD image_id INT DEFAULT NULL'); | |||||
| $this->addSql('ALTER TABLE user ADD CONSTRAINT FK_8D93D6493DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id)'); | |||||
| $this->addSql('CREATE INDEX IDX_8D93D6493DA5256D ON user (image_id)'); | |||||
| } | |||||
| public function down(Schema $schema): void | |||||
| { | |||||
| // this down() migration is auto-generated, please modify it to your needs | |||||
| $this->addSql('ALTER TABLE product DROP FOREIGN KEY FK_D34A04AD3DA5256D'); | |||||
| $this->addSql('DROP TABLE product'); | |||||
| $this->addSql('ALTER TABLE `user` DROP FOREIGN KEY FK_8D93D6493DA5256D'); | |||||
| $this->addSql('DROP INDEX IDX_8D93D6493DA5256D ON `user`'); | |||||
| $this->addSql('ALTER TABLE `user` DROP image_id'); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,69 @@ | |||||
| <?php | |||||
| /** | |||||
| * @author Daniel Knudsen <d.knudsen@spawntree.de> | |||||
| * @date 12.12.23 | |||||
| */ | |||||
| namespace App\ApiResource; | |||||
| use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; | |||||
| use ApiPlatform\Doctrine\Orm\State\Options; | |||||
| use ApiPlatform\Metadata\ApiFilter; | |||||
| use ApiPlatform\Metadata\ApiProperty; | |||||
| use ApiPlatform\Metadata\ApiResource; | |||||
| use App\Entity\MediaObject; | |||||
| use App\Entity\Product; | |||||
| use App\State\EntityClassDtoStateProcessor; | |||||
| use App\State\EntityToDtoStateProvider; | |||||
| use ApiPlatform\Metadata\Delete; | |||||
| use ApiPlatform\Metadata\Get; | |||||
| use ApiPlatform\Metadata\GetCollection; | |||||
| use ApiPlatform\Metadata\Patch; | |||||
| use ApiPlatform\Metadata\Post; | |||||
| use Symfony\Component\Validator\Constraints\NotBlank; | |||||
| #[ApiResource( | |||||
| shortName: 'Product', | |||||
| operations: [ | |||||
| new Get( | |||||
| security: 'is_granted("ROLE_USER")' | |||||
| ), | |||||
| new GetCollection(), | |||||
| new Post( | |||||
| security: 'is_granted("ROLE_USER")', | |||||
| ), | |||||
| new Patch( | |||||
| security: 'is_granted("ROLE_USER")', | |||||
| ), | |||||
| new Delete( | |||||
| security: 'is_granted("ROLE_ADMIN")', | |||||
| ) | |||||
| ], | |||||
| order: ['name' => 'ASC'], | |||||
| security: 'is_granted("ROLE_USER")', | |||||
| provider: EntityToDtoStateProvider::class, | |||||
| processor: EntityClassDtoStateProcessor::class, | |||||
| stateOptions: new Options(entityClass: Product::class) | |||||
| )] | |||||
| #[ApiFilter(OrderFilter::class, properties: ['name'], arguments: ['orderParameterName' => 'order'])] | |||||
| class ProductApi | |||||
| { | |||||
| #[ApiProperty(readable: false, writable: false, identifier: true)] | |||||
| public ?int $id = null; | |||||
| #[NotBlank] | |||||
| public string $name; | |||||
| public ?string $description = null; | |||||
| public ?MediaObject $image = null; | |||||
| #[ApiProperty(writable: false)] | |||||
| public ?string $imageUrl = null; | |||||
| public ?\DateTimeImmutable $createdAt = null; | |||||
| } | |||||
| @@ -5,9 +5,12 @@ namespace App\DataFixtures; | |||||
| use App\Factory\CommentFactory; | use App\Factory\CommentFactory; | ||||
| use App\Factory\ContactFactory; | use App\Factory\ContactFactory; | ||||
| use App\Factory\MediaObjectLogoFactory; | use App\Factory\MediaObjectLogoFactory; | ||||
| use App\Factory\MediaObjectProfileFactory; | |||||
| use App\Factory\MediaObjectProductFactory; | |||||
| use App\Factory\MediaObjectContactFactory; | |||||
| use App\Factory\MediaObjectUserFactory; | |||||
| use App\Factory\PartnerFactory; | use App\Factory\PartnerFactory; | ||||
| use App\Factory\PostingFactory; | use App\Factory\PostingFactory; | ||||
| use App\Factory\ProductFactory; | |||||
| use App\Factory\UserFactory; | use App\Factory\UserFactory; | ||||
| use Doctrine\Bundle\FixturesBundle\Fixture; | use Doctrine\Bundle\FixturesBundle\Fixture; | ||||
| use Doctrine\Persistence\ObjectManager; | use Doctrine\Persistence\ObjectManager; | ||||
| @@ -34,6 +37,7 @@ class AppFixtures extends Fixture | |||||
| } | } | ||||
| } | } | ||||
| MediaObjectUserFactory::createMany(20); | |||||
| $adminD = UserFactory::createOne( | $adminD = UserFactory::createOne( | ||||
| [ | [ | ||||
| 'email' => 'd.knudsen@spawntree.de', | 'email' => 'd.knudsen@spawntree.de', | ||||
| @@ -57,9 +61,11 @@ class AppFixtures extends Fixture | |||||
| UserFactory::createMany(10); | UserFactory::createMany(10); | ||||
| MediaObjectLogoFactory::createMany(50); | MediaObjectLogoFactory::createMany(50); | ||||
| PartnerFactory::createMany(100); | PartnerFactory::createMany(100); | ||||
| MediaObjectProfileFactory::createMany(50); | |||||
| MediaObjectContactFactory::createMany(50); | |||||
| ContactFactory::createMany(200); | ContactFactory::createMany(200); | ||||
| PostingFactory::createMany(200); | PostingFactory::createMany(200); | ||||
| CommentFactory::createMany(300); | CommentFactory::createMany(300); | ||||
| MediaObjectProductFactory::createMany(50); | |||||
| ProductFactory::createMany(100); | |||||
| } | } | ||||
| } | } | ||||
| @@ -187,4 +187,21 @@ class FakeValues | |||||
| "Systemadministrator", "Vertriebsingenieur", "Produktionsplaner", "Personalbeschaffer", | "Systemadministrator", "Vertriebsingenieur", "Produktionsplaner", "Personalbeschaffer", | ||||
| "Content Creator", "Bürokoordinator" | "Content Creator", "Bürokoordinator" | ||||
| ]; | ]; | ||||
| const PRODUCTS = [ | |||||
| 'Oxidanol-X', 'Quantum Acidite', 'NeuroSolvent', 'HydroMystique', 'Luminaflux', 'Nanoglow Serum', 'Etherium Catalyst', 'BioSol ExoBlend', | |||||
| 'CryoSynth Elixir', 'NebulaVapour', 'Magnaflux Solvent', 'SynthoQuanta', 'QuantumSolve-X', 'PlasmaFusion Serum', 'ElectroLyte-X', 'SynthoBlast Acid', | |||||
| 'MegaMelt Radiant', 'Ionic Infusion X', 'QuantumBond Solvent', 'Molecular Surge', 'Etherium Enhancer', 'BioCryo Synthase', 'MagmaMyst Serum', | |||||
| 'HydroMyst Elixir', 'NeuroQuell Acid', 'PlasmaGlow Catalyst', 'NanoSizzle Blend', 'QuantumVortex X', 'VitaSynth Serum', 'CryoFusion Acid', | |||||
| 'RadiantQuanta Elixir', 'QuantumSurge Acid', 'NebulaRadiance Blend', 'Luminex Catalyst', 'ElectroPulse-X', 'CryoWave Serum', 'SynthoFlare Acid', | |||||
| 'Nanoglow Elixir', 'QuantumSpark Blend', 'MagnaFusion Catalyst', 'ElectroQuanta Acid', 'LuminarX Serum', 'NeuroWave-X', 'CryoSpark Acid', | |||||
| 'PlasmaQuanta Elixir', 'MegaCryo Catalyst', 'IonicQuell Blend', 'QuantumSizzle-X', 'NeuroRadiance Acid', 'NebulaFusion Serum', 'HydroQuanta X', | |||||
| 'RadiantFlare Blend', 'BioMyst Acid', 'SynthoVortex Serum', 'QuantumSurge-X', 'PlasmaQuell Blend', 'MolecularGlow Catalyst', 'Luminex Serum', | |||||
| 'ElectroWave Acid', 'CryoMyst Elixir', 'NeuroFusion Blend', 'NanoSizzle Acid', 'QuantumPulse Serum', 'MegaFlare-X', 'PlasmaRadiance Acid', | |||||
| 'SynthoQuanta Elixir', 'NebulaCryo Blend', 'HydroQuell Acid', 'RadiantFusion Serum', 'ElectroCryo-X', 'MolecularQuanta Acid', 'BioSpark Elixir', | |||||
| 'QuantumRadiance Blend', 'NanoFlare Acid', 'MagnaQuanta Serum', 'Luminex Catalyst-X', 'CryoFusion Elixir', 'NeuroQuell Blend', 'PlasmaMyst Acid', | |||||
| 'SynthoRadiance Serum', 'QuantumVortex-X', 'NebulaSpark Blend', 'HydroGlow Catalyst', 'RadiantQuanta Acid', 'ElectroMyst Elixir', 'MegaQuell Serum', | |||||
| 'CryoVortex Acid', 'NanoRadiance Blend', 'QuantumFusion Serum', 'SynthoFlare-X', 'PlasmaQuell Acid', 'MolecularGlow Blend', 'Luminex Catalyst-X', | |||||
| 'ElectroWave Serum', 'CryoMyst Acid', 'NeuroFusion Elixir', 'NanoSizzle Blend-X', 'QuantumPulse Acid', 'RadiantFlare Serum', 'BioQuanta Catalyst', | |||||
| ]; | |||||
| } | } | ||||
| @@ -55,7 +55,8 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; | |||||
| // controller: DeleteMediaObjectAction::class | // controller: DeleteMediaObjectAction::class | ||||
| ), | ), | ||||
| ], | ], | ||||
| normalizationContext: ['groups' => ['media_object:read']] | |||||
| normalizationContext: ['groups' => ['media_object:read']], | |||||
| security: 'is_granted("ROLE_USER")', | |||||
| )] | )] | ||||
| class MediaObject | class MediaObject | ||||
| { | { | ||||
| @@ -0,0 +1,79 @@ | |||||
| <?php | |||||
| namespace App\Entity; | |||||
| use App\Repository\ProductRepository; | |||||
| use Doctrine\DBAL\Types\Types; | |||||
| use Doctrine\ORM\Mapping as ORM; | |||||
| #[ORM\Entity(repositoryClass: ProductRepository::class)] | |||||
| class Product | |||||
| { | |||||
| #[ORM\Id] | |||||
| #[ORM\GeneratedValue] | |||||
| #[ORM\Column] | |||||
| private ?int $id = null; | |||||
| #[ORM\Column(length: 255)] | |||||
| private ?string $name = null; | |||||
| #[ORM\Column(type: Types::TEXT, nullable: true)] | |||||
| private ?string $description = null; | |||||
| #[ORM\ManyToOne] | |||||
| private ?MediaObject $image = null; | |||||
| #[ORM\Column] | |||||
| private ?\DateTimeImmutable $createdAt = null; | |||||
| public function __construct() | |||||
| { | |||||
| $this->createdAt = new \DateTimeImmutable(); | |||||
| } | |||||
| public function getId(): ?int | |||||
| { | |||||
| return $this->id; | |||||
| } | |||||
| public function getName(): ?string | |||||
| { | |||||
| return $this->name; | |||||
| } | |||||
| public function setName(string $name): static | |||||
| { | |||||
| $this->name = $name; | |||||
| return $this; | |||||
| } | |||||
| public function getDescription(): ?string | |||||
| { | |||||
| return $this->description; | |||||
| } | |||||
| public function setDescription(?string $description): static | |||||
| { | |||||
| $this->description = $description; | |||||
| return $this; | |||||
| } | |||||
| public function getImage(): ?MediaObject | |||||
| { | |||||
| return $this->image; | |||||
| } | |||||
| public function setImage(?MediaObject $image): static | |||||
| { | |||||
| $this->image = $image; | |||||
| return $this; | |||||
| } | |||||
| public function getCreatedAt(): ?\DateTimeImmutable | |||||
| { | |||||
| return $this->createdAt; | |||||
| } | |||||
| } | |||||
| @@ -27,6 +27,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface | |||||
| #[ORM\Column(length: 255)] | #[ORM\Column(length: 255)] | ||||
| private ?string $lastName = null; | private ?string $lastName = null; | ||||
| #[ORM\ManyToOne] | |||||
| private ?MediaObject $image = null; | |||||
| #[ORM\Column] | #[ORM\Column] | ||||
| private array $roles = []; | private array $roles = []; | ||||
| @@ -151,6 +154,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface | |||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getImage(): ?MediaObject | |||||
| { | |||||
| return $this->image; | |||||
| } | |||||
| public function setImage(?MediaObject $image): void | |||||
| { | |||||
| $this->image = $image; | |||||
| } | |||||
| public function getCreatedAt(): ?\DateTimeImmutable | public function getCreatedAt(): ?\DateTimeImmutable | ||||
| { | { | ||||
| return $this->createdAt; | return $this->createdAt; | ||||
| @@ -55,7 +55,7 @@ final class ContactFactory extends ModelFactory | |||||
| 'phone' => self::faker()->phoneNumber(), | 'phone' => self::faker()->phoneNumber(), | ||||
| 'email' => self::faker()->email(), | 'email' => self::faker()->email(), | ||||
| 'partner' => PartnerFactory::random(), | 'partner' => PartnerFactory::random(), | ||||
| 'image' => MediaObjectProfileFactory::random() | |||||
| 'image' => MediaObjectContactFactory::random() | |||||
| ]; | ]; | ||||
| } | } | ||||
| @@ -31,7 +31,7 @@ use Zenstruck\Foundry\RepositoryProxy; | |||||
| * @method static MediaObject[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | * @method static MediaObject[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | ||||
| * @method static MediaObject[]|Proxy[] randomSet(int $number, array $attributes = []) | * @method static MediaObject[]|Proxy[] randomSet(int $number, array $attributes = []) | ||||
| */ | */ | ||||
| final class MediaObjectProfileFactory extends ModelFactory | |||||
| final class MediaObjectContactFactory extends ModelFactory | |||||
| { | { | ||||
| /** | /** | ||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | ||||
| @@ -0,0 +1,83 @@ | |||||
| <?php | |||||
| namespace App\Factory; | |||||
| use App\Entity\MediaObject; | |||||
| use Doctrine\ORM\EntityRepository; | |||||
| use Symfony\Component\HttpFoundation\File\File; | |||||
| use Symfony\Component\HttpFoundation\File\UploadedFile; | |||||
| use Symfony\Component\HttpKernel\KernelInterface; | |||||
| use Vich\UploaderBundle\FileAbstraction\ReplacingFile; | |||||
| use Zenstruck\Foundry\ModelFactory; | |||||
| use Zenstruck\Foundry\Proxy; | |||||
| use Zenstruck\Foundry\RepositoryProxy; | |||||
| /** | |||||
| * @extends ModelFactory<MediaObject> | |||||
| * | |||||
| * @method MediaObject|Proxy create(array|callable $attributes = []) | |||||
| * @method static MediaObject|Proxy createOne(array $attributes = []) | |||||
| * @method static MediaObject|Proxy find(object|array|mixed $criteria) | |||||
| * @method static MediaObject|Proxy findOrCreate(array $attributes) | |||||
| * @method static MediaObject|Proxy first(string $sortedField = 'id') | |||||
| * @method static MediaObject|Proxy last(string $sortedField = 'id') | |||||
| * @method static MediaObject|Proxy random(array $attributes = []) | |||||
| * @method static MediaObject|Proxy randomOrCreate(array $attributes = []) | |||||
| * @method static EntityRepository|RepositoryProxy repository() | |||||
| * @method static MediaObject[]|Proxy[] all() | |||||
| * @method static MediaObject[]|Proxy[] createMany(int $number, array|callable $attributes = []) | |||||
| * @method static MediaObject[]|Proxy[] createSequence(iterable|callable $sequence) | |||||
| * @method static MediaObject[]|Proxy[] findBy(array $attributes) | |||||
| * @method static MediaObject[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | |||||
| * @method static MediaObject[]|Proxy[] randomSet(int $number, array $attributes = []) | |||||
| */ | |||||
| final class MediaObjectProductFactory extends ModelFactory | |||||
| { | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | |||||
| * | |||||
| * @todo inject services if required | |||||
| */ | |||||
| public function __construct( | |||||
| private KernelInterface $appKernel | |||||
| ) | |||||
| { | |||||
| parent::__construct(); | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | |||||
| * | |||||
| * @todo add your default values here | |||||
| */ | |||||
| protected function getDefaults(): array | |||||
| { | |||||
| $projectRoot = $this->appKernel->getProjectDir(); | |||||
| $folderPath = $projectRoot . '/src/DataFixtures/images/products/'; | |||||
| $files = glob($folderPath . '*.*'); | |||||
| $randomFile = null; | |||||
| if ($files !== false && count($files) > 0) { | |||||
| $randomFile = $files[array_rand($files)]; | |||||
| } | |||||
| return [ | |||||
| 'file' => new ReplacingFile($randomFile) | |||||
| ]; | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | |||||
| */ | |||||
| protected function initialize(): self | |||||
| { | |||||
| return $this | |||||
| // ->afterInstantiate(function(MediaObject $mediaObject): void {}) | |||||
| ; | |||||
| } | |||||
| protected static function getClass(): string | |||||
| { | |||||
| return MediaObject::class; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,83 @@ | |||||
| <?php | |||||
| namespace App\Factory; | |||||
| use App\Entity\MediaObject; | |||||
| use Doctrine\ORM\EntityRepository; | |||||
| use Symfony\Component\HttpFoundation\File\File; | |||||
| use Symfony\Component\HttpFoundation\File\UploadedFile; | |||||
| use Symfony\Component\HttpKernel\KernelInterface; | |||||
| use Vich\UploaderBundle\FileAbstraction\ReplacingFile; | |||||
| use Zenstruck\Foundry\ModelFactory; | |||||
| use Zenstruck\Foundry\Proxy; | |||||
| use Zenstruck\Foundry\RepositoryProxy; | |||||
| /** | |||||
| * @extends ModelFactory<MediaObject> | |||||
| * | |||||
| * @method MediaObject|Proxy create(array|callable $attributes = []) | |||||
| * @method static MediaObject|Proxy createOne(array $attributes = []) | |||||
| * @method static MediaObject|Proxy find(object|array|mixed $criteria) | |||||
| * @method static MediaObject|Proxy findOrCreate(array $attributes) | |||||
| * @method static MediaObject|Proxy first(string $sortedField = 'id') | |||||
| * @method static MediaObject|Proxy last(string $sortedField = 'id') | |||||
| * @method static MediaObject|Proxy random(array $attributes = []) | |||||
| * @method static MediaObject|Proxy randomOrCreate(array $attributes = []) | |||||
| * @method static EntityRepository|RepositoryProxy repository() | |||||
| * @method static MediaObject[]|Proxy[] all() | |||||
| * @method static MediaObject[]|Proxy[] createMany(int $number, array|callable $attributes = []) | |||||
| * @method static MediaObject[]|Proxy[] createSequence(iterable|callable $sequence) | |||||
| * @method static MediaObject[]|Proxy[] findBy(array $attributes) | |||||
| * @method static MediaObject[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | |||||
| * @method static MediaObject[]|Proxy[] randomSet(int $number, array $attributes = []) | |||||
| */ | |||||
| final class MediaObjectUserFactory extends ModelFactory | |||||
| { | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | |||||
| * | |||||
| * @todo inject services if required | |||||
| */ | |||||
| public function __construct( | |||||
| private KernelInterface $appKernel | |||||
| ) | |||||
| { | |||||
| parent::__construct(); | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | |||||
| * | |||||
| * @todo add your default values here | |||||
| */ | |||||
| protected function getDefaults(): array | |||||
| { | |||||
| $projectRoot = $this->appKernel->getProjectDir(); | |||||
| $folderPath = $projectRoot . '/src/DataFixtures/images/users/'; | |||||
| $files = glob($folderPath . '*.*'); | |||||
| $randomFile = null; | |||||
| if ($files !== false && count($files) > 0) { | |||||
| $randomFile = $files[array_rand($files)]; | |||||
| } | |||||
| return [ | |||||
| 'file' => new ReplacingFile($randomFile) | |||||
| ]; | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | |||||
| */ | |||||
| protected function initialize(): self | |||||
| { | |||||
| return $this | |||||
| // ->afterInstantiate(function(MediaObject $mediaObject): void {}) | |||||
| ; | |||||
| } | |||||
| protected static function getClass(): string | |||||
| { | |||||
| return MediaObject::class; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,71 @@ | |||||
| <?php | |||||
| namespace App\Factory; | |||||
| use App\DataFixtures\FakeValues; | |||||
| use App\Entity\Product; | |||||
| use App\Repository\ProductRepository; | |||||
| use Zenstruck\Foundry\ModelFactory; | |||||
| use Zenstruck\Foundry\Proxy; | |||||
| use Zenstruck\Foundry\RepositoryProxy; | |||||
| /** | |||||
| * @extends ModelFactory<Product> | |||||
| * | |||||
| * @method Product|Proxy create(array|callable $attributes = []) | |||||
| * @method static Product|Proxy createOne(array $attributes = []) | |||||
| * @method static Product|Proxy find(object|array|mixed $criteria) | |||||
| * @method static Product|Proxy findOrCreate(array $attributes) | |||||
| * @method static Product|Proxy first(string $sortedField = 'id') | |||||
| * @method static Product|Proxy last(string $sortedField = 'id') | |||||
| * @method static Product|Proxy random(array $attributes = []) | |||||
| * @method static Product|Proxy randomOrCreate(array $attributes = []) | |||||
| * @method static ProductRepository|RepositoryProxy repository() | |||||
| * @method static Product[]|Proxy[] all() | |||||
| * @method static Product[]|Proxy[] createMany(int $number, array|callable $attributes = []) | |||||
| * @method static Product[]|Proxy[] createSequence(iterable|callable $sequence) | |||||
| * @method static Product[]|Proxy[] findBy(array $attributes) | |||||
| * @method static Product[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | |||||
| * @method static Product[]|Proxy[] randomSet(int $number, array $attributes = []) | |||||
| */ | |||||
| final class ProductFactory extends ModelFactory | |||||
| { | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services | |||||
| * | |||||
| * @todo inject services if required | |||||
| */ | |||||
| public function __construct() | |||||
| { | |||||
| parent::__construct(); | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories | |||||
| * | |||||
| * @todo add your default values here | |||||
| */ | |||||
| protected function getDefaults(): array | |||||
| { | |||||
| return [ | |||||
| 'name' => self::faker()->randomElement(FakeValues::PRODUCTS), | |||||
| 'description' => self::faker()->text(), | |||||
| 'image' => MediaObjectProductFactory::createOne(), | |||||
| ]; | |||||
| } | |||||
| /** | |||||
| * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | |||||
| */ | |||||
| protected function initialize(): self | |||||
| { | |||||
| return $this | |||||
| // ->afterInstantiate(function(Product $product): void {}) | |||||
| ; | |||||
| } | |||||
| protected static function getClass(): string | |||||
| { | |||||
| return Product::class; | |||||
| } | |||||
| } | |||||