Scalr\System\Zmq\Cron\Task\AnalyticsNotifications::saveNotificationData PHP Метод

saveNotificationData() приватный Метод

Saves project or cost center notification
private saveNotificationData ( ProjectEntity | Scalr\Stats\CostAnalytics\Entity\CostCentreEntity $subject, NotificationEntity $notification, boolean $ifAccountLevel = false )
$subject Scalr\Stats\CostAnalytics\Entity\ProjectEntity | Scalr\Stats\CostAnalytics\Entity\CostCentreEntity Project or cost center entity
$notification Scalr\Stats\CostAnalytics\Entity\NotificationEntity Current notification object
$ifAccountLevel boolean True if account level notification
    private function saveNotificationData($subject, NotificationEntity $notification, $ifAccountLevel = false)
    {
        $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'));
        $scope = $ifAccountLevel ? 'account' : 'admin';
        $subjectAnalytics = ['budget' => $periodSubjectData['totals']['budget'], 'name' => $subject->name, 'trends' => $periodSubjectData['totals']['trends'], 'forecastCost' => $periodSubjectData['totals']['forecastCost'], 'interval' => $periodSubjectData['interval'], 'date' => $formatedTitle, 'detailsUrl' => $baseUrl . "#/{$scope}/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();
            }
        }
    }