Completed
Push — master ( 3ef484...ea5778 )
by Paweł
08:46
created

findTenantByCodeOrThrowException()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Rule;
18
19
use SWP\Bundle\ContentBundle\Model\RouteInterface;
20
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
21
use SWP\Bundle\CoreBundle\Factory\PublishDestinationFactoryInterface;
22
use SWP\Bundle\CoreBundle\Model\TenantInterface;
23
use SWP\Component\Common\Exception\UnexpectedTypeException;
24
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
25
26
final class PublishDestinationResolver implements PublishDestinationResolverInterface
27
{
28
    /**
29
     * @var TenantRepositoryInterface
30
     */
31
    private $tenantRepository;
32
33
    /**
34
     * @var RouteRepositoryInterface
35
     */
36
    private $routeRepository;
37
38
    /**
39
     * @var PublishDestinationFactoryInterface
40
     */
41
    private $publishDestinationFactory;
42
43
    /**
44
     * PublishDestinationResolver constructor.
45
     *
46
     * @param TenantRepositoryInterface          $tenantRepository
47
     * @param RouteRepositoryInterface           $routeRepository
48
     * @param PublishDestinationFactoryInterface $publishDestinationFactory
49
     */
50
    public function __construct(
51
        TenantRepositoryInterface $tenantRepository,
52
        RouteRepositoryInterface $routeRepository,
53
        PublishDestinationFactoryInterface $publishDestinationFactory
54
    ) {
55
        $this->tenantRepository = $tenantRepository;
56
        $this->routeRepository = $routeRepository;
57
        $this->publishDestinationFactory = $publishDestinationFactory;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function resolve(string $tenant, int $route)
64
    {
65
        $tenant = $this->findTenantByCodeOrThrowException($tenant);
66
        $route = $this->findRouteByIdOrThrowException($route);
67
68
        return $this->publishDestinationFactory->createWithTenantAndRoute($tenant, $route);
69
    }
70
71 View Code Duplication
    private function findTenantByCodeOrThrowException(string $code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if (!($tenant = $this->tenantRepository->findOneByCode($code)) instanceof TenantInterface) {
74
            throw UnexpectedTypeException::unexpectedType(
75
                is_object($tenant) ? get_class($tenant) : gettype($tenant),
76
                TenantInterface::class);
77
        }
78
79
        return $tenant;
80
    }
81
82 View Code Duplication
    private function findRouteByIdOrThrowException(int $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (!($route = $this->routeRepository->findOneBy(['id' => $id])) instanceof RouteInterface) {
85
            throw UnexpectedTypeException::unexpectedType(
86
                is_object($route) ? get_class($route) : gettype($route),
87
                RouteInterface::class);
88
        }
89
90
        return $route;
91
    }
92
}
93