DoctrineStorageAdapterStrategy   F
last analyzed

Complexity

Total Complexity 133

Size/Duplication

Total Lines 1044
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 133
lcom 1
cbo 11
dl 0
loc 1044
ccs 0
cts 394
cp 0
rs 1.424
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createAccount() 0 22 4
A enableAccount() 0 21 4
A disableAccount() 0 21 4
A deleteAccount() 0 22 4
A renameAccount() 0 22 4
A loadAccount() 0 11 2
A loadFromDoctrineAccount() 0 18 3
B loadDomain() 0 48 7
A createDomain() 0 32 5
A deleteDomain() 0 28 5
A enableDomain() 0 21 4
A disableDomain() 0 21 4
A enableDomainWildcard() 0 21 4
A disableDomainWildcard() 0 21 4
A loadFromDoctrineDomain() 0 4 1
A loadVhost() 0 14 2
B createVhost() 0 49 8
A deleteVhost() 0 27 5
A enableVhost() 0 29 5
A disableVhost() 0 29 5
A redirectVhost() 0 30 5
A phpVhost() 0 29 5
B addVhostPort() 0 44 8
B removeVhostPort() 0 37 8
B addVhostDomain() 0 47 8
B removeVhostDomain() 0 46 8
B loadFromDoctrineVhost() 0 36 6

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 © 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\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\Port as DoctrinePort;
53
use Tollwerk\Admin\Infrastructure\Model\Vhost as DoctrineVhost;
54
55
/**
56
 * DoctrineAccountFactoryStrategy
57
 *
58
 * @package Tollwerk\Admin
59
 * @subpackage Tollwerk\Admin\Infrastructure\Strategy
60
 */
61
class DoctrineStorageAdapterStrategy implements StorageAdapterStrategyInterface
62
{
63
    /**
64
     * Doctrine entity manager
65
     *
66
     * @var EntityManager
67
     */
68
    protected $entityManager;
69
    /**
70
     * Account repository
71
     *
72
     * @var EntityRepository
73
     */
74
    protected $accountRepository;
75
    /**
76
     * Domain repository
77
     *
78
     * @var EntityRepository
79
     */
80
    protected $domainRepository;
81
    /**
82
     * Virtual host repository
83
     *
84
     * @var EntityRepository
85
     */
86
    protected $vhostRepository;
87
88
    /**
89
     * Constructor
90
     */
91
    public function __construct()
92
    {
93
        $this->entityManager = App::getEntityManager();
94
        $this->accountRepository =
95
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Account::class);
96
        $this->domainRepository =
97
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Domain::class);
98
        $this->vhostRepository =
99
            $this->entityManager->getRepository(\Tollwerk\Admin\Infrastructure\Model\Vhost::class);
100
    }
101
102
    /**
103
     * Create an account
104
     *
105
     * @param string $name Account name
106
     * @return AccountInterface Account
107
     * @throws \RuntimeException If the account cannot be created
108
     */
109
    public function createAccount($name)
110
    {
111
        // Create the new account
112
        $doctrineAccount = new DoctrineAccount();
113
        $doctrineAccount->setName($name);
114
        $doctrineAccount->setActive(false);
115
116
        // Persist the new account
117
        try {
118
            $this->entityManager->persist($doctrineAccount);
119
            $this->entityManager->flush();
120
        } catch (UniqueConstraintViolationException $e) {
121
            $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
122
        } catch (\Exception $e) {
123
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
124
        }
125
126
        // Create and return a domain account
127
        $account = new Account($doctrineAccount->getName());
128
        $account->setActive($doctrineAccount->getActive());
129
        return $account;
130
    }
131
132
    /**
133
     * Enable an account
134
     *
135
     * @param string $name Account name
136
     * @return AccountInterface Account
137
     * @throws \RuntimeException If the account cannot be enabled
138
     */
139
    public function enableAccount($name)
140
    {
141
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
142
143
        // If the account is unknown
144
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
145
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
146
        }
147
148
        // Enable and persist the account
