FinishStagingSiteCreateTask.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Tasks\StagingSiteCreate; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Facades\Hooks; |
| 7 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 8 | use WPStaging\Framework\Job\Dto\StepsDto; |
| 9 | use WPStaging\Framework\Job\Dto\TaskResponseDto; |
| 10 | use WPStaging\Framework\Utils\Cache\Cache; |
| 11 | use WPStaging\Staging\Dto\Job\StagingSiteJobsDataDto; |
| 12 | use WPStaging\Staging\Dto\StagingSiteDto; |
| 13 | use WPStaging\Staging\Dto\Task\Response\FinishStagingSiteResponseDto; |
| 14 | use WPStaging\Staging\Jobs\StagingSiteCreate; |
| 15 | use WPStaging\Staging\Sites; |
| 16 | use WPStaging\Staging\Tasks\StagingTask; |
| 17 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 18 | |
| 19 | use function WPStaging\functions\debug_log; |
| 20 | |
| 21 | class FinishStagingSiteCreateTask extends StagingTask |
| 22 | { |
| 23 | |
| 24 | protected $jobDataDto; |
| 25 | |
| 26 | |
| 27 | private $sites; |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | public function __construct(LoggerInterface $logger, Cache $cache, StepsDto $stepsDto, SeekableQueueInterface $taskQueue, Sites $sites) |
| 37 | { |
| 38 | parent::__construct($logger, $cache, $stepsDto, $taskQueue); |
| 39 | $this->sites = $sites; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | public static function getTaskName() |
| 46 | { |
| 47 | return 'staging_site_create_finish'; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | public static function getTaskTitle() |
| 54 | { |
| 55 | return 'Finishing Staging Site Creation'; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | public function execute() |
| 62 | { |
| 63 | $this->getJobTransientCache()->completeJob(); |
| 64 | $stagingSites = $this->sites->tryGettingStagingSites(); |
| 65 | $stagingSite = $this->buildStagingSite(); |
| 66 | $stagingSites[$this->jobDataDto->getCloneId()] = $stagingSite->toArray(); |
| 67 | $this->sites->updateStagingSites($stagingSites); |
| 68 | $this->logger->info(sprintf( |
| 69 | 'Staging Site "%s" created.', |
| 70 | $stagingSite->getSiteName() |
| 71 | )); |
| 72 | |
| 73 | $this->triggerOnStagingSiteCreatedEvent($stagingSite); |
| 74 | |
| 75 | return $this->overrideGenerateResponse(); |
| 76 | } |
| 77 | |
| 78 | protected function buildStagingSite(): StagingSiteDto |
| 79 | { |
| 80 | $stagingSite = $this->jobDataDto->getStagingSite(); |
| 81 | $stagingSite->setStatus(StagingSiteDto::STATUS_FINISHED); |
| 82 | $stagingSite->setDatetime(time()); |
| 83 | $stagingSite->setVersion(WPStaging::getVersion()); |
| 84 | $stagingSite->setOwnerId(get_current_user_id()); |
| 85 | |
| 86 | return $stagingSite; |
| 87 | } |
| 88 | |
| 89 | protected function triggerOnStagingSiteCreatedEvent(StagingSiteDto $stagingSite) |
| 90 | { |
| 91 | Hooks::doAction(StagingSiteCreate::ACTION_CLONING_COMPLETE, $stagingSite->toArray()); |
| 92 | Hooks::doAction(StagingSiteCreate::ACTION_STAGING_SITE_CREATED, $stagingSite->toArray()); |
| 93 | } |
| 94 | |
| 95 | protected function getResponseDto() |
| 96 | { |
| 97 | return new FinishStagingSiteResponseDto(); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | private function overrideGenerateResponse() |
| 104 | { |
| 105 | add_filter(self::FILTER_TASK_RESPONSE, function ($response) { |
| 106 | if ($response instanceof FinishStagingSiteResponseDto) { |
| 107 | $response->setCloneId($this->jobDataDto->getCloneId()); |
| 108 | $response->setStagingSiteUrl($this->jobDataDto->getStagingSiteUrl()); |
| 109 | } else { |
| 110 | debug_log('Fail to finalize response for staging site creation process! Response content: ' . print_r($response, true)); |
| 111 | } |
| 112 | |
| 113 | return $response; |
| 114 | }); |
| 115 | |
| 116 | return $this->generateResponse(); |
| 117 | } |
| 118 | } |
| 119 |