partkeepr

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

UnitSetupService.php (3383B)


      1 <?php
      2 
      3 namespace PartKeepr\SetupBundle\Services;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\SiPrefixBundle\Entity\SiPrefix;
      7 use PartKeepr\UnitBundle\Entity\Unit;
      8 use Symfony\Component\HttpKernel\KernelInterface;
      9 use Symfony\Component\Yaml\Parser;
     10 
     11 class UnitSetupService
     12 {
     13     const UNIT_PATH = '@PartKeeprSetupBundle/Resources/setup-data/';
     14     const UNIT_DATA = 'units.yml';
     15 
     16     /**
     17      * @var EntityManager
     18      */
     19     private $entityManager;
     20 
     21     /**
     22      * @var KernelInterface
     23      */
     24     private $kernel;
     25 
     26     /**
     27      * UnitSetupService constructor.
     28      *
     29      * @param EntityManager   $entityManager
     30      * @param KernelInterface $kernel
     31      */
     32     public function __construct(EntityManager $entityManager, KernelInterface $kernel)
     33     {
     34         $this->entityManager = $entityManager;
     35         $this->kernel = $kernel;
     36     }
     37 
     38     /**
     39      * Imports units.
     40      *
     41      * @throws \Exception If an error occured
     42      *
     43      * @return array An array with the keys "skipped" and "imported" which contain the number of units skipped and imported
     44      */
     45     public function importUnits($updateExisting = false)
     46     {
     47         $path = $this->kernel->locateResource(self::UNIT_PATH.self::UNIT_DATA);
     48 
     49         $yaml = new Parser();
     50         $data = $yaml->parse(file_get_contents($path));
     51 
     52         $count = 0;
     53         $skipped = 0;
     54 
     55         foreach ($data as $unitName => $unitData) {
     56             $existing = true;
     57             $unit = $this->getUnit($unitName);
     58 
     59             if ($unit === null) {
     60                 $existing = false;
     61                 $unit = new Unit();
     62                 $unit->setName($unitName);
     63                 $unit->setSymbol($unitData['symbol']);
     64             }
     65 
     66             if (!$existing || $updateExisting || count($unit->getPrefixes()) === 0) {
     67                 if (array_key_exists('prefixes', $unitData)) {
     68                     if (!is_array($unitData['prefixes'])) {
     69                         throw new \Exception($unitName." doesn't contain a prefix list, or the prefix list is not an array.");
     70                     }
     71 
     72                     foreach ($unitData['prefixes'] as $name) {
     73                         $prefix = $this->getSiPrefix($name);
     74                         if ($prefix === null) {
     75                             throw new \Exception('Unable to find SI Prefix '.$name);
     76                         }
     77 
     78                         $unit->addPrefix($prefix);
     79                     }
     80                 }
     81 
     82                 $this->entityManager->persist($unit);
     83                 $this->entityManager->flush();
     84                 $count++;
     85             } else {
     86                 $skipped++;
     87             }
     88         }
     89 
     90         return ['imported' => $count, 'skipped' => $skipped];
     91     }
     92 
     93     /**
     94      * Checks if the specified SI Prefix.
     95      *
     96      * @param string $name The footprint name
     97      *
     98      * @return Unit|null
     99      */
    100     protected function getUnit($name)
    101     {
    102         $repository = $this->entityManager->getRepository('PartKeeprUnitBundle:Unit');
    103 
    104         return $repository->findOneBy(['name' => $name]);
    105     }
    106 
    107     /**
    108      * Finds an SI Prefix by name.
    109      *
    110      * @param string $name The SI Prefix name
    111      *
    112      * @return SiPrefix|null
    113      */
    114     protected function getSiPrefix($name)
    115     {
    116         $repository = $this->entityManager->getRepository('PartKeeprSiPrefixBundle:SiPrefix');
    117 
    118         return $repository->findOneBy(['prefix' => $name]);
    119     }
    120 }