commit 28f2cd5d4cdff86ad1b6758198282ba3a8317966
parent fc7f3a190a1d8fc17de9ed45907f06bfeb983390
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 19 Dec 2015 18:07:17 +0100
Externalized manufacturer import, added cli command to import manufacturers
Diffstat:
6 files changed, 167 insertions(+), 92 deletions(-)
diff --git a/src/PartKeepr/SetupBundle/Command/ImportFootprintCommand.php b/src/PartKeepr/SetupBundle/Command/ImportFootprintCommand.php
@@ -1,25 +0,0 @@
-<?php
-namespace PartKeepr\SetupBundle\Command;
-
-use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-
-class ImportFootprintCommand extends ContainerAwareCommand
-{
- public function configure()
- {
- parent::configure();
- $this->setName('partkeepr:setup:import-footprints');
- $this->setDescription("Imports the default PartKeepr footprints");
- }
-
- public function execute(InputInterface $input, OutputInterface $output)
- {
- $return = $this->getContainer()->get("partkeepr.setup.footprint_service")->importFootprints();
-
- $output->writeln(sprintf("%d footprints imported, %d existing footprints skipped", $return["imported"],
- $return["skipped"]));
- }
-}
diff --git a/src/PartKeepr/SetupBundle/Command/ImportFootprintsCommand.php b/src/PartKeepr/SetupBundle/Command/ImportFootprintsCommand.php
@@ -0,0 +1,25 @@
+<?php
+namespace PartKeepr\SetupBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+
+class ImportFootprintsCommand extends ContainerAwareCommand
+{
+ public function configure()
+ {
+ parent::configure();
+ $this->setName('partkeepr:setup:import-footprints');
+ $this->setDescription("Imports the default PartKeepr footprints");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output)
+ {
+ $return = $this->getContainer()->get("partkeepr.setup.footprint_service")->importFootprints();
+
+ $output->writeln(sprintf("%d footprints imported, %d existing footprints skipped", $return["imported"],
+ $return["skipped"]));
+ }
+}
diff --git a/src/PartKeepr/SetupBundle/Command/ImportManufacturersCommand.php b/src/PartKeepr/SetupBundle/Command/ImportManufacturersCommand.php
@@ -0,0 +1,25 @@
+<?php
+namespace PartKeepr\SetupBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+
+class ImportManufacturersCommand extends ContainerAwareCommand
+{
+ public function configure()
+ {
+ parent::configure();
+ $this->setName('partkeepr:setup:import-manufacturers');
+ $this->setDescription("Imports the default PartKeepr manufacturers");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output)
+ {
+ $return = $this->getContainer()->get("partkeepr.setup.manufacturer_service")->importManufacturers();
+
+ $output->writeln(sprintf("%d manufacturers imported, %d existing manufacturers skipped", $return["imported"],
+ $return["skipped"]));
+ }
+}
diff --git a/src/PartKeepr/SetupBundle/Controller/ManufacturerSetupController.php b/src/PartKeepr/SetupBundle/Controller/ManufacturerSetupController.php
@@ -1,20 +1,15 @@
<?php
namespace PartKeepr\SetupBundle\Controller;
-use PartKeepr\ManufacturerBundle\Entity\Manufacturer;
-use PartKeepr\ManufacturerBundle\Entity\ManufacturerICLogo;
-use Symfony\Component\HttpFoundation\File\File;
+
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
-use Symfony\Component\Yaml\Parser;
+
class ManufacturerSetupController extends SetupController
{
- const MANUFACTURER_PATH = "@PartKeeprSetupBundle/Resources/setup-data/manufacturers/";
- const MANUFACTURER_DATA = "manufacturers.yml";
-
/**
* @Route("/setup/_int_create_manufacturers")
*/
@@ -26,26 +21,8 @@ class ManufacturerSetupController extends SetupController
"message" => "Default manufacturers successfully created",
);
- $path = $this->get("kernel")->locateResource(self::MANUFACTURER_PATH.self::MANUFACTURER_DATA);
-
- $skipped = 0;
-
try {
- $yaml = new Parser();
- $data = $yaml->parse(file_get_contents($path));
-
- $entityManager = $this->get("doctrine.orm.default_entity_manager");
-
- foreach ($data as $manufacturerName => $manufacturerData) {
- if ($this->manufacturerExists($manufacturerName)) {
- $skipped++;
- continue;
- }
-
- $this->createManufacturer($manufacturerName, $manufacturerData);
- }
-
- $entityManager->flush();
+ $this->get("partkeepr.setup.manufacturer_service")->importManufacturers();
} catch (\Exception $e) {
$response["success"] = false;
$response["message"] = "Manufacturers creation error";
@@ -57,6 +34,8 @@ class ManufacturerSetupController extends SetupController
/**
* @Route("/setup/createManufacturers")
+ * @param Request $request
+ * @return Response
*/
public function createManufacturersAction(Request $request)
{
@@ -64,45 +43,4 @@ class ManufacturerSetupController extends SetupController
return new Response($response->getContent());
}
-
- protected function createManufacturer($manufacturerName, $manufacturerData)
- {
- $fileService = $this->get("partkeepr_uploadedfile_service");
-
- $manufacturer = new Manufacturer();
- $manufacturer->setName($manufacturerName);
-
- if (array_key_exists("iclogos", $manufacturerData)) {
-
- foreach ($manufacturerData["iclogos"] as $icLogo) {
- $manufacturerIcLogo = new ManufacturerICLogo();
-
- $file = $this->get("kernel")->locateResource(self::MANUFACTURER_PATH.$icLogo);
- $fileService->replaceFromFilesystem($manufacturerIcLogo, new File($file));
-
- $manufacturer->addIcLogo($manufacturerIcLogo);
- }
- }
-
- $this->get("doctrine.orm.default_entity_manager")->persist($manufacturer);
- }
-
-
- /**
- * Checks if the specified manufacturer exists
- *
- * @param string $name The manufacturer name
- */
- protected function manufacturerExists($name)
- {
- $dql = "SELECT COUNT(m) FROM PartKeepr\ManufacturerBundle\Entity\Manufacturer m WHERE m.name = :name";
- $query = $this->get("doctrine.orm.default_entity_manager")->createQuery($dql);
- $query->setParameter("name", $name);
-
- if ($query->getSingleScalarResult() == 0) {
- return false;
- } else {
- return true;
- }
- }
}
diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml
@@ -12,6 +12,12 @@
<argument type="service" id="kernel"/>
</service>
+ <service id="partkeepr.setup.manufacturer_service" class="PartKeepr\SetupBundle\Services\ManufacturerSetupService">
+ <argument type="service" id="doctrine.orm.default_entity_manager" />
+ <argument type="service" id="partkeepr_uploadedfile_service"/>
+ <argument type="service" id="kernel"/>
+ </service>
+
<service id="partkeepr.setup.part_unit_service" class="PartKeepr\SetupBundle\Services\PartUnitSetupService">
<argument type="service" id="doctrine.orm.default_entity_manager" />
</service>
diff --git a/src/PartKeepr/SetupBundle/Services/ManufacturerSetupService.php b/src/PartKeepr/SetupBundle/Services/ManufacturerSetupService.php
@@ -0,0 +1,106 @@
+<?php
+namespace PartKeepr\SetupBundle\Services;
+
+use Doctrine\ORM\EntityManager;
+use PartKeepr\ManufacturerBundle\Entity\Manufacturer;
+use PartKeepr\ManufacturerBundle\Entity\ManufacturerICLogo;
+use PartKeepr\UploadedFileBundle\Services\UploadedFileService;
+use Symfony\Component\HttpFoundation\File\File;
+use Symfony\Component\HttpKernel\KernelInterface;
+use Symfony\Component\Yaml\Parser;
+
+class ManufacturerSetupService
+{
+ const MANUFACTURER_PATH = "@PartKeeprSetupBundle/Resources/setup-data/manufacturers/";
+ const MANUFACTURER_DATA = "manufacturers.yml";
+
+ /**
+ * @var EntityManager
+ */
+ private $entityManager;
+
+ /**
+ * @var UploadedFileService
+ */
+ private $uploadedFileService;
+
+ /**
+ * @var KernelInterface
+ */
+ private $kernel;
+
+ public function __construct(
+ EntityManager $entityManager,
+ UploadedFileService $uploadedFileService,
+ KernelInterface $kernel
+ ) {
+ $this->entityManager = $entityManager;
+ $this->uploadedFileService = $uploadedFileService;
+ $this->kernel = $kernel;
+ }
+
+ public function importManufacturers()
+ {
+ $path = $this->kernel->locateResource(self::MANUFACTURER_PATH.self::MANUFACTURER_DATA);
+
+ $skipped = 0;
+ $count = 0;
+ $yaml = new Parser();
+ $data = $yaml->parse(file_get_contents($path));
+
+
+ foreach ($data as $manufacturerName => $manufacturerData) {
+ if ($this->manufacturerExists($manufacturerName)) {
+ $skipped++;
+ continue;
+ }
+
+ $this->createManufacturer($manufacturerName, $manufacturerData);
+ $count++;
+ }
+
+ $this->entityManager->flush();
+
+ return array("skipped" => $skipped, "imported" => $count);
+ }
+
+ protected function createManufacturer($manufacturerName, $manufacturerData)
+ {
+ $manufacturer = new Manufacturer();
+ $manufacturer->setName($manufacturerName);
+
+ if (array_key_exists("iclogos", $manufacturerData)) {
+
+ foreach ($manufacturerData["iclogos"] as $icLogo) {
+ $manufacturerIcLogo = new ManufacturerICLogo();
+
+ $file = $this->kernel->locateResource(self::MANUFACTURER_PATH.$icLogo);
+ $this->uploadedFileService->replaceFromFilesystem($manufacturerIcLogo, new File($file));
+
+ $manufacturer->addIcLogo($manufacturerIcLogo);
+ }
+ }
+
+ $this->entityManager->persist($manufacturer);
+ }
+
+
+ /**
+ * Checks if the specified manufacturer exists
+ *
+ * @param string $name The manufacturer name
+ * @return true if the manufacturer exists, false otherwise
+ */
+ protected function manufacturerExists($name)
+ {
+ $dql = "SELECT COUNT(m) FROM PartKeepr\ManufacturerBundle\Entity\Manufacturer m WHERE m.name = :name";
+ $query = $this->entityManager->createQuery($dql);
+ $query->setParameter("name", $name);
+
+ if ($query->getSingleScalarResult() == 0) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+}