Completed
Push — master ( ee041c...22b621 )
by Joschi
04:52
created

DoctrineStorageAdapterStrategy::addVhostDomain()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 5.5555
cc 8
eloc 23
nc 8
nop 3
crap 72
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
276
        // Run through all virtual hosts of this account
277
        /** @var DoctrineVhost $doctrineVhost */
278
        foreach ($doctrineAccount->getVhosts() as $doctrineVhost) {
279
            $doctrinePrimaryDomain = $doctrineVhost->getPrimarydomain();
280
            if ($doctrinePrimaryDomain instanceof DoctrineDomain) {
281
                $primaryDomain = DomainFactory::parseString($doctrinePrimaryDomain->getName());
282
283
                $vhost = new Vhost($primaryDomain, $doctrineVhost->getDocroot(), $doctrineVhost->getType());
284
                $vhost->setPhp($doctrineVhost->getPhp());
285
                $vhost->setRedirectUrl($doctrineVhost->getRedirecturl());
286
                $vhost->setRedirectStatus($doctrineVhost->getRedirectstatus());
287
                if ($doctrineVhost->getHttpport() !== null) {
288
                    $vhost->enableProtocol(Vhost::PROTOCOL_HTTP, $doctrineVhost->getHttpport());
289
                }
290
                if ($doctrineVhost->getHttpsport() !== null) {
291
                    $vhost->enableProtocol(Vhost::PROTOCOL_HTTPS, $doctrineVhost->getHttpsport());
292
                }
293
294
                // Add the wilcard version of the primary domain
295
                if ($doctrinePrimaryDomain->isWildcard()) {
296
                    $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$primaryDomain));
297
                }
298
299
                // Add all secondary domains
300
                /** @var DoctrineDomain $doctrineDomain */
301
                foreach ($doctrineVhost->getDomains() as $doctrineDomain) {
302
                    if ($doctrineDomain->getName() != $doctrinePrimaryDomain->getName()) {
303
                        $vhost->addSecondaryDomain(DomainFactory::parseString($doctrineDomain->getName()));
304
305
                        if ($doctrineDomain->isWildcard()) {
306
                            $vhost->addSecondaryDomain(DomainFactory::parseString('*.'.$doctrineDomain->getName()));
307
                        }
308
                    }
309
                }
310
311
                $account->addVirtualHost($vhost);
312
            } else {
313
                trigger_error('Skipping virtual host due to missing primary domain', E_USER_NOTICE);
314
            }
315
        }
316
317
        return $account;
318
    }
319
320
321
    /**
322
     * Load a domain (optionally: unassigned)
323
     *
324
     * @param string $name Domain name
325
     * @param AccountInterface $account Optional: Account the domain must belong to
326
     * @param string $vhostDocroot Optional: Document root of the virtual host the domain must be assigned to (otherwise: unassigned)
327
     * @return DomainInterface Domain
328
     * @throws \RuntimeException If the domain is unknown
329
     * @throws \RuntimeException If the domain belongs to another account
330
     * @throws \RuntimeException If the domain is assigned to a virtual host but should be unassigned
331
     * @throws \RuntimeException If the domain is assigned to a different virtual host
332
     */
333
    public function loadDomain($name, AccountInterface $account = null, $vhostDocroot = null)
334
    {
335
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
336
337
        // If the domain is unknown
338
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
339
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475915909);
340
        }
341
342
        // If only an assigned or unassigned account domain should be returned
343
        if ($account instanceof Account) {
344
            // If the domain belongs to another account
345
            if ($doctrineDomain->getAccount()->getName() != $account->getName()) {
346
                throw new \RuntimeException(
347
                    sprintf(
348
                        'Domain "%s" belongs to another account ("%s")',
349
                        $name,
350
                        $doctrineDomain->getAccount()->getName()
351
                    ),
352
                    1475917184
353
                );
354
            }
355
356
            // Determine the virtual host the domain is assigned to
357
            $doctrineVhost = $doctrineDomain->getVhost();
358
359
            // If the domain is already assigned to another virtual host
360
            if ($doctrineVhost instanceof DoctrineVhost) {
361
                // If only an uassigned domain should be returned
362
                if ($vhostDocroot === null) {
363
                    throw new \RuntimeException(
364
                        sprintf('Domain "%s" is already assigned to a virtual host', $name),
365
                        1475917298
366
                    );
367
                }
368
369
                // If the document route doesn't match the requested one
370
                if ($doctrineVhost->getDocroot() !== $vhostDocroot) {
371
                    throw new \RuntimeException(
372
                        sprintf('Domain "%s" is assigned to another virtual host', $name),
373
                        1475942759
374
                    );
375
                }
376
            }
377
        }