149
        try {
150
            $doctrineAccount->setActive(true);
151
            $this->entityManager->persist($doctrineAccount);
152
            $this->entityManager->flush();
153
            $this->entityManager->refresh($doctrineAccount);
154
        } catch (\Exception $e) {
155
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
156
        }
157
158
        return $this->loadFromDoctrineAccount($doctrineAccount);
159
    }
160
161
    /**
162
     * Disable an account
163
     *
164
     * @param string $name Account name
165
     * @return AccountInterface Account
166
     * @throws \RuntimeException If the account cannot be disabled
167
     */
168
    public function disableAccount($name)
169
    {
170
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
171
172
        // If the account is unknown
173
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
174
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
175
        }
176
177
        // Enable and persist the account
178
        try {
179
            $doctrineAccount->setActive(false);
180
            $this->entityManager->persist($doctrineAccount);
181
            $this->entityManager->flush();
182
            $this->entityManager->refresh($doctrineAccount);
183
        } catch (\Exception $e) {
184
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
185
        }
186
187
        return $this->loadFromDoctrineAccount($doctrineAccount);
188
    }
189
190
    /**
191
     * Delete an account
192
     *
193
     * @param string $name Account name
194
     * @return AccountInterface Account
195
     * @throws \RuntimeException If the account cannot be deleted
196
     */
197
    public function deleteAccount($name)
198
    {
199
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
200
201
        // If the account is unknown
202
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
203
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
204
        }
205
206
        // Enable and persist the account
207
        try {
208
            $this->entityManager->remove($doctrineAccount);
209
            $this->entityManager->flush();
210
        } catch (\Exception $e) {
211
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
212
        }
213
214
        // Create and return a domain account stub
215
        $account = new Account($doctrineAccount->getName());
216
        $account->setActive($doctrineAccount->getActive());
217
        return $account;
218
    }
219
220
    /**
221
     * Rename and return an account
222
     *
223
     * @param string $oldname Old account name
224
     * @param string $newname New account name
225
     * @return AccountInterface Account
226
     * @throws \RuntimeException If the account is unknown
227
     */
228
    public function renameAccount($oldname, $newname)
229
    {
230
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $oldname]);
231
232
        // If the account is unknown
233
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
234
            throw new \RuntimeException(sprintf('Unknown account "%s"', $oldname), 1475495500);
235
        }
236
237
        // Rename and persist the account
238
        try {
239
            $doctrineAccount->setName($newname);
240
            $this->entityManager->persist($doctrineAccount);
241
            $this->entityManager->flush();
242
            $this->entityManager->refresh($doctrineAccount);
243
        } catch (\Exception $e) {
244
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
245
        }
246
247
        // Load and return the renamed account
248
        return $this->loadFromDoctrineAccount($doctrineAccount);
249
    }
250
251
    /**
252
     * Instantiate an account
253
     *
254
     * @param string $name Account name
255
     * @return AccountInterface Account
256
     * @throws \RuntimeException If the account is unknown
257
     */
258
    public function loadAccount($name)
259
    {
260
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $name]);
261
262
        // If the account is unknown
263
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
264
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
265
        }
266
267
        return $this->loadFromDoctrineAccount($doctrineAccount);
268
    }
269
270
    /**
271
     * Create domain account from doctrine account
272
     *
273
     * @param DoctrineAccount $doctrineAccount Doctrine account
274
     * @return AccountInterface Domain account
275
     */
276
    protected function loadFromDoctrineAccount(DoctrineAccount $doctrineAccount)
277
    {
278
        $account = new Account($doctrineAccount->getName());
279
        $account->setActive($doctrineAccount->isActive());
280
281
        // Run through all virtual hosts of this account
282
        /** @var DoctrineVhost $doctrineVhost */
283
        foreach ($doctrineAccount->getVhosts() as $doctrineVhost) {
284
            $doctrinePrimaryDomain = $doctrineVhost->getPrimarydomain();
285
            if ($doctrinePrimaryDomain instanceof DoctrineDomain) {
286
                $account->addVirtualHost($this->loadFromDoctrineVhost($doctrineVhost));
287
            } else {
288
                trigger_error('Skipping virtual host due to missing primary domain', E_USER_NOTICE);
289
            }
290
        }
291
292
        return $account;
293
    }
