Completed
Push — master ( d7fc79...487414 )
by Joschi
04:26
created

DoctrineStorageAdapterStrategy   D

Complexity

Total Complexity 121

Size/Duplication

Total Lines 943
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 943
c 0
b 0
f 0
wmc 121
lcom 1
cbo 10
ccs 0
cts 337
cp 0
rs 4.4444

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B createAccount() 0 22 4
A enableAccount() 0 20 4
A disableAccount() 0 20 4
B deleteAccount() 0 22 4
A renameAccount() 0 21 4
A loadAccount() 0 11 2
A loadFromDoctrineAccount() 0 18 3
C loadDomain() 0 48 7
B createDomain() 0 31 5
B deleteDomain() 0 28 5
A enableDomain() 0 20 4
A disableDomain() 0 20 4
A enableDomainWildcard() 0 20 4
A disableDomainWildcard() 0 20 4
A loadFromDoctrineDomain() 0 4 1
C createVhost() 0 48 8
B deleteVhost() 0 27 5
B enableVhost() 0 28 5
B disableVhost() 0 28 5
B redirectVhost() 0 29 5
B phpVhost() 0 28 5
B portVhost() 0 28 5
C addVhostDomain() 0 46 8
C removeVhostDomain() 0 46 8
C loadFromDoctrineVhost() 0 36 7

How to fix   Complexity   

Complex Class

Complex classes like DoctrineStorageAdapterStrategy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DoctrineStorageAdapterStrategy, and based on these observations, apply Extract Interface, too.

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 Doctrine\DBAL\Exception\UniqueConstraintViolationException;
40
use Doctrine\ORM\EntityManager;
41
use Doctrine\ORM\EntityRepository;
42
use Tollwerk\Admin\Application\Contract\StorageAdapterStrategyInterface;
43
use Tollwerk\Admin\Domain\Account\Account;
44
use Tollwerk\Admin\Domain\Account\AccountInterface;
45
use Tollwerk\Admin\Domain\Domain\DomainInterface;
46
use Tollwerk\Admin\Domain\Factory\DomainFactory;
47
use Tollwerk\Admin\Domain\Vhost\Vhost;
48
use Tollwerk\Admin\Domain\Vhost\VhostInterface;
49
use Tollwerk\Admin\Infrastructure\App;
50
use Tollwerk\Admin\Infrastructure\Model\Account as DoctrineAccount;
51
use Tollwerk\Admin\Infrastructure\Model\Domain as DoctrineDomain;
52
use Tollwerk\Admin\Infrastructure\Model\Vhost as DoctrineVhost;
53
54
/**
55
 * DoctrineAccountFactoryStrategy
56
 *
57
 * @package Tollwerk\Admin
58
 * @subpackage Tollwerk\Admin\Infrastructure\Strategy
59
 */
60
class DoctrineStorageAdapterStrategy implements StorageAdapterStrategyInterface
61
{
62
    /**
63
     * Doctrine entity manager
64
     *
65
     * @var EntityManager
66
     */
67
    protected $entityManager;
68
    /**
69
     * Account repository
70
     *
71
     * @var EntityRepository
72
     */
73
    protected $accountRepository;
74
    /**
75
     * Domain repository
76
     *
77
     * @var EntityRepository
78
     */
79
    protected $domainRepository;
80
    /**
81
     * Virtual host repository
82
     *
83
     * @var EntityRepository
84
     */
85
    protected $vhostRepository;
86
87
    /**
88
     * Constructor
89
     */
90
    public function __construct()
91
    {
92
        $this->entityManager = App::getEntityManager();
93
        $this->accountRepository =
94
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Account::class);
95
        $this->domainRepository =
96
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Domain::class);
97
        $this->vhostRepository =
98
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Vhost::class);
99
    }
100
101
    /**
102
     * Create an account
103
     *
104
     * @param string $name Account name
105
     * @return AccountInterface Account
106
     * @throws \RuntimeException If the account cannot be created
107
     */
108
    public function createAccount($name)
109
    {
110
        // Create the new account
111
        $doctrineAccount = new DoctrineAccount();
112
        $doctrineAccount->setName($name);
113
        $doctrineAccount->setActive(false);
114
115
        // Persist the new account
116
        try {
117
            $this->entityManager->persist($doctrineAccount);
118
            $this->entityManager->flush();
119
        } catch (UniqueConstraintViolationException $e) {
120
            $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
121
        } catch (\Exception $e) {
122
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
123
        }
124
125
        // Create and return a domain account
126
        $account = new Account($doctrineAccount->getName());
127
        $account->setActive($doctrineAccount->getActive());
128
        return $account;
129
    }