378
379
        return $this->loadFromDoctrineDomain($doctrineDomain);
380
    }
381
382
    /**
383
     * Create a domain
384
     *
385
     * @param string $name Domain name
386
     * @param Account $account Account the domain belongs to
387
     * @return DomainInterface Domain
388
     * @throws \RuntimeException If the account is unknown
389
     */
390
    public function createDomain($name, AccountInterface $account)
391
    {
392
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
393
394
        // If the account is unknown
395
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
396
            throw new \RuntimeException(sprintf('Unknown account "%s"', $name), 1475495500);
397
        }
398
399
        // Create the new domain
400
        $doctrineDomain = new DoctrineDomain();
401
        $doctrineDomain->setAccount($doctrineAccount);
402
        $doctrineDomain->setActive(false);
403
        $doctrineDomain->setName($name);
404
        $doctrineDomain->setWildcard(false);
405
        $doctrineDomain->setPrimarydomain(false);
406
407
        // Persist the new domain
408
        try {
409
            $this->entityManager->persist($doctrineDomain);
410
            $this->entityManager->flush();
411
        } catch (UniqueConstraintViolationException $e) {
412
            $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
413
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
414
        } catch (\Exception $e) {
415
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
416
        }
417
418
        // Create and return a domain domain
419
        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...
420
    }
421
422
    /**
423
     * Delete a domain
424
     *
425
     * @param string $name Domain name
426
     * @return DomainInterface Domain
427
     * @throws \RuntimeException If the domain cannot be deleted
428
     */
429
    public function deleteDomain($name)
430
    {
431
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
432
433
        // If the domain is unknown
434
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
435
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
436
        }
437
438
        // If the domain is assigned to a virtual host
439
        if ($doctrineDomain->getVhost() instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
440
            throw new \RuntimeException(
441
                sprintf('Domain "%s" is assigned to a virtual host, please release the assignment first', $name),
442
                1475921740
443
            );
444
        }
445
446
        // Remove and persist the domain
447
        try {
448
            $this->entityManager->remove($doctrineDomain);
449
            $this->entityManager->flush();
450
        } catch (\Exception $e) {
451
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
452
        }
453
454
        // Create and return a domain domain stub
455
        return $this->loadFromDoctrineDomain($doctrineDomain);
456
    }
457
458
    /**
459
     * Enable a domain
460
     *
461
     * @param string $name Domain name
462
     * @return DomainInterface Domain
463
     * @throws \RuntimeException If the domain cannot be enabled
464
     */
465
    public function enableDomain($name)
466
    {
467
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
468
469
        // If the domain is unknown
470
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
471
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
472
        }
473
474
        // Enable and persist the account
475
        try {
476
            $doctrineDomain->setActive(true);
477
            $this->entityManager->persist($doctrineDomain);
478
            $this->entityManager->flush();
479
        } catch (\Exception $e) {
480
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
481
        }
482
483
        return $this->loadFromDoctrineDomain($doctrineDomain);
484
    }
485
486
    /**
487
     * Disable a domain
488
     *
489
     * @param string $name Domain name
490
     * @return DomainInterface Domain
491
     * @throws \RuntimeException If the domain cannot be disabled
492
     */
493
    public function disableDomain($name)
494
    {
495
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
496
497
        // If the domain is unknown
498
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
499
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
500
        }
501
502
        // Enable and persist the account
503
        try {
504
            $doctrineDomain->setActive(false);
505
            $this->entityManager->persist($doctrineDomain);
506
            $this->entityManager->flush();
507
        } catch (\Exception $e) {
508
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
509
        }
510
511
        return $this->loadFromDoctrineDomain($doctrineDomain);
512
    }
513
514
    /**
515
     * Enable a domain wildcard
516
     *
517
     * @param string $name Domain name
518
     * @return DomainInterface Domain
519
     * @throws \RuntimeException If the domain cannot be enabled
520
     */
521
    public function enableDomainWildcard($name)
522
    {
523
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
524
525
        // If the domain is unknown
526
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
527
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
528
        }
529
530
        // Enable and persist the account
531
        try {
532
            $doctrineDomain->setWildcard(true);
533
            $this->entityManager->persist($doctrineDomain);
534
            $this->entityManager->flush();
535
        } catch (\Exception $e) {
536
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
537
        }
538
539
        return $this->loadFromDoctrineDomain($doctrineDomain);
540
    }
541
542
    /**
543
     * Disable a domain wildcard
544
     *
545
     * @param string $name Domain name
546
     * @return DomainInterface Domain
547
     * @throws \RuntimeException If the domain cannot be disabled
548
     */
