Scalr\Stats\CostAnalytics\Entity\ReportPayloadEntity::init PHP Method

init() public static method

Initializes ReportPayloadEntity object
public static init ( array $data, array $payload, string | DateTim\DateTime | null $start = null ) : ReportPayloadEntity
$data array Data array of params for uuid (Notification type, subject type, subject id)
$payload array Payload
$start string | DateTim\DateTime | null optional Start date of the report (Y-m-d)
return ReportPayloadEntity Returns ReportPayloadEntity object
    public static function init(array $data, $payload, $start = null)
    {
        $obj = new self();
        $obj->created = $start instanceof DateTime ? clone $start : new DateTime($start ?: "now", new DateTimeZone('UTC'));
        $dataToHash = implode('|', $data) . '|' . $obj->created->format('Y-m-d');
        $obj->uuid = $obj->type('uuid')->toPhp(substr(hash('sha1', $dataToHash, true), 0, 16));
        $obj->secret = $obj->type('secret')->toPhp(hash('sha1', \Scalr::GenerateRandomKey(40), true));
        $baseUrl = rtrim(\Scalr::getContainer()->config('scalr.endpoint.scheme') . "://" . \Scalr::getContainer()->config('scalr.endpoint.host'), '/');
        $payload['reportUrl'] = $baseUrl . '#/public/report?uuid=' . $obj->uuid . '&secretHash=' . bin2hex((string) $obj->secret);
        $obj->payload = json_encode($payload);
        return $obj;
    }

Usage Example

 /**
  * Saves project or cost center notification
  *
  * @param ProjectEntity|CostCentreEntity $subject       Project or cost center entity
  * @param NotificationEntity             $notification  Current notification object
  * @throws InvalidArgumentException
  */
 private function saveNotificationData($subject, NotificationEntity $notification)
 {
     $baseUrl = rtrim(\Scalr::getContainer()->config('scalr.endpoint.scheme') . "://" . \Scalr::getContainer()->config('scalr.endpoint.host'), '/');
     $quarters = new Quarters(SettingEntity::getQuarters());
     $date = $quarters->getPeriodForDate('yesterday');
     $formatedTitle = 'Q' . $quarters->getQuarterForDate('now') . ' budget (' . (new \DateTime('now', new \DateTimeZone('UTC')))->format('M j, Y') . ')';
     if ($subject instanceof ProjectEntity) {
         $getPeriodicSubjectData = 'getProjectPeriodData';
         $subjects = 'projects';
         $childItems = 'farms';
         $subjectIdName = 'projectId';
         $subjectName = 'project';
     } else {
         if ($subject instanceof CostCentreEntity) {
             $getPeriodicSubjectData = 'getCostCenterPeriodData';
             $subjects = 'costcenters';
             $childItems = 'projects';
             $subjectIdName = 'ccId';
             $subjectName = 'cc';
         } else {
             throw new InvalidArgumentException("Invalid subject parameter. It must be either ProjectEntity or CostCentreEntity type.");
         }
     }
     $periodSubjectData = \Scalr::getContainer()->analytics->usage->{$getPeriodicSubjectData}($subject->{$subjectIdName}, 'quarter', $date->start->format('Y-m-d'), $date->end->format('Y-m-d'));
     $subjectAnalytics = ['budget' => $periodSubjectData['totals']['budget'], 'name' => $subject->name, 'trends' => $periodSubjectData['totals']['trends'], 'forecastCost' => $periodSubjectData['totals']['forecastCost'], 'interval' => $periodSubjectData['interval'], 'date' => $formatedTitle, 'detailsUrl' => $baseUrl . '#/analytics/' . $subjects . '?' . $subjectIdName . '=' . $subject->{$subjectIdName}, 'jsonVersion' => '1.0.0', $childItems => []];
     if (!empty($periodSubjectData['totals'][$childItems])) {
         $subjectAnalytics[$childItems] = $this->getSubjectChildItems($subject, $periodSubjectData['totals'][$childItems], $date);
     }
     if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_USAGE) {
         $reportType = $subjectName . 'Usage';
         $budgetThreshold = 'budgetSpentPct';
         $emailSubject = $subjectAnalytics['name'] . ' usage notification.';
     } else {
         if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_PROJECTED_OVERSPEND) {
             $reportType = $subjectName . 'Overspend';
             $budgetThreshold = 'estimateOverspendPct';
             $emailSubject = $subjectAnalytics['name'] . ' overspend notification.';
         }
     }
     if ($subjectAnalytics['budget'][$budgetThreshold] >= $notification->threshold) {
         $subjectAnalytics['reportType'] = $reportType;
         $entity = ReportPayloadEntity::init([$notification->notificationType, $notification->subjectType, $subject->{$subjectIdName}, $notification->threshold], $subjectAnalytics);
         if (!ReportPayloadEntity::findPk($entity->uuid)) {
             $payload = json_decode($entity->payload, true);
             if (!empty($subjectAnalytics['budget']['estimateDate'])) {
                 $subjectAnalytics['budget']['estimateDate'] = (new DateTime($subjectAnalytics['budget']['estimateDate'], new DateTimeZone('UTC')))->format('M j, Y');
                 $subjectAnalytics['reportUrl'] = $payload['reportUrl'];
             }
             \Scalr::getContainer()->mailer->setSubject($emailSubject)->setContentType('text/html')->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/budget_notification_' . $subjectName . '.html.php', $subjectAnalytics, $notification->emails);
             $this->getLogger()->info('Notification email has been sent');
             $payload['date'] = $entity->created->format('Y-m-d');
             $entity->payload = json_encode($payload);
             $entity->save();
         }
     }
 }
All Usage Examples Of Scalr\Stats\CostAnalytics\Entity\ReportPayloadEntity::init
ReportPayloadEntity