130
131
    /**
132
     * Enable an account
133
     *
134
     * @param string $name Account name
135
     * @return AccountInterface Account
136
     * @throws \RuntimeException If the account cannot be enabled
137
     */
138
    public function enableAccount($name)
139
    {
140
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
141
142
        // If the account is unknown
143
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
144
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
145
        }
146
147
        // Enable and persist the account
148
        try {
149
            $doctrineAccount->setActive(true);
150
            $this->entityManager->persist($doctrineAccount);
151
            $this->entityManager->flush();
152
        } catch (\Exception $e) {
153
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
154
        }
155
156
        return $this->loadFromDoctrineAccount($doctrineAccount);
157
    }
158
159
    /**
160
     * Disable an account
161
     *
162
     * @param string $name Account name
163
     * @return AccountInterface Account
164
     * @throws \RuntimeException If the account cannot be disabled
165
     */
166
    public function disableAccount($name)
167
    {
168
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
169
170
        // If the account is unknown
171
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
172
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
173
        }
174
175
        // Enable and persist the account
176
        try {
177
            $doctrineAccount->setActive(false);
178
            $this->entityManager->persist($doctrineAccount);
179
            $this->entityManager->flush();
180
        } catch (\Exception $e) {
181
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
182
        }
183
184
        return $this->loadFromDoctrineAccount($doctrineAccount);
185
    }
186
187
    /**
188
     * Delete an account
189
     *
190
     * @param string $name Account name
191
     * @return AccountInterface Account
192
     * @throws \RuntimeException If the account cannot be deleted
193
     */
194
    public function deleteAccount($name)
195
    {
196
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
197
198
        // If the account is unknown
199
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
200
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
201
        }
202
203
        // Enable and persist the account
204
        try {
205
            $this->entityManager->remove($doctrineAccount);
206
            $this->entityManager->flush();
207
        } catch (\Exception $e) {
208
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
209
        }
210
211
        // Create and return a domain account stub
212
        $account = new Account($doctrineAccount->getName());
213
        $account->setActive($doctrineAccount->getActive());
214
        return $account;
215
    }
216
217
    /**
218
     * Rename and return an account
219
     *
220
     * @param string $oldname Old account name
221
     * @param string $newname New account name
222
     * @return AccountInterface Account
223
     * @throws \RuntimeException If the account is unknown
224
     */
225
    public function renameAccount($oldname, $newname)
226
    {
227
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $oldname]);
228
229
        // If the account is unknown
230
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
231
            throw new \RuntimeException(sprintf('Unknown account "%s"', $oldname), 1475495500);
232
        }
233
234
        // Rename and persist the account
235
        try {
236
            $doctrineAccount->setName($newname);
237
            $this->entityManager->persist($doctrineAccount);
238
            $this->entityManager->flush();
239
        } catch (\Exception $e) {
240
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
241
        }
242
243
        // Load and return the renamed account
244
        return $this->loadFromDoctrineAccount($doctrineAccount);
245
    }
246
247
    /**
248
     * Instantiate an account
249
     *
250
     * @param string $name Account name
251
     * @return AccountInterface Account
252
     * @throws \RuntimeException If the account is unknown
253
     */
254
    public function loadAccount($name)
255
    {
256
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
257
258
        // If the account is unknown
259
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
260
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
261
        }
262
263
        return $this->loadFromDoctrineAccount($doctrineAccount);
264
    }
265
266
    /**
267
     * Create domain account from doctrine account
268
     *
269
     * @param DoctrineAccount $doctrineAccount Doctrine account
270
     * @return AccountInterface Domain account
271
     */
272
    protected function loadFromDoctrineAccount(DoctrineAccount $doctrineAccount)
273
    {
274
        $account = new Account($doctrineAccount->getName());
275
        $account->setActive($doctrineAccount->isActive());
276
277
        // Run through all virtual hosts of this account
278
        /** @var DoctrineVhost $doctrineVhost */
279
        foreach ($doctrineAccount->getVhosts() as $doctrineVhost) {
280
            $doctrinePrimaryDomain = $doctrineVhost->getPrimarydomain();
281
            if ($doctrinePrimaryDomain instanceof DoctrineDomain) {
282
                $account->addVirtualHost($this->loadFromDoctrineVhost($doctrineVhost));
283
            } else {
284
                trigger_error('Skipping virtual host due to missing primary domain', E_USER_NOTICE);
285
            }
286
        }
287
288
        return $account;
289
    }
