Passed
Push — 6.4 ( 232ba6...48563d )
by Christian
18:34 queued 27s
created

EnvConfigWriter::writeConfig()   C

Complexity

Conditions 10
Paths 144

Size

Total Lines 86
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 61
nc 144
nop 2
dl 0
loc 86
rs 6.6909
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Installer\Configuration;
4
5
use Defuse\Crypto\Key;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\Installer\Controller\ShopConfigurationController;
8
use Shopware\Core\Installer\Finish\UniqueIdGenerator;
9
use Shopware\Core\Maintenance\System\Struct\DatabaseConnectionInformation;
10
11
/**
12
 * @internal
13
 *
14
 * @phpstan-import-type Shop from ShopConfigurationController
15
 */
16
#[Package('core')]
17
class EnvConfigWriter
18
{
19
    private string $projectDir;
20
21
    private UniqueIdGenerator $idGenerator;
22
23
    public function __construct(string $projectDir, UniqueIdGenerator $idGenerator)
24
    {
25
        $this->projectDir = $projectDir;
26
        $this->idGenerator = $idGenerator;
27
    }
28
29
    /**
30
     * @param Shop $shop
31
     */
32
    public function writeConfig(DatabaseConnectionInformation $info, array $shop): void
33
    {
34
        $key = Key::createNewRandomKey();
35
        $secret = $key->saveToAsciiSafeString();
36
37
        $newEnv = [];
38
39
        $newEnv[] = '###> symfony/lock ###';
40
        $newEnv[] = '# Choose one of the stores below';
41
        $newEnv[] = '# postgresql+advisory://db_user:db_password@localhost/db_name';
42
        $newEnv[] = 'LOCK_DSN=flock';
43
        $newEnv[] = '###< symfony/lock ###';
44
        $newEnv[] = '';
45
46
        $newEnv[] = '###> symfony/messenger ###';
47
        $newEnv[] = '# Choose one of the transports below';
48
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=doctrine://default';
49
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages';
50
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages';
51
        $newEnv[] = '###< symfony/messenger ###';
52
        $newEnv[] = '';
53
54
        $newEnv[] = '###> symfony/mailer ###';
55
        $newEnv[] = 'MAILER_DSN=null://null';
56
        $newEnv[] = '###< symfony/mailer ###';
57
        $newEnv[] = '';
58
59
        $newEnv[] = '###> shopware/core ###';
60
        $newEnv[] = 'APP_ENV=prod';
61
        $newEnv[] = 'APP_SECRET=' . $secret;
62
        $newEnv[] = 'APP_URL=' . $shop['schema'] . '://' . $shop['host'] . $shop['basePath'];
63
        $newEnv[] = 'DATABASE_URL=' . $info->asDsn();
64
65
        if (!empty($info->getSslCaPath())) {
66
            $newEnv[] = 'DATABASE_SSL_CA=' . $info->getSslCaPath();
67
        }
68
69
        if (!empty($info->getSslCertPath())) {
70
            $newEnv[] = 'DATABASE_SSL_CERT=' . $info->getSslCertPath();
71
        }
72
73
        if (!empty($info->getSslCertKeyPath())) {
74
            $newEnv[] = 'DATABASE_SSL_KEY=' . $info->getSslCertKeyPath();
75
        }
76
77
        if ($info->getSslDontVerifyServerCert() !== null) {
78
            $newEnv[] = 'DATABASE_SSL_DONT_VERIFY_SERVER_CERT=' . ($info->getSslDontVerifyServerCert() ? '1' : '');
79
        }
80
81
        $newEnv[] = 'COMPOSER_HOME=' . $this->projectDir . '/var/cache/composer';
82
        $newEnv[] = 'INSTANCE_ID=' . $this->idGenerator->getUniqueId();
83
        $newEnv[] = 'BLUE_GREEN_DEPLOYMENT=' . (int) $shop['blueGreenDeployment'];
84
        $newEnv[] = '###< shopware/core ###';
85
        $newEnv[] = '';
86
87
        $newEnv[] = '###> shopware/elasticsearch ###';
88
89
        if (file_exists($this->projectDir . '/symfony.lock')) {
90
            $newEnv[] = 'OPENSEARCH_URL=http://localhost:9200';
91
        } else {
92
            $newEnv[] = 'SHOPWARE_ES_HOSTS=http://localhost:9200';
93
        }
94
95
        $newEnv[] = 'SHOPWARE_ES_ENABLED=0';
96
        $newEnv[] = 'SHOPWARE_ES_INDEXING_ENABLED=0';
97
        $newEnv[] = 'SHOPWARE_ES_INDEX_PREFIX=sw';
98
        $newEnv[] = 'SHOPWARE_ES_THROW_EXCEPTION=1';
99
        $newEnv[] = '###< shopware/elasticsearch ###';
100
        $newEnv[] = '';
101
102
        $newEnv[] = '###> shopware/storefront ###';
103
        $newEnv[] = 'SHOPWARE_HTTP_CACHE_ENABLED=1';
104
        $newEnv[] = 'SHOPWARE_HTTP_DEFAULT_TTL=7200';
105
        $newEnv[] = '###< shopware/storefront ###';
106
        $newEnv[] = '';
107
108
        file_put_contents($this->projectDir . '/.env', implode("\n", $newEnv));
109
110
        $htaccessPath = $this->projectDir . '/public/.htaccess';
111
112
        if (file_exists($htaccessPath . '.dist') && !file_exists($htaccessPath)) {
113
            $perms = fileperms($htaccessPath . '.dist');
114
            copy($htaccessPath . '.dist', $htaccessPath);
115
116
            if ($perms) {
117
                chmod($htaccessPath, $perms | 0644);
118
            }
119
        }
120
    }
121
}
122