294
295
    /**
296
     * Load a domain (optionally: unassigned)
297
     *
298
     * @param string $name Domain name
299
     * @param AccountInterface $account Optional: Account the domain must belong to
300
     * @param string $vhostDocroot Optional: Document root of the virtual host the domain must be assigned to (otherwise: unassigned)
301
     * @return DomainInterface Domain
302
     * @throws \RuntimeException If the domain is unknown
303
     * @throws \RuntimeException If the domain belongs to another account
304
     * @throws \RuntimeException If the domain is assigned to a virtual host but should be unassigned
305
     * @throws \RuntimeException If the domain is assigned to a different virtual host
306
     */
307
    public function loadDomain($name, AccountInterface $account = null, $vhostDocroot = null)
308
    {
309
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
310
311
        // If the domain is unknown
312
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
313
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475915909);
314
        }
315
316
        // If only an assigned or unassigned account domain should be returned
317
        if ($account instanceof Account) {
318
            // If the domain belongs to another account
319
            if ($doctrineDomain->getAccount()->getName() != $account->getName()) {
320
                throw new \RuntimeException(
321
                    sprintf(
322
                        'Domain "%s" belongs to another account ("%s")',
323
                        $name,
324
                        $doctrineDomain->getAccount()->getName()
325
                    ),
326
                    1475917184
327
                );
328
            }
329
330
            // Determine the virtual host the domain is assigned to
331
            $doctrineVhost = $doctrineDomain->getVhost();
332
333
            // If the domain is already assigned to another virtual host
334
            if ($doctrineVhost instanceof DoctrineVhost) {
335
                // If only an unassigned domain should be returned
336
                if ($vhostDocroot === null) {
337
                    throw new \RuntimeException(
338
                        sprintf('Domain "%s" is already assigned to a virtual host', $name),
339
                        1475917298
340
                    );
341
                }
342
343
                // If the document route doesn't match the requested one
344
                if ($doctrineVhost->getDocroot() !== $vhostDocroot) {
345
                    throw new \RuntimeException(
346
                        sprintf('Domain "%s" is assigned to another virtual host1', $name),
347
                        1475942759
348
                    );
349
                }
350
            }
351
        }
352
353
        return $this->loadFromDoctrineDomain($doctrineDomain);
354
    }
355
356
    /**
357
     * Create a domain
358
     *
359
     * @param string $name Domain name
360
     * @param Account $account Account the domain belongs to
361
     * @return DomainInterface Domain
362
     * @throws \RuntimeException If the account is unknown
363
     */
364
    public function createDomain($name, AccountInterface $account)
365
    {
366
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
367
368
        // If the account is unknown
369
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
370
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
371
        }
372
373
        // Create the new domain
374
        $doctrineDomain = new DoctrineDomain();
375
        $doctrineDomain->setAccount($doctrineAccount);
376
        $doctrineDomain->setActive(false);
377
        $doctrineDomain->setName($name);
378
        $doctrineDomain->setWildcard(false);
379
        $doctrineDomain->setPrimarydomain(false);
380
381
        // Persist the new domain
382
        try {
383
            $this->entityManager->persist($doctrineDomain);
384
            $this->entityManager->flush();
385
            $this->entityManager->refresh($doctrineDomain);
386
        } catch (UniqueConstraintViolationException $e) {
387
            $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
388
389
        } catch (\Exception $e) {
390
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
391
        }
392
393
        // Create and return a domain domain
394
        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...
395
    }
396
397
    /**
398
     * Delete a domain
399
     *
400
     * @param string $name Domain name
401
     * @return DomainInterface Domain
402
     * @throws \RuntimeException If the domain cannot be deleted
403
     */
404
    public function deleteDomain($name)
405
    {
406
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
407
408
        // If the domain is unknown
409
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
410
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
411
        }
412
413
        // If the domain is assigned to a virtual host
