partkeepr

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

SiPrefixSetupService.php (2300B)


      1 <?php
      2 
      3 namespace PartKeepr\SetupBundle\Services;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\SiPrefixBundle\Entity\SiPrefix;
      7 use Symfony\Component\HttpKernel\KernelInterface;
      8 use Symfony\Component\Yaml\Parser;
      9 
     10 class SiPrefixSetupService
     11 {
     12     const SIPREFIX_PATH = '@PartKeeprSetupBundle/Resources/setup-data/';
     13     const SIPREFIX_DATA = 'siprefixes.yml';
     14 
     15     /**
     16      * @var EntityManager
     17      */
     18     private $entityManager;
     19 
     20     /**
     21      * @var KernelInterface
     22      */
     23     private $kernel;
     24 
     25     /**
     26      * SiPrefixSetupService constructor.
     27      *
     28      * @param EntityManager   $entityManager
     29      * @param KernelInterface $kernel
     30      */
     31     public function __construct(EntityManager $entityManager, KernelInterface $kernel)
     32     {
     33         $this->entityManager = $entityManager;
     34         $this->kernel = $kernel;
     35     }
     36 
     37     /**
     38      * Imports or updates the existing si prefixes.
     39      *
     40      * @return array An array with the keys "skipped" and "imported" which contain the number of si prefixes skipped and imported
     41      */
     42     public function importSiPrefixes()
     43     {
     44         $path = $this->kernel->locateResource(self::SIPREFIX_PATH.self::SIPREFIX_DATA);
     45 
     46         $yaml = new Parser();
     47         $data = $yaml->parse(file_get_contents($path));
     48 
     49         $count = 0;
     50         $updated = 0;
     51 
     52         foreach ($data as $prefixName => $prefixData) {
     53             $prefix = $this->getSiPrefix($prefixName);
     54 
     55             if ($prefix === null) {
     56                 $prefix = new SiPrefix();
     57                 $prefix->setPrefix($prefixName);
     58                 $this->entityManager->persist($prefix);
     59                 $count++;
     60             }
     61 
     62             $prefix->setExponent($prefixData['exponent']);
     63             $prefix->setSymbol($prefixData['symbol']);
     64             $prefix->setBase($prefixData['base']);
     65 
     66             $updated++;
     67         }
     68         $this->entityManager->flush();
     69 
     70         return ['updated' => $updated - $count, 'imported' => $count];
     71     }
     72 
     73     /**
     74      * Finds an SI Prefix by name.
     75      *
     76      * @param string $name The SI Prefix name
     77      *
     78      * @return SiPrefix|null
     79      */
     80     protected function getSiPrefix($name)
     81     {
     82         $repository = $this->entityManager->getRepository('PartKeeprSiPrefixBundle:SiPrefix');
     83 
     84         return $repository->findOneBy(['prefix' => $name]);
     85     }
     86 }