Account   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 5
dl 0
loc 110
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 3
A rename() 0 17 3
A delete() 0 20 5
A enable() 0 8 2
A disable() 0 8 2
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 © 2018 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 © 2018 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\Infrastructure\App;
40
use Tollwerk\Admin\Infrastructure\Facade\AbstractFacade;
41
use Tollwerk\Admin\Infrastructure\Shell\Exception as ShellException;
42
use Tollwerk\Admin\Infrastructure\Shell\User;
43
use Tollwerk\Admin\Domain\Account\Account as DomainAccount;
44
45
/**
46
 * Account facade
47
 *
48
 * @package Tollwerk\Admin
49
 * @subpackage Tollwerk\Admin\Ports
50
 */
51
class Account extends AbstractFacade
52
{
53
    /**
54
     * Create an account
55
     *
56
     * @param string $name Account name
57
     * @return boolean Success
58
     * @throws \Exception If the account couldn't be created
59
     */
60
    public static function create($name)
61
    {
62
        User::create($name);
63
64
        try {
65
            $account = App::getAccountService()->create($name);
66
            if (!$account instanceof DomainAccount) {
67
                throw new \Exception(sprintf('Couldn\'t create account "%s"', $name), 1475528906);
68
            }
69
70
        } catch (\Exception $e) {
71
            User::delete($name);
72
            throw $e;
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * Rename an account
80
     *
81
     * @param string $oldname Old account name
82
     * @param string $newname New account name
83
     * @return boolean Success
84
     */
85
    public static function rename($oldname, $newname)
86
    {
87
        User::rename($oldname, $newname);
88
89
        try {
90
            $account = App::getAccountService()->rename($oldname, $newname);
91
            if (!$account instanceof DomainAccount) {
92
                throw new \Exception(sprintf('Couldn\'t rename account "%s" to "%s"', $oldname, $newname), 1475531002);
93
            }
94
95
        } catch (\Exception $e) {
96
            User::rename($newname, $oldname);
97
            throw $e;
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * Delete an account
105
     *
106
     * @param string $name Account name
107
     */
108
    public static function delete($name)
109
    {
110
        // If the account cannot be deleted
111
        $deletedAccount = App::getAccountService()->delete($name);
112
        if (!($deletedAccount instanceof DomainAccount)) {
113
            throw new \RuntimeException(sprintf('Couldn\'t delete account "%s"', $name), 1475532983);
114
        }
115
116
        try {
117
            User::delete($name);
118
        } catch (ShellException $e) {
119
            $account = App::getAccountService()->create($name);
120
            if (($account instanceof DomainAccount) && $deletedAccount->isActive()) {
121
                App::getAccountService()->enable($name);
122
            }
123
            throw new \RuntimeException($e->getMessage(), $e->getCode());
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * Enable an account
131
     *
132
     * @param string $name Account name
133
     * @return bool Success
134
     * @throws \RuntimeException If the account cannot be ensabled
135
     */
136
    public static function enable($name)
137
    {
138
        // If the account cannot be enabled
139
        if (!(App::getAccountService()->enable($name) instanceof DomainAccount)) {
140
            throw new \RuntimeException(sprintf('Couldn\'t enable account "%s"', $name), 1475532231);
141
        }
142
        return true;
143
    }
144
145
    /**
146
     * Disable an account
147
     *
148
     * @param string $name Account name
149
     * @return bool Success
150
     * @throws \RuntimeException If the account cannot be disabled
151
     */
152
    public static function disable($name)
153
    {
154
        // If the account cannot be disabled
155
        if (!(App::getAccountService()->disable($name) instanceof DomainAccount)) {
156
            throw new \RuntimeException(sprintf('Couldn\'t disable account "%s"', $name), 1475532231);
157
        }
158
        return true;
159
    }
160
}
161