partkeepr

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

commit fc7f3a190a1d8fc17de9ed45907f06bfeb983390
parent 1eade4e1cca9c8b39ff3b0adef2dc7c3c33e6228
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat, 19 Dec 2015 17:59:28 +0100

Externalized part unit setup to an own service, added cli command to setup the default part unit

Diffstat:
Asrc/PartKeepr/SetupBundle/Command/SetupDefaultPartUnitCommand.php | 28++++++++++++++++++++++++++++
Msrc/PartKeepr/SetupBundle/Controller/PartUnitSetupController.php | 20+++-----------------
Msrc/PartKeepr/SetupBundle/Resources/config/services.xml | 4++++
Asrc/PartKeepr/SetupBundle/Services/PartUnitSetupService.php | 47+++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 82 insertions(+), 17 deletions(-)

diff --git a/src/PartKeepr/SetupBundle/Command/SetupDefaultPartUnitCommand.php b/src/PartKeepr/SetupBundle/Command/SetupDefaultPartUnitCommand.php @@ -0,0 +1,28 @@ +<?php +namespace PartKeepr\SetupBundle\Command; + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + + +class SetupDefaultPartUnitCommand extends ContainerAwareCommand +{ + public function configure() + { + parent::configure(); + $this->setName('partkeepr:setup:part-unit'); + $this->setDescription("Sets up the default part unit"); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $return = $this->getContainer()->get("partkeepr.setup.part_unit_service")->setupDefaultPartUnit(); + + if ($return) { + $output->writeln("Default part unit created"); + } else { + $output->writeln("Default part unit not created as it already exists"); + } + } +} diff --git a/src/PartKeepr/SetupBundle/Controller/PartUnitSetupController.php b/src/PartKeepr/SetupBundle/Controller/PartUnitSetupController.php @@ -1,7 +1,6 @@ <?php namespace PartKeepr\SetupBundle\Controller; -use PartKeepr\PartBundle\Entity\PartMeasurementUnit; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -11,6 +10,8 @@ class PartUnitSetupController extends SetupController { /** * @Route("/setup/createPartUnits") + * @param Request $request + * @return Response */ public function createPartUnitsAction(Request $request) { @@ -31,22 +32,7 @@ class PartUnitSetupController extends SetupController ); try { - $entityManager = $this->get("doctrine.orm.default_entity_manager"); - - $dql = "SELECT COUNT(p) FROM PartKeepr\PartBundle\Entity\PartMeasurementUnit p WHERE p.default = :default"; - $query = $entityManager->createQuery($dql); - $query->setParameter("default", true); - - if ($query->getSingleScalarResult() == 0) { - $partUnit = new PartMeasurementUnit(); - $partUnit->setName("Pieces"); - $partUnit->setShortName("pcs"); - $partUnit->setDefault(true); - - $entityManager->persist($partUnit); - $entityManager->flush(); - - } + $this->get("partkeepr.setup.part_unit_service")->setupDefaultPartUnit(); } catch (\Exception $e) { $response["success"] = false; $response["message"] = "Part unit setup error"; diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml @@ -11,5 +11,9 @@ <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> </services> </container> diff --git a/src/PartKeepr/SetupBundle/Services/PartUnitSetupService.php b/src/PartKeepr/SetupBundle/Services/PartUnitSetupService.php @@ -0,0 +1,47 @@ +<?php + + +namespace PartKeepr\SetupBundle\Services; + + +use Doctrine\ORM\EntityManager; +use PartKeepr\PartBundle\Entity\PartMeasurementUnit; + +class PartUnitSetupService +{ + /** + * @var EntityManager + */ + private $entityManager; + + public function __construct( + EntityManager $entityManager + ) { + $this->entityManager = $entityManager; + } + + /** + * Sets up the default part unit (pieces) if it doesn't exist + * @return boolean True if a default unit was created, false if it already exists + */ + public function setupDefaultPartUnit() + { + + $dql = "SELECT COUNT(p) FROM PartKeepr\PartBundle\Entity\PartMeasurementUnit p WHERE p.default = :default"; + $query = $this->entityManager->createQuery($dql); + $query->setParameter("default", true); + + if ($query->getSingleScalarResult() == 0) { + $partUnit = new PartMeasurementUnit(); + $partUnit->setName("Pieces"); + $partUnit->setShortName("pcs"); + $partUnit->setDefault(true); + + $this->entityManager->persist($partUnit); + $this->entityManager->flush(); + return true; + } else { + return false; + } + } +}