549
    public function disableDomainWildcard($name)
550
    {
551
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => $name]);
552
553
        // If the domain is unknown
554
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
555
            throw new \RuntimeException(sprintf('Unknown domain "%s"', $name), 1475921539);
556
        }
557
558
        // Enable and persist the account
559
        try {
560
            $doctrineDomain->setWildcard(false);
561
            $this->entityManager->persist($doctrineDomain);
562
            $this->entityManager->flush();
563
        } catch (\Exception $e) {
564
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
565
        }
566
567
        return $this->loadFromDoctrineDomain($doctrineDomain);
568
    }
569
570
    /**
571
     * Create domain domain from doctrine domain
572
     *
573
     * @param DoctrineDomain $doctrineDomain Doctrine domain
574
     * @return DomainInterface Domain domain
575
     */
576
    protected function loadFromDoctrineDomain(DoctrineDomain $doctrineDomain)
577
    {
578
        return DomainFactory::parseString($doctrineDomain->getName());
579
    }
580
581
    /**
582
     * Create a virtual host
583
     *
584
     * @param AccountInterface $account Account name
585
     * @param DomainInterface $domain Domain
586
     * @param string $docroot Document root
587
     * @param string $type Virtual host Type
588
     * @return VhostInterface Virtual host
589
     * @throws \RuntimeException If the account is unknown
590
     * @throws \RuntimeException If the domain is unknown
591
     */
592
    public function createVhost(AccountInterface $account, DomainInterface $domain, $docroot, $type)
593
    {
594
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
595
596
        // If the account is unknown
597
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
598
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
599
        }
600
601
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
602
603
        // If the domain is unknown
604
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
605
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
606
        }
607
608
        // Create the new domain
609
        $doctrineVhost = new DoctrineVhost();
610
        $doctrineVhost->setAccount($doctrineAccount);
611
        $doctrineVhost->setActive(false);
612
        $doctrineVhost->setType($type);
613
        $doctrineVhost->setDocroot($docroot);
614
615
        // Persist the new virtual host
616
        try {
617
            $this->entityManager->persist($doctrineVhost);
618
            $this->entityManager->flush();
619
        } catch (UniqueConstraintViolationException $e) {
620
            $doctrineVhost = $this->vhostRepository->findOneBy(['docroot' => $docroot]);
621
        } catch (\Exception $e) {
622
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
623
        }
624
625
        // Set the primary domain
626
        $doctrineDomain->setVhost($doctrineVhost);
0 ignored issues
show
Bug introduced by
It seems like $doctrineVhost defined by $this->vhostRepository->...'docroot' => $docroot)) on line 620 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...
627
        $doctrineDomain->setPrimarydomain(true);
628
629
        // Persist the domain
630
        try {
631
            $this->entityManager->persist($doctrineDomain);
632
            $this->entityManager->flush();
633
        } catch (\Exception $e) {
634
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
635
        }
636
637
        // Create and return a domain domain
638
        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...
639
    }
640
641
    /**
642
     * Delete a virtual host
643
     *
644
     * @param AccountInterface $account Account name
645
     * @param string $docroot Document root
646
     * @return VhostInterface Virtual host
647
     * @throws \RuntimeException If the account is unknown
648
     * @throws \RuntimeException If the virtual host is unknown
649
     */
650
    public function deleteVhost(AccountInterface $account, $docroot)
651
    {
652
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
653
654
        // If the account is unknown
655
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
656
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
657
        }
658
659
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
660
661
        // If the virtual host is unknown
662
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
663
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
664
        }
665
666
        // Remove and persist the virtual host
667
        try {
668
            $this->entityManager->remove($doctrineVhost);
669
            $this->entityManager->flush();
670
        } catch (\Exception $e) {
671
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
672
        }
673
674
        // Create and return a domain domain
675
        return $this->loadFromDoctrineVhost($doctrineVhost);
676
    }
677
678
    /**
679
     * Enable a virtual host
680
     *
681
     * @param AccountInterface $account Account name
682
     * @param string $docroot Document root
683
     * @return VhostInterface Virtual host
684
     * @throws \RuntimeException If the account is unknown
685
     * @throws \RuntimeException If the virtual host is unknown
686
     */
687
    public function enableVhost(AccountInterface $account, $docroot)
688
    {
689
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
690
691
        // If the account is unknown
692
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
693
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
694
        }
695
696
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
697
698
        // If the virtual host is unknown
699
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
700
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
701
        }
702
703
        // Update and persist the virtual host
704
        try {
705
            $doctrineVhost->setActive(true);
706
            $this->entityManager->persist($doctrineVhost);
707
            $this->entityManager->flush();
708
        } catch (\Exception $e) {
709
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
710
        }
