partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit d3825a3d9bce225f429a7556c37362643f32e937
parent 28f2cd5d4cdff86ad1b6758198282ba3a8317966
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 19 Dec 2015 18:16:14 +0100

Externalized si prefix importer, added CLi command to import si prefixes, added phpdoc for manufacturers import

Diffstat:
Asrc/PartKeepr/SetupBundle/Command/ImportSiPrefixesCommand.php | 25+++++++++++++++++++++++++
Msrc/PartKeepr/SetupBundle/Controller/SiPrefixSetupController.php | 45++++++---------------------------------------
Msrc/PartKeepr/SetupBundle/Resources/config/services.xml | 21++++++++++++++-------
Msrc/PartKeepr/SetupBundle/Services/ManufacturerSetupService.php | 6++++++
Asrc/PartKeepr/SetupBundle/Services/SiPrefixSetupService.php | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 137 insertions(+), 46 deletions(-)

diff --git a/src/PartKeepr/SetupBundle/Command/ImportSiPrefixesCommand.php b/src/PartKeepr/SetupBundle/Command/ImportSiPrefixesCommand.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 ImportSiPrefixesCommand extends ContainerAwareCommand +{ + public function configure() + { + parent::configure(); + $this->setName('partkeepr:setup:import-si-prefixes'); + $this->setDescription("Imports or updates the default PartKeepr si prefixes"); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $return = $this->getContainer()->get("partkeepr.setup.si_prefix_service")->importSiPrefixes(); + + $output->writeln(sprintf("%d SI prefixes imported, %d existing SI Prefixes updated", $return["imported"], + $return["updated"])); + } +} diff --git a/src/PartKeepr/SetupBundle/Controller/SiPrefixSetupController.php b/src/PartKeepr/SetupBundle/Controller/SiPrefixSetupController.php @@ -3,18 +3,15 @@ namespace PartKeepr\SetupBundle\Controller; -use PartKeepr\SiPrefixBundle\Entity\SiPrefix; + 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 SiPrefixSetupController extends SetupController { - const SIPREFIX_PATH = "@PartKeeprSetupBundle/Resources/setup-data/"; - const SIPREFIX_DATA = "siprefixes.yml"; - /** * @Route("/setup/_int_create_si_prefixes") */ @@ -26,30 +23,8 @@ class SiPrefixSetupController extends SetupController "message" => "SI Prefixes successfully created/updated", ); - $path = $this->get("kernel")->locateResource(self::SIPREFIX_PATH.self::SIPREFIX_DATA); - try { - $yaml = new Parser(); - $data = $yaml->parse(file_get_contents($path)); - - $entityManager = $this->get("doctrine.orm.default_entity_manager"); - - foreach ($data as $prefixName => $prefixData) { - $prefix = $this->getSiPrefix($prefixName); - - if ($prefix === null) { - $prefix = new SiPrefix(); - $prefix->setPrefix($prefixName); - $entityManager->persist($prefix); - } - - $prefix->setExponent($prefixData["exponent"]); - $prefix->setSymbol($prefixData["symbol"]); - $prefix->setBase($prefixData["base"]); - - - } - $entityManager->flush(); + $this->get("partkeepr.setup.si_prefix_service")->importSiPrefixes(); } catch (\Exception $e) { $response["success"] = false; $response["message"] = "SI Prefix creation error"; @@ -61,6 +36,8 @@ class SiPrefixSetupController extends SetupController /** * @Route("/setup/createSiPrefixes") + * @param Request $request + * @return Response */ public function createSiPrefixesAction(Request $request) { @@ -69,15 +46,5 @@ class SiPrefixSetupController extends SetupController return new Response($response->getContent()); } - /** - * Finds an SI Prefix by name - * - * @param string $name The SI Prefix name - * @return SiPrefix|null - */ - protected function getSiPrefix($name) - { - $repository = $this->get("doctrine.orm.default_entity_manager")->getRepository("PartKeeprSiPrefixBundle:SiPrefix"); - return $repository->findOneBy(array("prefix" => $name)); - } + } diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml @@ -1,25 +1,32 @@ <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="partkeepr.setup.footprint_service" class="PartKeepr\SetupBundle\Services\FootprintSetupService"> - <argument type="service" id="doctrine.orm.default_entity_manager" /> - <argument type="service" id="partkeepr.footprint.category_service" /> + <argument type="service" id="doctrine.orm.default_entity_manager"/> + <argument type="service" id="partkeepr.footprint.category_service"/> <argument type="service" id="partkeepr_uploadedfile_service"/> <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" /> + <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" /> + <argument type="service" id="doctrine.orm.default_entity_manager"/> </service> + + <service id="partkeepr.setup.si_prefix_service" class="PartKeepr\SetupBundle\Services\SiPrefixSetupService"> + <argument id="doctrine.orm.default_entity_manager" type="service"/> + <argument id="kernel" type="service"/> + </service> + </services> </container> diff --git a/src/PartKeepr/SetupBundle/Services/ManufacturerSetupService.php b/src/PartKeepr/SetupBundle/Services/ManufacturerSetupService.php @@ -39,6 +39,11 @@ class ManufacturerSetupService $this->kernel = $kernel; } + /** + * Imports manufacturers. + * + * @return array An array with the keys "skipped" and "imported" which contain the number of manufacturers skipped and imported + */ public function importManufacturers() { $path = $this->kernel->locateResource(self::MANUFACTURER_PATH.self::MANUFACTURER_DATA); @@ -89,6 +94,7 @@ class ManufacturerSetupService * Checks if the specified manufacturer exists * * @param string $name The manufacturer name + * * @return true if the manufacturer exists, false otherwise */ protected function manufacturerExists($name) diff --git a/src/PartKeepr/SetupBundle/Services/SiPrefixSetupService.php b/src/PartKeepr/SetupBundle/Services/SiPrefixSetupService.php @@ -0,0 +1,86 @@ +<?php +namespace PartKeepr\SetupBundle\Services; + + +use Doctrine\ORM\EntityManager; +use PartKeepr\SiPrefixBundle\Entity\SiPrefix; +use Symfony\Component\HttpKernel\KernelInterface; +use Symfony\Component\Yaml\Parser; + +class SiPrefixSetupService +{ + const SIPREFIX_PATH = "@PartKeeprSetupBundle/Resources/setup-data/"; + const SIPREFIX_DATA = "siprefixes.yml"; + + /** + * @var EntityManager + */ + private $entityManager; + + /** + * @var KernelInterface + */ + private $kernel; + + /** + * SiPrefixSetupService constructor. + * + * @param EntityManager $entityManager + * @param KernelInterface $kernel + */ + public function __construct(EntityManager $entityManager, KernelInterface $kernel) + { + $this->entityManager = $entityManager; + $this->kernel = $kernel; + } + + /** + * Imports or updates the existing si prefixes. + * + * @return array An array with the keys "skipped" and "imported" which contain the number of si prefixes skipped and imported + */ + public function importSiPrefixes() + { + $path = $this->kernel->locateResource(self::SIPREFIX_PATH.self::SIPREFIX_DATA); + + $yaml = new Parser(); + $data = $yaml->parse(file_get_contents($path)); + + $count = 0; + $updated = 0; + + foreach ($data as $prefixName => $prefixData) { + $prefix = $this->getSiPrefix($prefixName); + + if ($prefix === null) { + $prefix = new SiPrefix(); + $prefix->setPrefix($prefixName); + $this->entityManager->persist($prefix); + $count++; + } + + $prefix->setExponent($prefixData["exponent"]); + $prefix->setSymbol($prefixData["symbol"]); + $prefix->setBase($prefixData["base"]); + + $updated++; + } + $this->entityManager->flush(); + + return array("updated" => $updated-$count, "imported" => $count); + } + + /** + * Finds an SI Prefix by name + * + * @param string $name The SI Prefix name + * + * @return SiPrefix|null + */ + protected function getSiPrefix($name) + { + $repository = $this->entityManager->getRepository("PartKeeprSiPrefixBundle:SiPrefix"); + + return $repository->findOneBy(array("prefix" => $name)); + } +}