414
        if ($doctrineDomain->getVhost() instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
415
            throw new \RuntimeException(
416
                sprintf('Domain "%s" is assigned to a virtual host, please release the assignment first', $name),
417
                1475921740
418
            );
419
        }
420
421
        // Remove and persist the domain
422
        try {
423
            $this->entityManager->remove($doctrineDomain);
424
            $this->entityManager->flush();
425
        } catch (\Exception $e) {
426
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
427
        }
428
429
        // Create and return a domain domain stub
430
        return $this->loadFromDoctrineDomain($doctrineDomain);
431
    }
432
433
    /**
434
     * Enable a domain
435
     *
436
     * @param string $name Domain name
437
     * @return DomainInterface Domain
438
     * @throws \RuntimeException If the domain cannot be enabled
439
     */
440
    public function enableDomain($name)
441
    {
442
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
443
444
        // If the domain is unknown
445
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
446
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
447
        }
448
449
        // Enable and persist the account
450
        try {
451
            $doctrineDomain->setActive(true);
452
            $this->entityManager->persist($doctrineDomain);
453
            $this->entityManager->flush();
454
            $this->entityManager->refresh($doctrineDomain);
455
        } catch (\Exception $e) {
456
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
457
        }
458
459
        return $this->loadFromDoctrineDomain($doctrineDomain);
460
    }
461
462
    /**
463
     * Disable a domain
464
     *
465
     * @param string $name Domain name
466
     * @return DomainInterface Domain
467
     * @throws \RuntimeException If the domain cannot be disabled
468
     */
469
    public function disableDomain($name)
470
    {
471
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
472
473
        // If the domain is unknown
474
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
475
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
476
        }
477
478
        // Enable and persist the account
479
        try {
480
            $doctrineDomain->setActive(false);
481
            $this->entityManager->persist($doctrineDomain);
482
            $this->entityManager->flush();
483
            $this->entityManager->refresh($doctrineDomain);
484
        } catch (\Exception $e) {
485
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
486
        }
487
488
        return $this->loadFromDoctrineDomain($doctrineDomain);
489
    }
490
491
    /**
492
     * Enable a domain wildcard
493
     *
494
     * @param string $name Domain name
495
     * @return DomainInterface Domain
496
     * @throws \RuntimeException If the domain cannot be enabled
497
     */
498
    public function enableDomainWildcard($name)
499
    {
500
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
501
502
        // If the domain is unknown
503
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
504
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
505
        }
506
507
        // Enable and persist the account
508
        try {
509
            $doctrineDomain->setWildcard(true);
510
            $this->entityManager->persist($doctrineDomain);
511
            $this->entityManager->flush();
512
            $this->entityManager->refresh($doctrineDomain);
513
        } catch (\Exception $e) {
514
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
515
        }
516
517
        return $this->loadFromDoctrineDomain($doctrineDomain);
518
    }
519
520
    /**
521
     * Disable a domain wildcard
522
     *
523
     * @param string $name Domain name
524
     * @return DomainInterface Domain
525
     * @throws \RuntimeException If the domain cannot be disabled
526
     */
527
    public function disableDomainWildcard($name)
528
    {
529
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
530
531
        // If the domain is unknown
532
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
533
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
534
        }
535
536
        // Enable and persist the account
537
        try {
538
            $doctrineDomain->setWildcard(false);
539
            $this->entityManager->persist($doctrineDomain);
540
            $this->entityManager->flush();
541
            $this->entityManager->refresh($doctrineDomain);
542
        } catch (\Exception $e) {
543
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
544
        }
545
546
        return $this->loadFromDoctrineDomain($doctrineDomain);
547
    }
548
549
    /**
550
     * Create domain domain from doctrine domain
551
     *
552
     * @param DoctrineDomain $doctrineDomain Doctrine domain
553
     * @return DomainInterface Domain domain
554
     */
555
    protected function loadFromDoctrineDomain(DoctrineDomain $doctrineDomain)
556
    {
557
        return DomainFactory::parseString($doctrineDomain->getName());
558
    }
559
560
    /**
561
     * Load a virtual host
562
     *
563
     * @param AccountInterface $account Account
564
     * @param string $docroot Document root
565
     * @return VhostInterface Virtual host
566
     */