290
291
    /**
292
     * Load a domain (optionally: unassigned)
293
     *
294
     * @param string $name Domain name
295
     * @param AccountInterface $account Optional: Account the domain must belong to
296
     * @param string $vhostDocroot Optional: Document root of the virtual host the domain must be assigned to (otherwise: unassigned)
297
     * @return DomainInterface Domain
298
     * @throws \RuntimeException If the domain is unknown
299
     * @throws \RuntimeException If the domain belongs to another account
300
     * @throws \RuntimeException If the domain is assigned to a virtual host but should be unassigned
301
     * @throws \RuntimeException If the domain is assigned to a different virtual host
302
     */
303
    public function loadDomain($name, AccountInterface $account = null, $vhostDocroot = null)
304
    {
305
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
306
307
        // If the domain is unknown
308
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
309
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475915909);
310
        }
311
312
        // If only an assigned or unassigned account domain should be returned
313
        if ($account instanceof Account) {
314
            // If the domain belongs to another account
315
            if ($doctrineDomain->getAccount()->getName() != $account->getName()) {
316
                throw new \RuntimeException(
317
                    sprintf(
318
                        'Domain "%s" belongs to another account ("%s")',
319
                        $name,
320
                        $doctrineDomain->getAccount()->getName()
321
                    ),
322
                    1475917184
323
                );
324
            }
325
326
            // Determine the virtual host the domain is assigned to
327
            $doctrineVhost = $doctrineDomain->getVhost();
328
329
            // If the domain is already assigned to another virtual host
330
            if ($doctrineVhost instanceof DoctrineVhost) {
331
                // If only an uassigned domain should be returned
332
                if ($vhostDocroot === null) {
333
                    throw new \RuntimeException(
334
                        sprintf('Domain "%s" is already assigned to a virtual host', $name),
335
                        1475917298
336
                    );
337
                }
338
339
                // If the document route doesn't match the requested one
340
                if ($doctrineVhost->getDocroot() !== $vhostDocroot) {
341
                    throw new \RuntimeException(
342
                        sprintf('Domain "%s" is assigned to another virtual host', $name),
343
                        1475942759
344
                    );
345
                }
346
            }
347
        }
348
349
        return $this->loadFromDoctrineDomain($doctrineDomain);
350
    }
351
352
    /**
353
     * Create a domain
354
     *
355
     * @param string $name Domain name
356
     * @param Account $account Account the domain belongs to
357
     * @return DomainInterface Domain
358
     * @throws \RuntimeException If the account is unknown
359
     */
360
    public function createDomain($name, AccountInterface $account)
361
    {
362
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
363
364
        // If the account is unknown
365
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
366
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
367
        }
368
369
        // Create the new domain
370
        $doctrineDomain = new DoctrineDomain();
371
        $doctrineDomain->setAccount($doctrineAccount);
372
        $doctrineDomain->setActive(false);
373
        $doctrineDomain->setName($name);
374
        $doctrineDomain->setWildcard(false);
375
        $doctrineDomain->setPrimarydomain(false);
376
377
        // Persist the new domain
378
        try {
379
            $this->entityManager->persist($doctrineDomain);
380
            $this->entityManager->flush();
381
        } catch (UniqueConstraintViolationException $e) {
382
            $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
383
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
384
        } catch (\Exception $e) {
385
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
386
        }
387
388
        // Create and return a domain domain
389
        return $this->loadFromDoctrineDomain($doctrineDomain);
0 ignored issues
show
Documentation introduced by
$doctrineDomain is of type object|null, but the function expects a object<Tollwerk\Admin\In...structure\Model\Domain>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
390
    }
391
392
    /**
393
     * Delete a domain
394
     *
395
     * @param string $name Domain name
396
     * @return DomainInterface Domain
397
     * @throws \RuntimeException If the domain cannot be deleted
398
     */
399
    public function deleteDomain($name)
400
    {
401
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
402
403
        // If the domain is unknown
404
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
405
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
406
        }
407
408
        // If the domain is assigned to a virtual host