711
712
        // Create and return a domain domain
713
        return $this->loadFromDoctrineVhost($doctrineVhost);
714
    }
715
716
    /**
717
     * Disable a virtual host
718
     *
719
     * @param AccountInterface $account Account name
720
     * @param string $docroot Document root
721
     * @return VhostInterface Virtual host
722
     * @throws \RuntimeException If the account is unknown
723
     * @throws \RuntimeException If the virtual host is unknown
724
     */
725
    public function disableVhost(AccountInterface $account, $docroot)
726
    {
727
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
728
729
        // If the account is unknown
730
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
731
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
732
        }
733
734
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
735
736
        // If the virtual host is unknown
737
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
738
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
739
        }
740
741
        // Update and persist the virtual host
742
        try {
743
            $doctrineVhost->setActive(false);
744
            $this->entityManager->persist($doctrineVhost);
745
            $this->entityManager->flush();
746
        } catch (\Exception $e) {
747
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
748
        }
749
750
        // Create and return a domain domain
751
        return $this->loadFromDoctrineVhost($doctrineVhost);
752
    }
753
754
    /**
755
     * Redirect a virtual host
756
     *
757
     * @param AccountInterface $account Account name
758
     * @param string $docroot Document root
759
     * @param string|null $url Redirect URL
760
     * @param int $status Redirect HTTP status
761
     * @return VhostInterface Virtual host
762
     * @throws \RuntimeException If the account is unknown
763
     * @throws \RuntimeException If the virtual host is unknown
764
     */
765
    public function redirectVhost(AccountInterface $account, $docroot, $url, $status)
766
    {
767
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
768
769
        // If the account is unknown
770
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
771
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
772
        }
773
774
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
775
776
        // If the virtual host is unknown
777
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
778
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
779
        }
780
781
        // Update and persist the virtual host
782
        try {
783
            $doctrineVhost->setRedirecturl($url);
784
            $doctrineVhost->setRedirectstatus($status);
785
            $this->entityManager->persist($doctrineVhost);
786
            $this->entityManager->flush();
787
        } catch (\Exception $e) {
788
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
789
        }
790
791
        // Create and return a domain domain
792
        return $this->loadFromDoctrineVhost($doctrineVhost);
793
    }
794
795
    /**
796
     * Configure the PHP version of a virtual host
797
     *
798
     * @param AccountInterface $account Account name
799
     * @param string $docroot Document root
800
     * @param string|null $php PHP version
801
     * @return VhostInterface Virtual host
802
     * @throws \RuntimeException If the account is unknown
803
     * @throws \RuntimeException If the virtual host is unknown
804
     */
805
    public function phpVhost(AccountInterface $account, $docroot, $php)
806
    {
807
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
808
809
        // If the account is unknown
810
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
811
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
812
        }
813
814
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
815
816
        // If the virtual host is unknown
817
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
818
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
819
        }
820
821
        // Update and persist the virtual host
822
        try {
823
            $doctrineVhost->setPhp($php);
824
            $this->entityManager->persist($doctrineVhost);
825
            $this->entityManager->flush();
826
        } catch (\Exception $e) {
827
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
828
        }
829
830
        // Create and return a domain domain
831
        return $this->loadFromDoctrineVhost($doctrineVhost);
832
    }
833
834
    /**
835
     * Configure a protocol based port for a virtual host
836
     *
837
     * @param string $account Account name
838
     * @param string $docroot Document root
839
     * @param int $protocol Protocol
840
     * @param int $port Port
841
     * @return VhostInterface Virtual host
842
     * @throws \RuntimeException If the account is unknown
843
     * @throws \RuntimeException If the virtual host is unknown
844
     */
845
    public function portVhost(AccountInterface $account, $docroot, $protocol, $port)
846
    {
847
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
848
849
        // If the account is unknown
850
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
851
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
852
        }
853
854
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
855
856
        // If the virtual host is unknown
857
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
858
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
859
        }
860
861
        // Update and persist the virtual host
862
        try {
863
            $doctrineVhost->{'set'.ucfirst(Vhost::$supportedProtocols[$protocol]).'port'}($port);
864
            $this->entityManager->persist($doctrineVhost);
865
            $this->entityManager->flush();
866
        } catch (\Exception $e) {
867
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
868
        }
869
870
        // Create and return a domain domain
871
        return $this->loadFromDoctrineVhost($doctrineVhost);
872
    }
