Completed
Branch feature/environment (8e9d70)
by Benjamin
01:29
created

SwitchTask::copyFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * This file is part of the teamneusta/magallanes-task-typo3 package.
4
 *
5
 * Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
8
 *
9
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
10
 */
11
declare(strict_types=1);
12
13
namespace TeamNeusta\Magallanes\Task\TYPO3\Environment;
14
15
use Mage\Runtime\Exception\RuntimeException;
16
use Mage\Task\BuiltIn\FS\CopyTask;
17
use Mage\Task\BuiltIn\FS\MoveTask;
18
use Psr\Log\LogLevel;
19
use TeamNeusta\Magallanes\Task\TYPO3\AbstractTypo3Task;
20
21
/**
22
 * Class SwitchTask
23
 *
24
 * @author Benjamin Kluge <[email protected]>
25
 * @package TeamNeusta\Magallanes\Task\TYPO3
26
 */
27
class SwitchTask extends AbstractTypo3Task
28
{
29
    /**
30
     * name
31
     *
32
     * @var string
33
     */
34
    protected $name = 'typo3-environment-switch';
35
36
    /**
37
     * description
38
     *
39
     * @var string
40
     */
41
    protected $description = '[TYPO3] TYPO3 switch environment Localconfiguration.php';
42
43
44
    /**
45
     * execute
46
     * @return bool
47
     * @throws RuntimeException
48
     */
49
    public function execute(): bool
50
    {
51
        $options = $this->getOptions(['web-dir' => 'web', 'environment-folder' => './Environments']);
52
        $environmentFile = $options['environment-folder'].DIRECTORY_SEPARATOR.'LocalConfiguration.'.$this->runtime->getEnvironment().'.php';
53
        $actualConfigurationFile = $options['web-dir'].DIRECTORY_SEPARATOR.'typo3conf'.DIRECTORY_SEPARATOR.'LocalConfiguration.php';
54
        $actualConfigurationFileTo = $options['environment-folder'].DIRECTORY_SEPARATOR.'LocalConfiguration.php';
55
        if ($this->runtime->getStage() !== 'post-deploy') {
56
            if (file_exists($environmentFile)) {
57
                $this->moveFile($actualConfigurationFile, $actualConfigurationFileTo);
58
                $this->setPostDeployTask();
59
                $this->copyFile($environmentFile, $actualConfigurationFile);
60
            } else {
61
                // throw new RuntimeException('Environment not exist: ' . $environmentFile);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
                $this->runtime->log('Environment not exist: ' . $environmentFile, LogLevel::ERROR);
63
                return false;
64
            }
65
        } else {
66
            $this->moveFile($actualConfigurationFileTo, $actualConfigurationFile);
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * moveFile
74
     *
75
     * @param string $from
76
     * @param string $to
77
     * @return void
78
     */
79 View Code Duplication
    protected function moveFile(string $from, string $to)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if (file_exists($from)) {
82
            $moveTaks = new MoveTask();
83
            $moveTaks->setRuntime($this->runtime);
84
            $moveTaks->setOptions($this->getOptions(['from' => $from, 'to' => $to]));
85
            $moveTaks->execute();
86
        }
87
    }
88
89
    /**
90
     * copyFile
91
     *
92
     * @param string $from
93
     * @param string $to
94
     * @return void
95
     */
96 View Code Duplication
    protected function copyFile(string $from, string $to)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if (file_exists($from)) {
99
            $copyTaks = new CopyTask();
100
            $copyTaks->setRuntime($this->runtime);
101
            $copyTaks->setOptions($this->getOptions(['from' => $from, 'to' => $to]));
102
            $copyTaks->execute();
103
        }
104
    }
105
106
    /**
107
     * setPostDeployTask
108
     *
109
     * @return void
110
     */
111
    protected function setPostDeployTask()
112
    {
113
        $configuration = $this->runtime->getConfiguration();
114
        $configuration['environments'][$this->runtime->getEnvironment()]['post-deploy'][] = self::class;
115
        $this->runtime->setConfiguration($configuration);
116
    }
117
}
118