567
    public function loadVhost(AccountInterface $account, $docroot = '')
568
    {
569
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
570
571
        // If the account is unknown
572
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
573
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
574
        }
575
576
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
577
578
        // Create and return a domain virtual host
579
        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...
580
    }
581
582
    /**
583
     * Create a virtual host
584
     *
585
     * @param AccountInterface $account Account name
586
     * @param DomainInterface $domain Domain
587
     * @param string $docroot Document root
588
     * @param string $type Virtual host Type
589
     * @return VhostInterface Virtual host
590
     * @throws \RuntimeException If the account is unknown
591
     * @throws \RuntimeException If the domain is unknown
592
     */
593
    public function createVhost(AccountInterface $account, DomainInterface $domain, $docroot, $type)
594
    {
595
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
596
597
        // If the account is unknown
598
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
599
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
600
        }
601
602
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
603
604
        // If the domain is unknown
605
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
606
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
607
        }
608
609
        // Create the new domain
610
        $doctrineVhost = new DoctrineVhost();
611
        $doctrineVhost->setAccount($doctrineAccount);
612
        $doctrineVhost->setActive(false);
613
        $doctrineVhost->setType($type);
614
        $doctrineVhost->setDocroot($docroot);
615
616
        // Persist the new virtual host
617
        try {
618
            $this->entityManager->persist($doctrineVhost);
619
            $this->entityManager->flush();
620
            $this->entityManager->refresh($doctrineVhost);
621
        } catch (UniqueConstraintViolationException $e) {
622
            $doctrineVhost = $this->vhostRepository->findOneBy(['docroot' => $docroot]);
623
        } catch (\Exception $e) {
624
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
625
        }
626
627
        // Set the primary domain
628
        $doctrineDomain->setVhost($doctrineVhost);
0 ignored issues
show
Bug introduced by
It seems like $doctrineVhost defined by $this->vhostRepository->...'docroot' => $docroot)) on line 622 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...
629
        $doctrineDomain->setPrimarydomain(true);
630
631
        // Persist the domain
632
        try {
633
            $this->entityManager->persist($doctrineDomain);
634
            $this->entityManager->flush();
635
        } catch (\Exception $e) {
636
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
637
        }
638
639
        // Create and return a domain virtual host
640
        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...
641
    }
642
643
    /**
644
     * Delete a virtual host
645
     *
646
     * @param AccountInterface $account Account name
647
     * @param string $docroot Document root
648
     * @return VhostInterface Virtual host
649
     * @throws \RuntimeException If the account is unknown
650
     * @throws \RuntimeException If the virtual host is unknown
651
     */
652
    public function deleteVhost(AccountInterface $account, $docroot)
653
    {
654
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
655
656
        // If the account is unknown
657
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
658
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
659
        }
660
661
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
662
663
        // If the virtual host is unknown
664
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
665
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
666
        }
667
668
        // Remove and persist the virtual host
669
        try {
670
            $this->entityManager->remove($doctrineVhost);
671
            $this->entityManager->flush();
672
        } catch (\Exception $e) {
673
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
674
        }
675
676
        // Create and return a domain virtual host
677
        return $this->loadFromDoctrineVhost($doctrineVhost);
678
    }
679
680
    /**
681
     * Enable a virtual host
682
     *
683
     * @param AccountInterface $account Account name
684
     * @param string $docroot Document root
685
     * @return VhostInterface Virtual host
686
     * @throws \RuntimeException If the account is unknown
687
     * @throws \RuntimeException If the virtual host is unknown
688
     */
689
    public function enableVhost(AccountInterface $account, $docroot)
690
    {
691
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
692
693
        // If the account is unknown
694
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
695
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
696
        }
697
698
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
699
700
        // If the virtual host is unknown
701
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
702
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
703
        }
704
705
        // Update and persist the virtual host
706
        try {
707
            $doctrineVhost->setActive(true);
708
            $this->entityManager->persist($doctrineVhost);
709
            $this->entityManager->flush();
710
            $this->entityManager->refresh($doctrineVhost);
711
        } catch (\Exception $e) {
712
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
713
        }
