Browse Source

FD3-236 Se crea el servicio para crear Reports.

Maximiliano Schvindt 7 years ago
parent
commit
27528484fb

+ 14 - 2
src/StatsBundle/Entity/Report.php

@@ -62,7 +62,7 @@ class Report implements TenancyIdTraitInterface
 
     /**
      * @Gedmo\Timestampable(on="create")
-     * @ORM\Column(type="datetime")
+     * @ORM\Column(type="datetime", nullable=true)
      */
     protected $created;
 
@@ -172,7 +172,19 @@ class Report implements TenancyIdTraitInterface
 
 
     /**
-     * Get updated
+     * Set created
+     *
+     * @param \DateTime $created
+     */
+    public function setCreated($created)
+    {
+        $this->created = $created;
+        
+        return $this;
+    }
+    
+    /**
+     * Get created
      *
      * @return \DateTime
      */

+ 4 - 0
src/StatsBundle/Resources/config/services.yml

@@ -39,3 +39,7 @@ services:
             - [setTranslationDomain, [StatsBundle]]
             - [setTemplate, ['show','StatsBundle:PonPort:base_show.html.twig']]
             - [setTemplate, ['list','StatsBundle:PonPort:base_list.html.twig']]
+    
+    stats.report.manager:
+        class: StatsBundle\Services\ReportManager
+        arguments: ['@doctrine.orm.entity_manager','@validator','@webservice']

+ 80 - 0
src/StatsBundle/Services/ReportManager.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace StatsBundle\Services;
+
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\ORM\EntityRepository;
+use StatsBundle\Entity\Report;
+use Symfony\Component\Validator\Validator\ValidatorInterface;
+use WebserviceBundle\Services\Webservice;
+
+class ReportManager
+{
+    /**
+     * @var EntityManagerInterface
+     */
+    private $em;
+
+    /**
+     * @var ValidatorInterface
+     */
+    private $validator;
+
+    /**
+     * @var EntityRepository 
+     */
+    private $reportRepository;
+
+    /**
+     * @var Webservice 
+     */
+    private $webservice;
+
+
+    /**
+     * @param EntityManagerInterface $em
+     * @param ValidatorInterface $validator
+     * @param Webservice $webservice
+     */
+    public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, Webservice $webservice)
+    {
+        $this->em = $em;
+        $this->reportRepository = $em->getRepository('StatsBundle:Report');
+        $this->validator = $validator;
+        $this->webservice = $webservice;
+    }
+
+    /**
+     * @param string $deviceType
+     * @param string $reportType
+     * @param int $deviceId
+     * @param int $deviceServer
+     * @param int $tenancyId
+     * @param array $data
+     * 
+     * @return Report
+     */
+    public function create($deviceType, $reportType, $deviceId, $deviceServer, $tenancyId, $data = array())
+    {
+        $report = new Report();
+        $report->setDeviceType($deviceType);
+        $report->setReportType($reportType);
+        $report->setDeviceId($deviceId);
+
+        $deviceServer = $this->em->getRepository('\StatsBundle\Entity\DeviceServer')->findOneById($deviceServer);
+        $report->setDeviceServer($deviceServer);
+        
+        $report->setTenancyId($tenancyId);
+        $report->setJsonExtraData($data);
+        $report->setCreated(new \DateTime());
+        
+
+        if ($this->validator->validate($report)->count() == 0) {
+            $this->em->persist($report);
+            $this->em->flush($report);
+        }
+
+        return $report;
+    }
+
+}