409
        if ($doctrineDomain->getVhost() instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
410
            throw new \RuntimeException(
411
                sprintf('Domain "%s" is assigned to a virtual host, please release the assignment first', $name),
412
                1475921740
413
            );
414
        }
415
416
        // Remove and persist the domain
417
        try {
418
            $this->entityManager->remove($doctrineDomain);
419
            $this->entityManager->flush();
420
        } catch (\Exception $e) {
421
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
422
        }
423
424
        // Create and return a domain domain stub
425
        return $this->loadFromDoctrineDomain($doctrineDomain);
426
    }
427
428
    /**
429
     * Enable a domain
430
     *
431
     * @param string $name Domain name
432
     * @return DomainInterface Domain
433
     * @throws \RuntimeException If the domain cannot be enabled
434
     */
435
    public function enableDomain($name)
436
    {
437
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
438
439
        // If the domain is unknown
440
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
441
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
442
        }
443
444
        // Enable and persist the account
445
        try {
446
            $doctrineDomain->setActive(true);
447
            $this->entityManager->persist($doctrineDomain);
448
            $this->entityManager->flush();
449
        } catch (\Exception $e) {
450
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
451
        }
452
453
        return $this->loadFromDoctrineDomain($doctrineDomain);
454
    }
455
456
    /**
457
     * Disable a domain
458
     *
459
     * @param string $name Domain name
460
     * @return DomainInterface Domain
461
     * @throws \RuntimeException If the domain cannot be disabled
462
     */
463
    public function disableDomain($name)
464
    {
465
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
466
467
        // If the domain is unknown
468
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
469
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
470
        }
471
472
        // Enable and persist the account
473
        try {
474
            $doctrineDomain->setActive(false);
475
            $this->entityManager->persist($doctrineDomain);
476
            $this->entityManager->flush();
477
        } catch (\Exception $e) {
478
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
479
        }
480
481
        return $this->loadFromDoctrineDomain($doctrineDomain);
482
    }
483
484
    /**
485
     * Enable a domain wildcard
486
     *
487
     * @param string $name Domain name
488
     * @return DomainInterface Domain
489
     * @throws \RuntimeException If the domain cannot be enabled
490
     */
491
    public function enableDomainWildcard($name)
492
    {
493
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
494
495
        // If the domain is unknown
496
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
497
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
498
        }
499
500
        // Enable and persist the account
501
        try {
502
            $doctrineDomain->setWildcard(true);
503
            $this->entityManager->persist($doctrineDomain);
504
            $this->entityManager->flush();
505
        } catch (\Exception $e) {
506
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
507
        }
508
509
        return $this->loadFromDoctrineDomain($doctrineDomain);
510
    }
511
512
    /**
513
     * Disable a domain wildcard
514
     *
515
     * @param string $name Domain name
516
     * @return DomainInterface Domain
517
     * @throws \RuntimeException If the domain cannot be disabled
518
     */
519
    public function disableDomainWildcard($name)
520
    {
521
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
522
523
        // If the domain is unknown
524
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
525
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
526
        }
527
528
        // Enable and persist the account
529
        try {
530
            $doctrineDomain->setWildcard(false);
531
            $this->entityManager->persist($doctrineDomain);
532
            $this->entityManager->flush();
533
        } catch (\Exception $e) {
534
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
535
        }
536
537
        return $this->loadFromDoctrineDomain($doctrineDomain);
538
    }
539
540
    /**
541
     * Create domain domain from doctrine domain
542
     *
543
     * @param DoctrineDomain $doctrineDomain Doctrine domain
544
     * @return DomainInterface Domain domain
545
     */
546
    protected function loadFromDoctrineDomain(DoctrineDomain $doctrineDomain)
547
    {
548
        return DomainFactory::parseString($doctrineDomain->getName());
549
    }
550
551
    /**
552
     * Create a virtual host
553
     *
554
     * @param AccountInterface $account Account name
555
     * @param DomainInterface $domain Domain
556
     * @param string $docroot Document root
557
     * @param string $type Virtual host Type
558
     * @return VhostInterface Virtual host
559
     * @throws \RuntimeException If the account is unknown
560
     * @throws \RuntimeException If the domain is unknown
561
     */
562
    public function createVhost(AccountInterface $account, DomainInterface $domain, $docroot, $type)
