magallanes/src/Deploy/Strategy/RsyncStrategy.php

96 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2022-04-10 06:20:03 +02:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2017-01-12 02:22:21 +01:00
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
/**
* Strategy for Deployment with Rsync
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RsyncStrategy implements StrategyInterface
{
2022-04-10 06:20:03 +02:00
protected Runtime $runtime;
2022-04-10 06:20:03 +02:00
public function getName(): string
{
return 'Rsync';
}
2022-04-10 06:20:03 +02:00
public function setRuntime(Runtime $runtime): void
{
$this->runtime = $runtime;
}
2022-04-10 06:20:03 +02:00
public function getPreDeployTasks(): array
{
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_unshift($tasks, 'git/change-branch');
}
return $tasks;
}
2022-04-10 06:20:03 +02:00
public function getOnDeployTasks(): array
{
$this->checkStage(Runtime::ON_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/rsync', $tasks)) {
array_unshift($tasks, 'deploy/rsync');
}
return $tasks;
}
2022-04-10 06:20:03 +02:00
public function getOnReleaseTasks(): array
{
return [];
}
2022-04-10 06:20:03 +02:00
public function getPostReleaseTasks(): array
{
return [];
}
2022-04-10 06:20:03 +02:00
public function getPostDeployTasks(): array
{
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
}
return $tasks;
}
/**
* Check the runtime stage is correct
*
* @throws RuntimeException
*/
2022-04-10 06:20:03 +02:00
private function checkStage(string $stage): void
{
if ($this->runtime->getStage() !== $stage) {
2022-04-10 06:20:03 +02:00
throw new RuntimeException(
sprintf('Invalid stage, got "%s" but expected "%s"', $this->runtime->getStage(), $stage)
);
}
}
}