873
874
    /**
875
     * Add a secondary domain to a virtual host
876
     *
877
     * @param string $account Account name
878
     * @param string $docroot Document root
879
     * @param DomainInterface $domain Domain
880
     * @return VhostInterface Virtual host
881
     * @throws \RuntimeException If the account is unknown
882
     * @throws \RuntimeException If the virtual host is unknown
883
     * @throws \RuntimeException If the domain is unknown
884
     */
885
    public function addVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
886
    {
887
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
888
889
        // If the account is unknown
890
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
891
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
892
        }
893
894
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
895
896
        // If the virtual host is unknown
897
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
898
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
899
        }
900
901
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
902
903
        // If the domain is unknown
904
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
905
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
906
        }
907
908
        // If the domain is already assigned to a virtual host
909
        $doctrineDomainVhost = $doctrineDomain->getVhost();
910
        if (($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
911
            && ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
912
        ) {
913
            throw new \RuntimeException(
914
                sprintf('Domain "%s" is already assigned to a virtual host', strval($domain)),
915
                1475917298
916
            );
917
        }
918
919
        // Update and persist the virtual host
920
        try {
921
            $doctrineDomain->setVhost($doctrineVhost);
922
            $this->entityManager->persist($doctrineDomain);
923
            $this->entityManager->flush();
924
        } catch (\Exception $e) {
925
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
926
        }
927
928
        // Create and return a domain domain
929
        return $this->loadFromDoctrineVhost($doctrineVhost);
930
    }
931
932
    /**
933
     * Remove a secondary domain from a virtual host
934
     *
935
     * @param string $account Account name
936
     * @param string $docroot Document root
937
     * @param DomainInterface $domain Domain
938
     * @return VhostInterface Virtual host
939
     * @throws \RuntimeException If the account is unknown
940
     * @throws \RuntimeException If the virtual host is unknown
941
     * @throws \RuntimeException If the domain is unknown
942
     */
943
    public function removeVhostDomain(AccountInterface $account, $docroot, DomainInterface $domain)
944
    {
945
        $doctrineAccount = $this->accountRepository->findOneBy(['name' => $account->getName()]);
946
947
        // If the account is unknown
948
        if (!$doctrineAccount instanceof \Tollwerk\Admin\Infrastructure\Model\Account) {
949
            throw new \RuntimeException(sprintf('Unknown account "%s"', $account->getName()), 1475495500);
950
        }
951
952
        $doctrineVhost = $this->vhostRepository->findOneBy(['account' => $doctrineAccount, 'docroot' => $docroot]);
953
954
        // If the virtual host is unknown
955
        if (!$doctrineVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost) {
956
            throw new \RuntimeException(sprintf('Unknown virtual host "%s"', $docroot), 1475933194);
957
        }
958
959
        $doctrineDomain = $this->domainRepository->findOneBy(['name' => strval($domain)]);
960
961
        // If the domain is unknown
962
        if (!$doctrineDomain instanceof \Tollwerk\Admin\Infrastructure\Model\Domain) {
963
            throw new \RuntimeException(sprintf('Unknown domain "%s"', strval($domain)), 1475921539);
964
        }
965
966
        // If the domain is not assigned to the current virtual host
967
        $doctrineDomainVhost = $doctrineDomain->getVhost();
968
        if (!($doctrineDomainVhost instanceof \Tollwerk\Admin\Infrastructure\Model\Vhost)
969
            || ($doctrineDomainVhost->getId() != $doctrineVhost->getId())
970
        ) {
971
            throw new \RuntimeException(
972
                sprintf('Domain "%s" is not assigned to this virtual host', strval($domain)),
973
                1475942759
974
            );
975
        }
976
977
        // Update and persist the virtual host
978
        try {
979
            $doctrineDomain->setVhost(null);
980
            $this->entityManager->persist($doctrineDomain);
981
            $this->entityManager->flush();
982
        } catch (\Exception $e) {
983
            throw new \RuntimeException($e->getMessage(), $e->getCode() || 1475925451);
984
        }
985
986
        // Create and return a domain domain
987
        return $this->loadFromDoctrineVhost($doctrineVhost);
988
    }
989
990
    /**
991
     * Create virtual host from a doctrine virtual host
992
     *
993
     * @param DoctrineVhost $doctrineVhost Doctrine virtual host
994
     * @return VhostInterface Virtual host
995
     */
996
    protected function loadFromDoctrineVhost(DoctrineVhost $doctrineVhost)
997
    {
998
        $vhost = new Vhost(
999
            $this->loadFromDoctrineDomain($doctrineVhost->getPrimarydomain()),
1000
            $doctrineVhost->getDocroot(),
1001
            $doctrineVhost->getType()
1002
        );
1003
1004
        return $vhost;
1005
    }
1006
}
1007