563
    {
564
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
565
566
        // If the account is unknown
567
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
568
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
569
        }
570
571
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
572
573
        // If the domain is unknown
574
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
575
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
576
        }
577
578
        // Create the new domain
579
        $doctrineVhost = new DoctrineVhost();
580
        $doctrineVhost->setAccount($doctrineAccount);
581
        $doctrineVhost->setActive(false);
582
        $doctrineVhost->setType($type);
583
        $doctrineVhost->setDocroot($docroot);
584
585
        // Persist the new virtual host
586
        try {
587
            $this->entityManager->persist($doctrineVhost);
588
            $this->entityManager->flush();
589
        } catch (UniqueConstraintViolationException $e) {
590
            $doctrineVhost = $this->vhostRepository->findOneBy(['docroot' => $docroot]);
591
        } catch (\Exception $e) {
592
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
593
        }
594
595
        // Set the primary domain
596
        $doctrineDomain->setVhost($doctrineVhost);
0 ignored issues
show
Bug introduced by
It seems like $doctrineVhost defined by $this->vhostRepository->...'docroot' => $docroot)) on line 590 can also be of type object; however, Tollwerk\Admin\Infrastru...odel\Domain::setVhost() does only seem to accept object<Tollwerk\Admin\In...cture\Model\Vhost>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
597
        $doctrineDomain->setPrimarydomain(true);
598
599
        // Persist the domain
600
        try {
601
            $this->entityManager->persist($doctrineDomain);
602
            $this->entityManager->flush();
603
        } catch (\Exception $e) {
604
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
605
        }
606
607
        // Create and return a domain domain
608
        return $this->loadFromDoctrineVhost($doctrineVhost);
0 ignored issues
show
Documentation introduced by
$doctrineVhost is of type object|null, but the function expects a object<Tollwerk\Admin\Infrastructure\Model\Vhost>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
609
    }
610
611
    /**
612
     * Delete a virtual host
613
     *
614
     * @param AccountInterface $account Account name
615
     * @param string $docroot Document root
616
     * @return VhostInterface Virtual host
617
     * @throws \RuntimeException If the account is unknown
618
     * @throws \RuntimeException If the virtual host is unknown
619
     */
620
    public function deleteVhost(AccountInterface $account, $docroot)
621
    {
622
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
623
624
        // If the account is unknown
625
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
626
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
627
        }
628
629
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
630
631
        // If the virtual host is unknown
632
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
633
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
634
        }
635
636
        // Remove and persist the virtual host
637
        try {
638
            $this->entityManager->remove($doctrineVhost);
639
            $this->entityManager->flush();
640
        } catch (\Exception $e) {
641
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
642
        }
643
644
        // Create and return a domain domain
645
        return $this->loadFromDoctrineVhost($doctrineVhost);
646
    }
647
648
    /**
649
     * Enable a virtual host
650
     *
651
     * @param AccountInterface $account Account name
652
     * @param string $docroot Document root
653
     * @return VhostInterface Virtual host
654
     * @throws \RuntimeException If the account is unknown
655
     * @throws \RuntimeException If the virtual host is unknown
656
     */
657
    public function enableVhost(AccountInterface $account, $docroot)
658
    {
659
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
660
661
        // If the account is unknown
662
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
663
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
664
        }
665
666
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
667
668
        // If the virtual host is unknown
669
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
670
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
671
        }
672
673
        // Update and persist the virtual host
674
        try {
675
            $doctrineVhost->setActive(true);
676
            $this->entityManager->persist($doctrineVhost);
677
            $this->entityManager->flush();
678
        } catch (\Exception $e) {
679
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
680
        }
681
682
        // Create and return a domain domain
683
        return $this->loadFromDoctrineVhost($doctrineVhost);
684
    }
685
686
    /**
687
     * Disable a virtual host
688
     *
689
     * @param AccountInterface $account Account name
690
     * @param string $docroot Document root
691
     * @return VhostInterface Virtual host
692
     * @throws \RuntimeException If the account is unknown
693
     * @throws \RuntimeException If the virtual host is unknown
694
     */
695
    public function disableVhost(AccountInterface $account, $docroot)
696
    {
697
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
698
699
        // If the account is unknown
700
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
701
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
702
        }
703
704
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
705
706
        // If the virtual host is unknown
707
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
708
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
709
        }
710
711
        // Update and persist the virtual host
