PhinxMigrateTask::getEnvironment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * Wolnościowiec / WebDeploy
5
 * ------------------------
6
 *
7
 *   Framework for creation of post-install scripts dedicated
8
 *   for applications hosted on shared hosting (without access to the shell).
9
 *
10
 *   A part of an anarchist portal - wolnosciowiec.net
11
 *
12
 *   Wolnościowiec is a project to integrate the movement
13
 *   of people who strive to build a society based on
14
 *   solidarity, freedom, equality with a respect for
15
 *   individual and cooperation of each other.
16
 *
17
 *   We support human rights, animal rights, feminism,
18
 *   anti-capitalism (taking over the production by workers),
19
 *   anti-racism, and internationalism. We negate
20
 *   the political fight and politicians at all.
21
 *
22
 *   http://wolnosciowiec.net/en
23
 *
24
 *   License: LGPLv3
25
 */
26
27
namespace Wolnosciowiec\WebDeploy\Tasks;
28
29
use Phinx\Wrapper\TextWrapper;
30
use \Psr\Http\Message\RequestInterface;
31
use Wolnosciowiec\WebDeploy\Exceptions\DeploymentFailureException;
32
33
/**
34
 * @package Wolnosciowiec\WebDeploy\Tasks
35
 */
36
class PhinxMigrateTask implements TaskInterface
37
{
38
    /**
39
     * @var string $configurationPath
40
     */
41
    protected $configurationPath = null;
42
43
    protected function getEnvironment()
44
    {
45
        return getenv('WL_PHINX_ENV') ? getenv('WL_PHINX_ENV') : null;
46
    }
47
48
    protected function getTarget()
49
    {
50
        return getenv('WL_PHINX_TARGET') ? getenv('WL_PHINX_TARGET') : null;
51
    }
52
53
    protected function getParser(): string
54
    {
55
        if (!getenv('WL_PHINX_PARSER')) {
56
            $ext = strtolower(pathinfo($this->getConfigurationPath(), PATHINFO_EXTENSION));
57
58
            if (in_array($ext, ['yml', 'yaml'])) {
59
                return 'Yaml';
60
            }
61
            elseif ($ext == 'php') {
62
                return 'Php';
63
            }
64
        }
65
66
        return getenv('WL_PHINX_PARSER') ? getenv('WL_PHINX_PARSER') : 'Yaml';
67
    }
68
69
    protected function getConfigurationPath(): string
70
    {
71
        if ($this->configurationPath === null) {
72
            foreach (array_filter([
73
                __DIR__ . '/../../../../../phinx.yml',
74
                __DIR__ . '/../../../../../phinx.php',
75
                __DIR__ . '/../../phinx.yml',
76
                __DIR__ . '/../../phinx.php',
77
                getenv('WL_PHINX_PATH')
78
            ]) as $path)
79
            {
80
                if (is_file($path)) {
81
                    $this->configurationPath = $path;
82
                    return $path;
83
                }
84
            }
85
86
            throw new \Exception('Cannot find a phinx.php configuration file');
87
        }
88
89
        return $this->configurationPath;
90
    }
91
92
    private function getPhinxPath(): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
93
    {
94
        $paths = [
95
            __DIR__ . '/../../vendor/robmorgan/phinx/app/phinx.php',
96
            __DIR__ . '/../../../../robmorgan/phinx/app/phinx.php',
97
            getenv('WL_PHINX_APP_PATH')
98
        ];
99
100
        foreach (array_filter($paths) as $path) {
101
            if (is_file($path)) {
102
                return $path;
103
            }
104
        }
105
106
        throw new \Exception('Cannot find phinx path!');
107
    }
108
109
    /**
110
     * @param RequestInterface $request
111
     *
112
     * @throws DeploymentFailureException
113
     * @throws \Exception
114
     *
115
     * @return string
116
     */
117
    public function execute(RequestInterface $request): string
118
    {
119
        $app = require $this->getPhinxPath();
120
        $wrap = new TextWrapper($app);
121
122
        $wrap->setOption('configuration', $this->getConfigurationPath());
123
        $wrap->setOption('parser',        $this->getParser());
124
125
        $action = $wrap->getMigrate($this->getEnvironment(), $this->getTarget());
126
127
        if ($wrap->getExitCode() !== 0) {
128
            throw new DeploymentFailureException('Phinx failed with a non-zero exit code, details: "' . $action . '"');
129
        }
130
131
        return $action;
132
    }
133
134
    /**
135
     * @param string $configurationPath
136
     * @return $this
137
     */
138
    public function setConfigurationPath(string $configurationPath)
139
    {
140
        $this->configurationPath = $configurationPath;
141
        return $this;
142
    }
143
}