714
715
        // Create and return a domain virtual host
716
        return $this->loadFromDoctrineVhost($doctrineVhost);
717
    }
718
719
    /**
720
     * Disable a virtual host
721
     *
722
     * @param AccountInterface $account Account name
723
     * @param string $docroot Document root
724
     * @return VhostInterface Virtual host
725
     * @throws \RuntimeException If the account is unknown
726
     * @throws \RuntimeException If the virtual host is unknown
727
     */
728
    public function disableVhost(AccountInterface $account, $docroot)
729
    {
730
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
731
732
        // If the account is unknown
733
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
734
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
735
        }
736
737
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
738
739
        // If the virtual host is unknown
740
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
741
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
742
        }
743
744
        // Update and persist the virtual host
745
        try {
746
            $doctrineVhost->setActive(false);
747
            $this->entityManager->persist($doctrineVhost);
748
            $this->entityManager->flush();
749
            $this->entityManager->refresh($doctrineVhost);
750
        } catch (\Exception $e) {
751
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
752
        }
753
754
        // Create and return a domain virtual host
755
        return $this->loadFromDoctrineVhost($doctrineVhost);
756
    }
757
758
    /**
759
     * Redirect a virtual host
760
     *
761
     * @param AccountInterface $account Account name
762
     * @param string $docroot Document root
763
     * @param string|null $url Redirect URL
764
     * @param int $status Redirect HTTP status
765
     * @return VhostInterface Virtual host
766
     * @throws \RuntimeException If the account is unknown
767
     * @throws \RuntimeException If the virtual host is unknown
768
     */
769
    public function redirectVhost(AccountInterface $account, $docroot, $url, $status)
770
    {
771
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
772
773
        // If the account is unknown
774
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
775
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
776
        }
777
778
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
779
780
        // If the virtual host is unknown
781
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
782
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
783
        }
784
785
        // Update and persist the virtual host
786
        try {
787
            $doctrineVhost->setRedirecturl($url);
788
            $doctrineVhost->setRedirectstatus($status);
789
            $this->entityManager->persist($doctrineVhost);
790
            $this->entityManager->flush();
791
            $this->entityManager->refresh($doctrineVhost);
792
        } catch (\Exception $e) {
793
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
794
        }
795
796
        // Create and return a domain virtual host
797
        return $this->loadFromDoctrineVhost($doctrineVhost);
798
    }
799
800
    /**
801
     * Configure the PHP version of a virtual host
802
     *
803
     * @param AccountInterface $account Account name
804
     * @param string $docroot Document root
805
     * @param string|null $php PHP version
806
     * @return VhostInterface Virtual host
807
     * @throws \RuntimeException If the account is unknown
808
     * @throws \RuntimeException If the virtual host is unknown
809
     */
810
    public function phpVhost(AccountInterface $account, $docroot, $php)
811
    {
812
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
813
814
        // If the account is unknown
815
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
816
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
817
        }
818
819
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
820
821
        // If the virtual host is unknown
822
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
823
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
824
        }
825
826
        // Update and persist the virtual host
827
        try {
828
            $doctrineVhost->setPhp($php);
829
            $this->entityManager->persist($doctrineVhost);
830
            $this->entityManager->flush();
831
            $this->entityManager->refresh($doctrineVhost);
832
        } catch (\Exception $e) {
833
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
834
        }
835
836
        // Create and return a domain virtual host
837
        return $this->loadFromDoctrineVhost($doctrineVhost);
838
    }
839
840
    /**
841
     * Add a protocol port to a virtual host
842
     *
843
     * @param string $account Account name
844
     * @param string $docroot Document root
845
     * @param int $protocol Protocol
846
     * @param int $port Port
847
     * @return VhostInterface Virtual host
848
     * @throws \RuntimeException If the account is unknown
849
     * @throws \RuntimeException If the virtual host is unknown
850
     */
851
    public function addVhostPort(AccountInterface $account, $docroot, $protocol, $port)
