Completed
Pull Request — develop (#605)
by
unknown
14:48
created

ServiceManager::getProxyModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * ParamConverter class for entry point to ProxyApi Bundle
4
 */
5
6
namespace Graviton\ProxyApiBundle\Manager;
7
8
use Graviton\ExceptionBundle\Exception\NotFoundException;
9
use Graviton\ProxyApiBundle\Helper\HttpHelper;
10
use Graviton\ProxyApiBundle\Listener\ProxyExceptionListener;
11
use Graviton\ProxyApiBundle\Model\ProxyModel;
12
use Graviton\ProxyApiBundle\Processor\PostProcessorInterface;
13
use Graviton\ProxyApiBundle\Processor\PreProcessor;
14
use Graviton\ProxyApiBundle\Processor\PreProcessorInterface;
15
use Graviton\ProxyApiBundle\Processor\ProxyProcessorInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Doctrine\Common\Cache\CacheProvider;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * Service Request Converter and startup for ProxyApi
23
 *
24
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
25
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
26
 * @link     http://swisscom.ch
27
 */
28
class ServiceManager
29
{
30
    /** TODO Cache name for services */
31
    const CACHE_KEY_SERVICES = 'proxy_services';
32
    const CACHE_KEY_SERVICES_TIME = 10;
33
    const CACHE_KEY_SERVICES_URLS = 'proxy_services_urls';
34
    const CACHE_KEY_SERVICES_URLS_TIME = 10;
35
    const CACHE_KEY_SERVICES_PREFIX = 'proxy_';
36
37
    /** @var Request */
38
    protected $request;
39
40
    /** @var ProxyManager */
41
    protected $proxyManager;
42
43
    /** @var CacheProvider */
44
    protected $cacheProvider;
45
46
    /** @var HttpHelper */
47
    protected $httpHelper;
48
49
    /**
50
     * Client request "source"
51
     *
52
     * @var string
53
     */
54
    protected $reqClient;
55
56
    /**
57
     * Client request service
58
     *
59
     * @var string
60
     */
61
    protected $reqService;
62
63
    /** @var PreProcessor */
64
    protected $preProcessor;
65
    protected $proxyProcessor;
66
    protected $postProcessor;
67
68
    /**
69
     * ServiceConverter constructor.
70
     * @param RequestStack  $requestStack  Sf Request information service
71
     * @param HttpHelper    $httpHelper    Request http builder
72
     * @param ProxyManager  $proxyManager  Db Manager and query control
73
     * @param CacheProvider $cacheProvider Cache service
74
     */
75
    public function __construct(
76
        RequestStack $requestStack,
77
        HttpHelper $httpHelper,
78
        ProxyManager $proxyManager,
79
        CacheProvider $cacheProvider
80
    ) {
81
        $this->request = $requestStack->getCurrentRequest();
82
        $this->httpHelper = $httpHelper;
83
        $this->proxyManager = $proxyManager;
84
        $this->cacheProvider = $cacheProvider;
85
    }
86
87
    /**
88
     * Get Request Proxy model
89
     *
90
     * @return ProxyModel
91
     */
92
    private function getProxyModel()
93
    {
94
        $this->reqClient  = $this->request->get('client');
95
        $this->reqService = $this->request->get('service');
96
97
        if (!$this->reqClient) {
98
            throw new ProxyExceptionListener(404, 'Proxy service not found');
99
        }
100
101
        /** @var ProxyModel $proxyModel */
102
        $proxyModel = $this->proxyManager->getService($this->reqClient);
103
104
        return $proxyModel;
105
    }
106
107
    /**
108
     * List all services
109
     * TODO correct link
110
     *
111
     * @return array
112
     */
113
    public function getServices()
114
    {
115
        $services = [];
116
        foreach ($this->proxyManager->getServices()['services'] as $key => $service) {
117
            $services[$key] = '/'.$key;
118
        }
119
120
        return $services;
121
    }
122
123
    /**
124
     * Execute proxy request
125
     *
126
     * @return Response
127
     */
128
    public function processRequest()
129
    {
130
        // Check if there is a service with that name
131
        $proxyModel = $this->getProxyModel();
132
133
        $this->httpHelper->setBaseUri($proxyModel->getUri());
134
135
        // Do processing steps
136
        $preProcessing = $proxyModel->getPreProcessorService();
137
        if (!$preProcessing instanceof PreProcessorInterface) {
138
            throw new ProxyExceptionListener(412, 'Configured PreProcessing configuration is incorrect');
139
        }
140
        $this->httpHelper = $preProcessing->process($this->request, $this->httpHelper, $proxyModel);
141
142
        $proxyProcesor = $proxyModel->getProxyProcessorService();
143
        if (!$proxyProcesor instanceof ProxyProcessorInterface) {
144
            throw new ProxyExceptionListener(412, 'Configured ProxyProcessor configuration is incorrect');
145
        }
146
147
        /** @var Response $response */
148
        $response = $proxyProcesor->process($this->request, $this->httpHelper, $proxyModel);
149
150
        $postProcesor = $proxyModel->getPostProcessorService();
151
        if (!$postProcesor instanceof PostProcessorInterface) {
152
            throw new ProxyExceptionListener(412, 'Configured ProxyProcessor configuration is incorrect');
153
        }
154
        $response = $postProcesor->process($response, $proxyModel);
155
156
        return $response;
157
    }
158
}
159