Completed
Push — master ( 38192e...3b3b09 )
by Joschi
04:50
created

DoctrineStorageAdapterStrategy::loadVhost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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