852
    {
853
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
854
855
        // If the account is unknown
856
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
857
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
858
        }
859
860
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
861
862
        // If the virtual host is unknown
863
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
864
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
865
        }
866
867
        /**
868
         * @var int $doctrineVhostPortIndex
869
         * @var DoctrinePort $doctrineVhostPort
870
         */
871
        foreach ($doctrineVhost->getPorts() as $doctrineVhostPortIndex => $doctrineVhostPort) {
872
            if (($doctrineVhostPort->getProtocol() == $protocol) && ($doctrineVhostPort->getPort() == $port)) {
873
                return $this->loadFromDoctrineVhost($doctrineVhost);
874
            }
875
        }
876
877
        // Update and persist the virtual host
878
        try {
879
            // Create the new protocol / port
880
            $doctrinePort = new DoctrinePort();
881
            $doctrinePort->setVhost($doctrineVhost);
882
            $doctrinePort->setProtocol($protocol);
883
            $doctrinePort->setPort($port);
884
            $this->entityManager->persist($doctrinePort);
885
            $this->entityManager->flush();
886
            $this->entityManager->refresh($doctrineVhost);
887
888
        } catch (\Exception $e) {
889
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
890
        }
891
892
        // Create and return a domain virtual host
893
        return $this->loadFromDoctrineVhost($doctrineVhost);
894
    }
895
896
    /**
897
     * Remove a protocol port from a virtual host
898
     *
899
     * @param string $account Account name
900
     * @param string $docroot Document root
901
     * @param int $protocol Protocol
902
     * @param int $port Port
903
     * @return VhostInterface Virtual host
904
     * @throws \RuntimeException If the account is unknown
905
     * @throws \RuntimeException If the virtual host is unknown
906
     */
907
    public function removeVhostPort(AccountInterface $account, $docroot, $protocol, $port)
908
    {
909
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
910
911
        // If the account is unknown
912
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
913
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
914
        }
915
916
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
917
918
        // If the virtual host is unknown
919
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
920
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
921
        }
922
923
        // Update and persist the virtual host
924
        try {
925
            /**
926
             * @var int $doctrineVhostPortIndex
927
             * @var DoctrinePort $doctrineVhostPort
928
             */
929
            foreach ($doctrineVhost->getPorts() as $doctrineVhostPortIndex => $doctrineVhostPort) {
930
                if (($doctrineVhostPort->getProtocol() == $protocol) && ($doctrineVhostPort->getPort() == $port)) {
931
                    $this->entityManager->remove($doctrineVhostPort);
932
                    $this->entityManager->flush();
933
                    $this->entityManager->refresh($doctrineVhost);
934
                    break;
935
                }
936
            }
937
        } catch (\Exception $e) {
938
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
939
        }
940
941
        // Create and return a domain virtual host
942
        return $this->loadFromDoctrineVhost($doctrineVhost);
943
    }
944
945
    /**
946
     * Add a secondary domain to a virtual host
947
     *
948
     * @param string $account Account name
949
     * @param string $docroot Document root
950
     * @param DomainInterface $domain Domain
951
     * @return VhostInterface Virtual host
952
     * @throws \RuntimeException If the account is unknown
953
     * @throws \RuntimeException If the virtual host is unknown
954
     * @throws \RuntimeException If the domain is unknown
955
     */
956
    public function addVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
957
    {
958
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
959
960
        // If the account is unknown
961
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
962
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
963
        }
964
965
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
966
967
        // If the virtual host is unknown
968
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
969
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
970
        }
971
972
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
973
974
        // If the domain is unknown
975
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
976
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
977
        }
978
979
        // If the domain is already assigned to a virtual host
980
        $doctrineDomainVhost = $doctrineDomain->getVhost();
