Completed
Push — master ( ddad5c...64feaf )
by Joschi
06:57
created

ApachePersistenceAdapterStrategy::persist()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 11
nc 6
nop 2
crap 30
1
<?php
2
3
/**
4
 * admin
5
 *
6
 * @category    Tollwerk
7
 * @package     Tollwerk\Admin
8
 * @subpackage  Tollwerk\Admin\Infrastructure\Strategy
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Tollwerk\Admin\Infrastructure\Strategy;
38
39
use Tollwerk\Admin\Application\Contract\VhostPersistenceAdapterStrategyInterface;
40
use Tollwerk\Admin\Domain\Account\AccountInterface;
41
use Tollwerk\Admin\Domain\Vhost\VhostInterface;
42
use Tollwerk\Admin\Infrastructure\App;
43
use Tollwerk\Admin\Infrastructure\Persistence\Apache;
44
45
/**
46
 * Apache persistence adapter strategy
47
 *
48
 * @package Tollwerk\Admin
49
 * @subpackage Tollwerk\Admin\Infrastructure
50
 */
51
class ApachePersistenceAdapterStrategy implements VhostPersistenceAdapterStrategyInterface
52
{
53
    /**
54
     * Configuration
55
     *
56
     * @var array
57
     */
58
    protected $config = [];
59
60
    /**
61
     * Constructor
62
     *
63
     * @throws \RuntimeException If the adapter configuration is invalid
64
     */
65
    public function __construct()
66
    {
67
        $apacheConfig = App::getConfig('apache');
68
69
        // If the adapter configuration is invalid
70
        if (!is_array($apacheConfig)) {
71
            throw new \RuntimeException('Invalid apache adapter strategy configuration', 1475500531);
72
        }
73
74
        $this->config = array_merge((array)App::getConfig('general'), $apacheConfig);
75
    }
76
77
    /**
78
     * Persist a virtual host
79
     *
80
     * @param AccountInterface $account Account
81
     * @param VhostInterface $vhost Virtual host
82
     * @return bool Success
83
     * @throws \RuntimeException If a config file couldn't be written
84
     */
85
    public function persist(AccountInterface $account, VhostInterface $vhost)
86
    {
87
        $persister = new Apache($account, $this->config);
88
        $entries = $persister($vhost);
89
90
        // Persist the file entries
91
        foreach ($entries as $filename => $filecontent) {
92
            // Check whether this file should only be written once
93
            if (substr($filename, -1) === '?') {
94
                $filename = substr($filename, 0, -1);
95
                if (@file_exists($filename)) {
96
                    continue;
97
                }
98
            }
99
100
            if (!file_put_contents($filename, $filecontent)) {
101
                throw new \RuntimeException(sprintf('Couldn\'t write config file "%s"', $filename, 1475507805));
102
            }
103
        }
104
105
        return true;
106
    }
107
}
108