Completed
Push — master ( 9003f0...ee041c )
by Joschi
04:37
created

Vhost::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 4
crap 12
1
<?php
2
3
/**
4
 * admin
5
 *
6
 * @category    Tollwerk
7
 * @package     Tollwerk\Admin
8
 * @subpackage  Tollwerk\Admin\Ports\Facade
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\Ports\Facade;
38
39
use Tollwerk\Admin\Domain\Vhost\Vhost as DomainVhost;
40
use Tollwerk\Admin\Domain\Account\Account as DomainAccount;
41
use Tollwerk\Admin\Infrastructure\App;
42
43
/**
44
 * Virtual host facade
45
 *
46
 * @package Tollwerk\Admin
47
 * @subpackage Tollwerk\Admin\Ports
48
 */
49
class Vhost
50
{
51
    /**
52
     * Create and add a virtual host to an account
53
     *
54
     * @param string $account Account name
55
     * @param string $domain Primary domain name
56
     * @param string $docroot Document root
57
     * @param string $type Virtual host type
58
     * @return boolean Success
59
     * @throws \Exception If the account couldn't be created
60
     */
61
    public static function create($account, $domain, $docroot, $type = DomainVhost::TYPE_APACHE)
0 ignored issues
show
Unused Code introduced by
The parameter $docroot is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        // Get the account to operate on
64
        $account = self::loadAccount($account);
65
66
        // Check if the domain already exists and is unassigned
67
        try {
68
            $domain = App::getDomainService()->loadAvailable($domain, $account);
69
        } catch (\RuntimeException $e) {
70
            // If the domain already exists but is assigned or belongs to another account: Error
71
            if ($e->getCode() != 1475915909) {
72
                throw $e;
73
            }
74
75
            // Create the domain
76
            App::getDomainService()->create($domain, $account);
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * Get the account to operate on
84
     *
85
     * @param string $name Account name
86
     * @return DomainAccount Account instance
87
     * @throws \Exception If the account doesn't exist
88
     */
89
    protected static function loadAccount($name) {
90
        $account = App::getAccountService()->load($name);
91
        if (!$account instanceof DomainAccount) {
92
            throw new \Exception(sprintf('Account "%s" doesn\'t exist', $name), 1475915142);
93
        }
94
        return $account;
95
    }
96
}
97