981
        if (($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
982
            && ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
983
        ) {
984
            throw new \RuntimeException(
985
                sprintf('Domain "%s" is already assigned to a virtual host', strval($domain)),
986
                1475917298
987
            );
988
        }
989
990
        // Update and persist the virtual host
991
        try {
992
            $doctrineDomain->setVhost($doctrineVhost);
993
            $this->entityManager->persist($doctrineDomain);
994
            $this->entityManager->flush();
995
            $this->entityManager->refresh($doctrineVhost);
996
        } catch (\Exception $e) {
997
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
998
        }
999
1000
        // Create and return a domain virtual host
1001
        return $this->loadFromDoctrineVhost($doctrineVhost);
1002
    }
1003
1004
    /**
1005
     * Remove a secondary domain from a virtual host
1006
     *
1007
     * @param string $account Account name
1008
     * @param string $docroot Document root
1009
     * @param DomainInterface $domain Domain
1010
     * @return VhostInterface Virtual host
1011
     * @throws \RuntimeException If the account is unknown
1012
     * @throws \RuntimeException If the virtual host is unknown
1013
     * @throws \RuntimeException If the domain is unknown
1014
     */
1015
    public function removeVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
1016
    {
1017
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
1018
1019
        // If the account is unknown
1020
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
1021
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
1022
        }
1023
1024
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
1025
1026
        // If the virtual host is unknown
1027
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
1028
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
1029
        }
1030
1031
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
1032
1033
        // If the domain is unknown
1034
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
1035
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
1036
        }
1037
1038
        // If the domain is not assigned to the current virtual host
1039
        $doctrineDomainVhost = $doctrineDomain->getVhost();
1040
        if (!($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
1041
            || ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
1042
        ) {
1043
            throw new \RuntimeException(
1044
                sprintf('Domain "%s" is not assigned to this virtual host', strval($domain)),
1045
                1475942759
1046
            );
1047
        }
1048
1049
        // Update and persist the virtual host
1050
        try {
1051
            $doctrineDomain->setVhost(null);
1052
            $this->entityManager->persist($doctrineDomain);
1053
            $this->entityManager->flush();
1054
        } catch (\Exception $e) {
1055
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
1056
        }
1057
1058
        // Create and return a domain virtual host
1059
        return $this->loadFromDoctrineVhost($doctrineVhost);
1060
    }
1061
1062
    /**
1063
     * Create virtual host from a doctrine virtual host
1064
     *
1065
     * @param DoctrineVhost $doctrineVhost Doctrine virtual host
1066
     * @return VhostInterface Virtual host
1067
     */
1068
    protected function loadFromDoctrineVhost(DoctrineVhost $doctrineVhost)
1069
    {
1070
        $doctrinePrimaryDomain = $doctrineVhost->getPrimarydomain();
1071
        $primaryDomain = $this->loadFromDoctrineDomain($doctrinePrimaryDomain);
1072
1073
        $vhost = new Vhost($primaryDomain, $doctrineVhost->getDocroot(), $doctrineVhost->getType());
1074
        $vhost->setActive($doctrineVhost->isActive());
1075
        $vhost->setPhp($doctrineVhost->getPhp());
1076
        $vhost->setRedirectUrl($doctrineVhost->getRedirecturl());
1077
        $vhost->setRedirectStatus($doctrineVhost->getRedirectstatus());
1078
1079
        // Enable protocols / ports
1080
        /** @var DoctrinePort $port */
1081
        foreach ($doctrineVhost->getPorts() as $port) {
1082
            $vhost->enablePort($port->getProtocol(), $port->getPort());
1083
        }
1084
1085
        // Add the wilcard version of the primary domain
1086
        if ($doctrinePrimaryDomain->isWildcard()) {
1087
            $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$primaryDomain));
1088
        }
1089
1090
        // Add all secondary domains
1091
        /** @var DoctrineDomain $doctrineDomain */
1092
        foreach ($doctrineVhost->getDomains() as $doctrineDomain) {
1093
            if ($doctrineDomain->getName() != $doctrinePrimaryDomain->getName()) {
1094
                $vhost->addSecondaryDomain(DomainFactory::parseString($doctrineDomain->getName()));
1095
1096
                if ($doctrineDomain->isWildcard()) {
1097
                    $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$doctrineDomain->getName()));
1098
                }
1099
            }
1100
        }
1101
1102
        return $vhost;
1103
    }
1104
}
1105