FootprintSetupService.php (5027B)
1 <?php 2 3 namespace PartKeepr\SetupBundle\Services; 4 5 use Doctrine\ORM\EntityManager; 6 use PartKeepr\CategoryBundle\Services\CategoryService; 7 use PartKeepr\FootprintBundle\Entity\Footprint; 8 use PartKeepr\FootprintBundle\Entity\FootprintCategory; 9 use PartKeepr\FootprintBundle\Entity\FootprintImage; 10 use PartKeepr\UploadedFileBundle\Services\UploadedFileService; 11 use Symfony\Component\HttpFoundation\File\File; 12 use Symfony\Component\HttpKernel\KernelInterface; 13 use Symfony\Component\Yaml\Parser; 14 15 class FootprintSetupService 16 { 17 const FOOTPRINT_PATH = '@PartKeeprSetupBundle/Resources/setup-data/footprints/'; 18 const FOOTPRINT_DATA = 'footprints.yml'; 19 20 /** 21 * @var EntityManager 22 */ 23 private $entityManager; 24 25 /** 26 * @var CategoryService 27 */ 28 private $footprintCategoryService; 29 30 /** 31 * @var UploadedFileService 32 */ 33 private $uploadedFileService; 34 35 /** 36 * @var KernelInterface 37 */ 38 private $kernel; 39 40 public function __construct( 41 EntityManager $entityManager, 42 CategoryService $categoryService, 43 UploadedFileService $uploadedFileService, 44 KernelInterface $kernel 45 ) { 46 $this->entityManager = $entityManager; 47 $this->footprintCategoryService = $categoryService; 48 $this->uploadedFileService = $uploadedFileService; 49 $this->kernel = $kernel; 50 } 51 52 /** 53 * Imports the existing footprints. Skips existing footprints. 54 * 55 * @return array An array with the keys "skipped" and "imported" which contain the number of footprints skipped and imported 56 */ 57 public function importFootprints() 58 { 59 $path = $this->kernel->locateResource(self::FOOTPRINT_PATH.self::FOOTPRINT_DATA); 60 61 $skipped = 0; 62 $count = 0; 63 64 $yaml = new Parser(); 65 $data = $yaml->parse(file_get_contents($path)); 66 67 foreach ($data as $footprintName => $footprintData) { 68 if ($this->footprintExists($footprintName)) { 69 $skipped++; 70 continue; 71 } 72 73 $this->createFootprint($footprintName, $footprintData); 74 $count++; 75 } 76 77 $this->entityManager->flush(); 78 79 return ['skipped' => $skipped, 'imported' => $count]; 80 } 81 82 protected function createFootprint($footprintName, $footprintData) 83 { 84 85 /** 86 * @var FootprintCategory 87 */ 88 $footprintCategoryRootNode = $this->footprintCategoryService->getRootNode(); 89 90 $footprint = new Footprint(); 91 $footprint->setName($footprintName); 92 93 if (array_key_exists('description', $footprintData)) { 94 $footprint->setDescription($footprintData['description']); 95 } 96 97 if (array_key_exists('category', $footprintData)) { 98 $footprintCategory = $this->addFootprintCategoryPath( 99 explode('/', $footprintData['category']), 100 $footprintCategoryRootNode 101 ); 102 $footprint->setCategory($footprintCategory); 103 } 104 105 if (array_key_exists('image', $footprintData)) { 106 $footprintImage = new FootprintImage(); 107 108 $file = $this->kernel->locateResource(self::FOOTPRINT_PATH.$footprintData['image']); 109 $this->uploadedFileService->replaceFromFilesystem($footprintImage, new File($file)); 110 111 $footprint->setImage($footprintImage); 112 } 113 114 $this->entityManager->persist($footprint); 115 } 116 117 /** 118 * Creates a node structure for the given path. 119 * 120 * @param $path array The components of the path 121 * @param $parentNode FootprintCategory The parent node 122 * 123 * @return FootprintCategory 124 */ 125 protected function addFootprintCategoryPath(array $path, FootprintCategory $parentNode) 126 { 127 if (count($path) == 0) { 128 return $parentNode; 129 } 130 $name = array_shift($path); 131 132 $category = null; 133 134 foreach ($parentNode->getChildren() as $child) { 135 if ($child->getName() == $name) { 136 $category = $child; 137 } 138 } 139 140 if ($category === null) { 141 $category = new FootprintCategory(); 142 $category->setParent($parentNode); 143 $category->setName($name); 144 $parentNode->getChildren()[] = $category; 145 146 $this->entityManager->persist($category); 147 } 148 149 return $this->addFootprintCategoryPath($path, $category); 150 } 151 152 /** 153 * Checks if the specified footprint exists. 154 * 155 * @param string $name The footprint name 156 * 157 * @return bool true if the footprints exists, false otherwise 158 */ 159 protected function footprintExists($name) 160 { 161 $dql = "SELECT COUNT(fp) FROM PartKeepr\FootprintBundle\Entity\Footprint fp WHERE fp.name = :name"; 162 $query = $this->entityManager->createQuery($dql); 163 $query->setParameter('name', $name); 164 165 if ($query->getSingleScalarResult() == 0) { 166 return false; 167 } else { 168 return true; 169 } 170 } 171 }