partkeepr

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

SystemNoticeService.php (1147B)


      1 <?php
      2 
      3 namespace PartKeepr\CoreBundle\Services;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\CoreBundle\Entity\SystemNotice;
      7 use Symfony\Component\DependencyInjection\ContainerAware;
      8 
      9 class SystemNoticeService extends ContainerAware
     10 {
     11     /**
     12      * @var EntityManager
     13      */
     14     private $entityManager;
     15 
     16     public function __construct(EntityManager $entityManager)
     17     {
     18         $this->entityManager = $entityManager;
     19     }
     20 
     21     public function createUniqueSystemNotice($type, $title, $description)
     22     {
     23         $dql = "SELECT sn FROM PartKeepr\CoreBundle\Entity\SystemNotice sn WHERE sn.type = :type";
     24         $query = $this->entityManager->createQuery($dql);
     25 
     26         $query->setParameter('type', $type);
     27 
     28         try {
     29             $notice = $query->getSingleResult();
     30         } catch (\Exception $e) {
     31             $notice = new SystemNotice();
     32             $this->entityManager->persist($notice);
     33         }
     34 
     35         $notice->setDate(new \DateTime());
     36         $notice->setTitle($title);
     37         $notice->setDescription($description);
     38         $notice->setType($type);
     39 
     40         $this->entityManager->flush();
     41 
     42         return $notice;
     43     }
     44 }