src/Entity/DynamicReportGoogleAnalytics.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Timestampable\Traits\TimestampableEntity;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\DynamicReportGoogleAnalyticsRepository")
  9.  * @ORM\Table(name="`dynamic_report_google_analytics`")
  10.  */
  11. class DynamicReportGoogleAnalytics
  12. {
  13.     use TimestampableEntity;
  14.     public const SNAPSHOT_TYPE_LATEST_YEARLY 'yearly';
  15.     public const SNAPSHOT_TYPE_LATEST_MONTHLY 'monthly';
  16.     public const SNAPSHOT_TYPE_LATEST_WEEKLY 'weekly';
  17.     public const SNAPSHOT_TYPE_LATEST_DAILY 'daily';
  18.     public const STATUS_COMPLETED 'completed';
  19.     public const STATUS_PROCESSING 'processing';
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private ?int $id null;
  26.     /**
  27.      * @ORM\Column(type="string", nullable=true)
  28.      */
  29.     private ?string $name null;
  30.     /**
  31.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="dynamicReportsGoogle")
  32.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  33.      */
  34.     private ?User $user;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity="App\Entity\GoogleAccount", inversedBy="reports")
  37.      * @ORM\JoinColumn(name="google_account_id", referencedColumnName="id")
  38.      */
  39.     private ?GoogleAccount $googleAccount;
  40.     /**
  41.      * @ORM\Column(type="string")
  42.      */
  43.     private ?string $analyticsAccountId;
  44.     /**
  45.      * @ORM\Column(type="string", nullable=true)
  46.      */
  47.     private ?string $analyticsAccountName null;
  48.     /**
  49.      * @ORM\Column(type="string", nullable=true)
  50.      */
  51.     private ?string $analyticsPropertyId null;
  52.     /**
  53.      * @ORM\Column(type="string", nullable=true)
  54.      */
  55.     private ?string $analyticsPropertyName null;
  56.     /**
  57.      * @ORM\Column(type="string", nullable=true)
  58.      */
  59.     private ?string $analyticsStreamId null;
  60.     /**
  61.      * @ORM\Column(type="string", nullable=true)
  62.      */
  63.     private ?string $analyticsStreamName null;
  64.     /**
  65.      * @ORM\Column(type="string", nullable=true)
  66.      */
  67.     private ?string $universalAnalyticsViewId null;
  68.     /**
  69.      * @ORM\Column(type="string", nullable=true)
  70.      */
  71.     private ?string $universalAnalyticsViewName null;
  72.     /**
  73.      * @ORM\Column(type="string", nullable=true)
  74.      */
  75.     private ?string $defaultUri null;
  76.     /**
  77.      * @ORM\OneToMany(targetEntity="App\Entity\DynamicReportGoogleAnalyticsStatistic", mappedBy="report", cascade={"persist", "remove"}, orphanRemoval=true)
  78.      */
  79.     private ?Collection $statistics;
  80.     /**
  81.      * @ORM\OneToMany(targetEntity="App\Entity\DynamicReportGoogleAnalyticsSnapshot", mappedBy="report", cascade={"persist", "remove"}, orphanRemoval=true)
  82.      * @ORM\OrderBy({"periodStart" = "ASC"})
  83.      */
  84.     private ?Collection $snapshots;
  85.     /**
  86.      * @ORM\Column(type="string", nullable=true)
  87.      */
  88.     private ?string $hash;
  89.     /**
  90.      * @ORM\Column(type="string", nullable=false, options={"default" : "monthly"})
  91.      */
  92.     private ?string $snapshotType self::SNAPSHOT_TYPE_LATEST_MONTHLY;
  93.     /**
  94.      * @ORM\Column(type="date", nullable=true)
  95.      */
  96.     private ?\DateTime $periodStart null;
  97.     /**
  98.      * @ORM\Column(type="date", nullable=true)
  99.      */
  100.     private ?\DateTime $periodEnd null;
  101.     /**
  102.      * @ORM\Column(type="string", nullable=false, options={"default" : "processing"})
  103.      */
  104.     private ?string $status self::STATUS_PROCESSING;
  105.     public function __construct()
  106.     {
  107.         $this->statistics = new ArrayCollection();
  108.         $this->snapshots = new ArrayCollection();
  109.         $this->hash uniqid(''true);
  110.     }
  111.     public function getId(): ?int
  112.     {
  113.         return $this->id;
  114.     }
  115.     public function setId(?int $id): void
  116.     {
  117.         $this->id $id;
  118.     }
  119.     public function getName(): ?string
  120.     {
  121.         if (null === $this->name) {
  122.             return $this->getDisplayName();
  123.         }
  124.         return $this->name;
  125.     }
  126.     public function setName(?string $name): void
  127.     {
  128.         $this->name $name;
  129.     }
  130.     public function getUser(): ?User
  131.     {
  132.         return $this->user;
  133.     }
  134.     public function setUser(?User $user): void
  135.     {
  136.         $this->user $user;
  137.     }
  138.     public function getGoogleAccount(): ?GoogleAccount
  139.     {
  140.         return $this->googleAccount;
  141.     }
  142.     public function setGoogleAccount(?GoogleAccount $googleAccount): void
  143.     {
  144.         $this->googleAccount $googleAccount;
  145.     }
  146.     public function getAnalyticsAccountId(): ?string
  147.     {
  148.         return $this->analyticsAccountId;
  149.     }
  150.     public function setAnalyticsAccountId(?string $analyticsAccountId): void
  151.     {
  152.         $this->analyticsAccountId $analyticsAccountId;
  153.     }
  154.     public function getAnalyticsAccountName(): ?string
  155.     {
  156.         return $this->analyticsAccountName;
  157.     }
  158.     public function setAnalyticsAccountName(?string $analyticsAccountName): void
  159.     {
  160.         $this->analyticsAccountName $analyticsAccountName;
  161.     }
  162.     public function getAnalyticsPropertyId(): ?string
  163.     {
  164.         return $this->analyticsPropertyId;
  165.     }
  166.     public function setAnalyticsPropertyId(?string $analyticsPropertyId): void
  167.     {
  168.         $this->analyticsPropertyId $analyticsPropertyId;
  169.     }
  170.     public function getAnalyticsPropertyName(): ?string
  171.     {
  172.         return $this->analyticsPropertyName;
  173.     }
  174.     public function setAnalyticsPropertyName(?string $analyticsPropertyName): void
  175.     {
  176.         $this->analyticsPropertyName $analyticsPropertyName;
  177.     }
  178.     public function getAnalyticsStreamId(): ?string
  179.     {
  180.         return $this->analyticsStreamId;
  181.     }
  182.     public function setAnalyticsStreamId(?string $analyticsStreamId): void
  183.     {
  184.         $this->analyticsStreamId $analyticsStreamId;
  185.     }
  186.     public function getAnalyticsStreamName(): ?string
  187.     {
  188.         return $this->analyticsStreamName;
  189.     }
  190.     public function setAnalyticsStreamName(?string $analyticsStreamName): void
  191.     {
  192.         $this->analyticsStreamName $analyticsStreamName;
  193.     }
  194.     public function getUniversalAnalyticsViewId(): ?string
  195.     {
  196.         return $this->universalAnalyticsViewId;
  197.     }
  198.     public function setUniversalAnalyticsViewId(?string $universalAnalyticsViewId): void
  199.     {
  200.         $this->universalAnalyticsViewId $universalAnalyticsViewId;
  201.     }
  202.     public function getUniversalAnalyticsViewName(): ?string
  203.     {
  204.         return $this->universalAnalyticsViewName;
  205.     }
  206.     public function setUniversalAnalyticsViewName(?string $universalAnalyticsViewName): void
  207.     {
  208.         $this->universalAnalyticsViewName $universalAnalyticsViewName;
  209.     }
  210.     public function getDefaultUri(): ?string
  211.     {
  212.         return $this->defaultUri;
  213.     }
  214.     public function setDefaultUri(?string $defaultUri): void
  215.     {
  216.         $this->defaultUri $defaultUri;
  217.     }
  218.     public function getStatistics(): Collection
  219.     {
  220.         return $this->statistics;
  221.     }
  222.     public function setStatistics($statistics): void
  223.     {
  224.         $this->statistics $statistics;
  225.     }
  226.     public function isCompleted(): bool
  227.     {
  228.         return self::STATUS_COMPLETED === $this->status;
  229.     }
  230.     public function getStatus(): ?string
  231.     {
  232.         return $this->status;
  233.     }
  234.     public function setStatus(?string $status): void
  235.     {
  236.         $this->status $status;
  237.     }
  238.     public function setIsProcessing()
  239.     {
  240.         $this->status self::STATUS_PROCESSING;
  241.     }
  242.     public function getHash(): ?string
  243.     {
  244.         return $this->hash;
  245.     }
  246.     public function setHash(?string $hash): void
  247.     {
  248.         $this->hash $hash;
  249.     }
  250.     public function getSnapshotType(): ?string
  251.     {
  252.         return $this->snapshotType;
  253.     }
  254.     public function setSnapshotType(?string $snapshotType): void
  255.     {
  256.         $this->snapshotType $snapshotType;
  257.     }
  258.     public function getPeriodStart(): ?\DateTime
  259.     {
  260.         return $this->periodStart;
  261.     }
  262.     public function setPeriodStart(?\DateTime $periodStart): void
  263.     {
  264.         $this->periodStart $periodStart;
  265.     }
  266.     public function getPeriodEnd(): ?\DateTime
  267.     {
  268.         return $this->periodEnd;
  269.     }
  270.     public function setPeriodEnd(?\DateTime $periodEnd): void
  271.     {
  272.         $this->periodEnd $periodEnd;
  273.     }
  274.     public function getNextPeriods(): array
  275.     {
  276.         $periodStart = clone $this->periodStart;
  277.         $periodEnd = clone $this->periodEnd;
  278.         $periodStart->setTime(00);
  279.         $periodEnd->setTime(00);
  280.         switch ($this->snapshotType) {
  281.             case self::SNAPSHOT_TYPE_LATEST_YEARLY:
  282.                 $periodStart->modify('NEXT YEAR JANUARY 1');
  283.                 $periodEnd->modify('NEXT YEAR DECEMBER 31');
  284.                 break;
  285.             case self::SNAPSHOT_TYPE_LATEST_MONTHLY:
  286.                 $periodStart->modify('FIRST DAY OF NEXT MONTH');
  287.                 $periodEnd->modify('LAST DAY OF NEXT MONTH');
  288.                 break;
  289.             case self::SNAPSHOT_TYPE_LATEST_WEEKLY:
  290.                 $periodStart->modify('NEXT WEEK MONDAY');
  291.                 $periodEnd->modify('NEXT WEEK SUNDAY');
  292.                 break;
  293.             case self::SNAPSHOT_TYPE_LATEST_DAILY:
  294.                 $periodStart->modify('TOMORROW');
  295.                 $periodEnd->modify('TOMORROW');
  296.                 break;
  297.         }
  298.         $periodStart->setTime(00);
  299.         $periodEnd->setTime(00);
  300.         return [
  301.             $periodStart,
  302.             $periodEnd,
  303.         ];
  304.     }
  305.     public function getSnapshotPeriods(?\DateTime $periodStart null, ?\DateTime $periodEnd null): array
  306.     {
  307.         if (null === $periodStart) {
  308.             $periodStart = clone $this->periodStart;
  309.         }
  310.         if (null === $periodEnd) {
  311.             $periodEnd = clone $this->periodEnd;
  312.         }
  313.         $periodStart->setTime(00);
  314.         $periodEnd->setTime(00);
  315.         $datePeriods = [];
  316.         switch ($this->snapshotType) {
  317.             case self::SNAPSHOT_TYPE_LATEST_YEARLY:
  318.                 $datePeriod = new \DatePeriod($periodStart\DateInterval::createFromDateString('+1 YEAR'), $periodEnd);
  319.                 /** @var \DateTime $date */
  320.                 foreach ($datePeriod->getIterator() as $date) {
  321.                     $dateEnd = clone $date;
  322.                     $datePeriods[] = [
  323.                         $date,
  324.                         $dateEnd->modify('DECEMBER 31'),
  325.                     ];
  326.                 }
  327.                 return $datePeriods;
  328.             case self::SNAPSHOT_TYPE_LATEST_MONTHLY:
  329.                 $datePeriod = new \DatePeriod($periodStart\DateInterval::createFromDateString('+1 MONTH'), $periodEnd);
  330.                 /** @var \DateTime $date */
  331.                 foreach ($datePeriod->getIterator() as $date) {
  332.                     $dateEnd = clone $date;
  333.                     $datePeriods[] = [
  334.                         $date,
  335.                         $dateEnd->modify('LAST DAY OF THIS MONTH'),
  336.                     ];
  337.                 }
  338.                 return $datePeriods;
  339.             case self::SNAPSHOT_TYPE_LATEST_WEEKLY:
  340.                 $datePeriod = new \DatePeriod($periodStart\DateInterval::createFromDateString('NEXT MONDAY'), $periodEnd);
  341.                 /** @var \DateTime $date */
  342.                 foreach ($datePeriod->getIterator() as $date) {
  343.                     $dateEnd = clone $date;
  344.                     $dateEnd->modify('NEXT SUNDAY');
  345.                     if ($dateEnd $this->periodEnd) {
  346.                         $dateEnd $this->periodEnd;
  347.                     }
  348.                     $datePeriods[] = [
  349.                         $date,
  350.                         $dateEnd,
  351.                     ];
  352.                 }
  353.                 return $datePeriods;
  354.             case self::SNAPSHOT_TYPE_LATEST_DAILY:
  355.                 $datePeriod = new \DatePeriod($periodStart\DateInterval::createFromDateString('+1 DAY'), $periodEnd);
  356.                 /** @var \DateTime $date */
  357.                 foreach ($datePeriod->getIterator() as $date) {
  358.                     $dateEnd = clone $date;
  359.                     $datePeriods[] = [
  360.                         $date,
  361.                         $dateEnd->modify('TODAY'),
  362.                     ];
  363.                 }
  364.                 // Include end of month (PHP 8.2: \DateInterval::INCLUDE_END_DATE)
  365.                 $datePeriods[] = [
  366.                     $periodEnd,
  367.                     $periodEnd,
  368.                 ];
  369.                 return $datePeriods;
  370.         }
  371.         return [];
  372.     }
  373.     public function getDisplayPeriod(): string
  374.     {
  375.         if (self::SNAPSHOT_TYPE_LATEST_YEARLY === $this->getSnapshotType()) {
  376.             return sprintf('%s - %s'$this->periodStart->format('M Y'), $this->periodEnd->format('M Y'));
  377.         }
  378.         if (self::SNAPSHOT_TYPE_LATEST_MONTHLY === $this->getSnapshotType()) {
  379.             return $this->periodStart->format('M Y');
  380.         }
  381.         if (self::SNAPSHOT_TYPE_LATEST_WEEKLY === $this->getSnapshotType()) {
  382.             return sprintf('%s - %s'$this->periodStart->format('M d Y'), $this->periodEnd->format('M d Y'));
  383.         }
  384.         if (self::SNAPSHOT_TYPE_LATEST_DAILY === $this->getSnapshotType()) {
  385.             return $this->periodStart->format('M d Y');
  386.         }
  387.         return 'Unknown';
  388.     }
  389.     public function getDisplayName(): string
  390.     {
  391.         return $this->getDomain();
  392.     }
  393.     public function getDomain(): string
  394.     {
  395.         return strtolower(
  396.             str_replace('www.'''parse_url($this->getDefaultUri(), PHP_URL_HOST))
  397.         );
  398.     }
  399.     public function getGoogleAnalyticsReference(): string
  400.     {
  401.         if ($this->getUniversalAnalyticsViewId()) {
  402.             return sprintf('%s / %s'$this->getAnalyticsPropertyId(), $this->getUniversalAnalyticsViewName());
  403.         }
  404.         if ($this->getAnalyticsAccountName() && $this->getAnalyticsPropertyName()) {
  405.             return sprintf('%s / %s'$this->getAnalyticsAccountName(), $this->getAnalyticsPropertyName());
  406.         }
  407.         return sprintf('Property Identifier: %s'$this->getAnalyticsPropertyId());
  408.     }
  409.     public function getSnapshots(): ?Collection
  410.     {
  411.         return $this->snapshots;
  412.     }
  413.     public function setSnapshots(?Collection $snapshots): void
  414.     {
  415.         $this->snapshots $snapshots;
  416.     }
  417.     public function useUniversalAnalytics(): bool
  418.     {
  419.         return null !== $this->getUniversalAnalyticsViewId();
  420.     }
  421.     public function __toString(): string
  422.     {
  423.         return $this->getDisplayName() ?? 'New Report';
  424.     }
  425. }