712
        try {
713
            $doctrineVhost->setActive(false);
714
            $this->entityManager->persist($doctrineVhost);
715
            $this->entityManager->flush();
716
        } catch (\Exception $e) {
717
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
718
        }
719
720
        // Create and return a domain domain
721
        return $this->loadFromDoctrineVhost($doctrineVhost);
722
    }
723
724
    /**
725
     * Redirect a virtual host
726
     *
727
     * @param AccountInterface $account Account name
728
     * @param string $docroot Document root
729
     * @param string|null $url Redirect URL
730
     * @param int $status Redirect HTTP status
731
     * @return VhostInterface Virtual host
732
     * @throws \RuntimeException If the account is unknown
733
     * @throws \RuntimeException If the virtual host is unknown
734
     */
735
    public function redirectVhost(AccountInterface $account, $docroot, $url, $status)
736
    {
737
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
738
739
        // If the account is unknown
740
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
741
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
742
        }
743
744
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
745
746
        // If the virtual host is unknown
747
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
748
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
749
        }
750
751
        // Update and persist the virtual host
752
        try {
753
            $doctrineVhost->setRedirecturl($url);
754
            $doctrineVhost->setRedirectstatus($status);
755
            $this->entityManager->persist($doctrineVhost);
756
            $this->entityManager->flush();
757
        } catch (\Exception $e) {
758
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
759
        }
760
761
        // Create and return a domain domain
762
        return $this->loadFromDoctrineVhost($doctrineVhost);
763
    }
764
765
    /**
766
     * Configure the PHP version of a virtual host
767
     *
768
     * @param AccountInterface $account Account name
769
     * @param string $docroot Document root
770
     * @param string|null $php PHP version
771
     * @return VhostInterface Virtual host
772
     * @throws \RuntimeException If the account is unknown
773
     * @throws \RuntimeException If the virtual host is unknown
774
     */
775
    public function phpVhost(AccountInterface $account, $docroot, $php)
776
    {
777
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
778
779
        // If the account is unknown
780
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
781
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
782
        }
783
784
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
785
786
        // If the virtual host is unknown
787
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
788
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
789
        }
790
791
        // Update and persist the virtual host
792
        try {
793
            $doctrineVhost->setPhp($php);
794
            $this->entityManager->persist($doctrineVhost);
795
            $this->entityManager->flush();
796
        } catch (\Exception $e) {
797
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
798
        }
799
800
        // Create and return a domain domain
801
        return $this->loadFromDoctrineVhost($doctrineVhost);
802
    }
803
804
    /**
805
     * Configure a protocol based port for a virtual host
806
     *
807
     * @param string $account Account name
808
     * @param string $docroot Document root
809
     * @param int $protocol Protocol
810
     * @param int $port Port
811
     * @return VhostInterface Virtual host
812
     * @throws \RuntimeException If the account is unknown
813
     * @throws \RuntimeException If the virtual host is unknown
814
     */
815
    public function portVhost(AccountInterface $account, $docroot, $protocol, $port)
816
    {
817
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
818
819
        // If the account is unknown
820
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
821
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
822
        }
823
824
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
825
826
        // If the virtual host is unknown
827
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
828
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
829
        }
830
831
        // Update and persist the virtual host
832
        try {
833
            $doctrineVhost->{'set'.ucfirst(Vhost::$supportedProtocols[$protocol]).'port'}($port);
834
            $this->entityManager->persist($doctrineVhost);
835
            $this->entityManager->flush();
836
        } catch (\Exception $e) {
837
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
838
        }
839
840
        // Create and return a domain domain
841
        return $this->loadFromDoctrineVhost($doctrineVhost);
842
    }
843
844
    /**
845
     * Add a secondary domain to a virtual host
846
     *
847
     * @param string $account Account name
848
     * @param string $docroot Document root
849
     * @param DomainInterface $domain Domain
850
     * @return VhostInterface Virtual host
851
     * @throws \RuntimeException If the account is unknown
852
     * @throws \RuntimeException If the virtual host is unknown
853
     * @throws \RuntimeException If the domain is unknown
854
     */
855
    public function addVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
856
    {
857
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
858
859
        // If the account is unknown
860
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
861
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
862
        }
863
864
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
865
866
        // If the virtual host is unknown
867
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
868
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
869
        }
870
871
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
872
873
        // If the domain is unknown
874
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
875
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
876
        }
877
878
        // If the domain is already assigned to a virtual host
879
        $doctrineDomainVhost = $doctrineDomain->getVhost();
880
        if (($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
881
            && ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
882
        ) {
883
            throw new \RuntimeException(
884
                sprintf('Domain "%s" is already assigned to a virtual host', strval($domain)),
885
                1475917298
886
            );
887
        }
888
889
        // Update and persist the virtual host
890
        try {
891
            $doctrineDomain->setVhost($doctrineVhost);
892
            $this->entityManager->persist($doctrineDomain);
893
            $this->entityManager->flush();
894
        } catch (\Exception $e) {
895
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
896
        }
897
898
        // Create and return a domain domain
899
        return $this->loadFromDoctrineVhost($doctrineVhost);
900
    }
901
902
    /**
903
     * Remove a secondary domain from a virtual host
904
     *
905
     * @param string $account Account name
906
     * @param string $docroot Document root
907
     * @param DomainInterface $domain Domain
908
     * @return VhostInterface Virtual host
909
     * @throws \RuntimeException If the account is unknown
910
     * @throws \RuntimeException If the virtual host is unknown
911
     * @throws \RuntimeException If the domain is unknown
912
     */
913
    public function removeVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
914
    {
915
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
916
917
        // If the account is unknown
918
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
919
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
920
        }
921
922
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
923
924
        // If the virtual host is unknown
925
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
926
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
927
        }
928
929
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
930
931
        // If the domain is unknown
932
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
933
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
934
        }
935
936
        // If the domain is not assigned to the current virtual host
937
        $doctrineDomainVhost = $doctrineDomain->getVhost();
938
        if (!($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
939
            || ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
940
        ) {
941
            throw new \RuntimeException(
942
                sprintf('Domain "%s" is not assigned to this virtual host', strval($domain)),
943
                1475942759
944
            );
945
        }
946
947
        // Update and persist the virtual host
948
        try {
949
            $doctrineDomain->setVhost(null);
950
            $this->entityManager->persist($doctrineDomain);
951
            $this->entityManager->flush();
952
        } catch (\Exception $e) {
953
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
954
        }
955
956
        // Create and return a domain domain
957
        return $this->loadFromDoctrineVhost($doctrineVhost);
958
    }
959
960
    /**
961
     * Create virtual host from a doctrine virtual host
962
     *
963
     * @param DoctrineVhost $doctrineVhost Doctrine virtual host
964
     * @return VhostInterface Virtual host
965
     */
966
    protected function loadFromDoctrineVhost(DoctrineVhost $doctrineVhost)
967
    {
968
        $doctrinePrimaryDomain = $doctrineVhost->getPrimarydomain();
969
        $primaryDomain = $this->loadFromDoctrineDomain($doctrinePrimaryDomain);
970
971
        $vhost = new Vhost($primaryDomain, $doctrineVhost->getDocroot(), $doctrineVhost->getType());
972
        $vhost->setActive($doctrineVhost->isActive());
973
        $vhost->setPhp($doctrineVhost->getPhp());
974
        $vhost->setRedirectUrl($doctrineVhost->getRedirecturl());
975
        $vhost->setRedirectStatus($doctrineVhost->getRedirectstatus());
976
        if ($doctrineVhost->getHttpport() !== null) {
977
            $vhost->enableProtocol(Vhost::PROTOCOL_HTTP, $doctrineVhost->getHttpport());
978
        }
979
        if ($doctrineVhost->getHttpsport() !== null) {
980
            $vhost->enableProtocol(Vhost::PROTOCOL_HTTPS, $doctrineVhost->getHttpsport());
981
        }
982
983
        // Add the wilcard version of the primary domain
984
        if ($doctrinePrimaryDomain->isWildcard()) {
985
            $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$primaryDomain));
986
        }
987
988
        // Add all secondary domains
989
        /** @var DoctrineDomain $doctrineDomain */
990
        foreach ($doctrineVhost->getDomains() as $doctrineDomain) {
991
            if ($doctrineDomain->getName() != $doctrinePrimaryDomain->getName()) {
992
                $vhost->addSecondaryDomain(DomainFactory::parseString($doctrineDomain->getName()));
993
994
                if ($doctrineDomain->isWildcard()) {
995
                    $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$doctrineDomain->getName()));
996
                }
997
            }
998
        }
999
1000
        return $vhost;
1001
    }
1002
}
1003