SymfonyClient   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 6
Bugs 1 Features 3
Metric Value
wmc 10
c 6
b 1
f 3
lcom 1
cbo 10
dl 0
loc 172
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A buildClient() 0 14 1
B getResponseHTTPCode() 0 24 3
B authenticate() 0 31 3
A expireAuthentication() 0 13 1
A destroyClient() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Visithor package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 */
13
14
namespace Visithor\Bundle\Client;
15
16
use Exception;
17
use Symfony\Bundle\FrameworkBundle\Client as FrameworkClient;
18
use Symfony\Component\BrowserKit\Cookie;
19
use Symfony\Component\HttpFoundation\Session\SessionInterface;
20
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21
use Symfony\Component\HttpKernel\KernelInterface;
22
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
23
24
use Visithor\Bundle\Environment\Interfaces\EnvironmentBuilderInterface;
25
use Visithor\Client\Interfaces\ClientInterface;
26
use Visithor\Model\Url;
27
28
/**
29
 * Class SymfonyClient
30
 */
31
class SymfonyClient implements ClientInterface
32
{
33
    /**
34
     * @var FrameworkClient
35
     *
36
     * Client
37
     */
38
    protected $client;
39
40
    /**
41
     * @var KernelInterface
42
     *
43
     * Kernel
44
     */
45
    protected $kernel;
46
47
    /**
48
     * @var SessionInterface
49
     *
50
     * Session
51
     */
52
    protected $session;
53
54
    /**
55
     * @var EnvironmentBuilderInterface
56
     *
57
     * Environment Builder
58
     */
59
    protected $environmentBuilder;
60
61
    /**
62
     * Construct
63
     *
64
     * @param EnvironmentBuilderInterface $environmentBuilder Environment Builder
65
     */
66
    public function __construct(
67
        SessionInterface $session,
68
        EnvironmentBuilderInterface $environmentBuilder = null,
69
        \AppKernel $kernel = null
70
    ) {
71
        $this->session = $session;
72
        $this->environmentBuilder = $environmentBuilder;
73
        $this->kernel = $kernel;
74
    }
75
76
    /**
77
     * Build client
78
     *
79
     * @return $this Self object
80
     */
81
    public function buildClient()
82
    {
83
        $this->kernel->boot();
84
        $this->session->clear();
85
86
        $this->client = $this
87
            ->kernel
88
            ->getContainer()
89
            ->get('test.client');
90
91
        $this
92
            ->environmentBuilder
93
            ->setUp($this->kernel);
94
    }
95
96
    /**
97
     * Get the HTTP Code Response given an URL instance
98
     *
99
     * @param Url $url Url
100
     *
101
     * @return int Response HTTP Code
102
     */
103
    public function getResponseHTTPCode(Url $url)
104
    {
105
        try {
106
            $this->authenticate($url);
107
            $verb = $url->getOption('verb', 'GET');
108
109
            $this
110
                ->client
111
                ->request($verb, $url->getPath());
112
113
            $result = $this
114
                ->client
115
                ->getResponse()
116
                ->getStatusCode();
117
        } catch (AccessDeniedHttpException $e) {
118
            $result = $e->getStatusCode();
119
        } catch (Exception $e) {
120
            $result = 'ERR';
121
        }
122
123
        $this->expireAuthentication($url);
124
125
        return $result;
126
    }
127
128
    /**
129
     * Authenticates a user if is needed.
130
     *
131
     * A user is needed to be authenticated if in the url a role and a firewall
132
     * is specified. Otherwise, the system will understand that is a public url
133
     *
134
     * @param Url $url Url
135
     *
136
     * @return $this Self object
137
     */
138
    protected function authenticate(Url $url)
139
    {
140
        if (
141
            !$url->getOption('role') ||
142
            !$url->getOption('firewall')
143
        ) {
144
            return $this;
145
        }
146
147
        $session = $this->session;
148
        $firewall = $url->getOption('firewall');
149
        $role = $url->getOption('role');
150
        $user = $this
151
            ->environmentBuilder
152
            ->getAuthenticationUser($url->getOption('role'));
153
154
        $token = new UsernamePasswordToken($user, null, $firewall, [$role]);
155
        $session->set('_security_' . $firewall, serialize($token));
156
        $session->save();
157
158
        $cookie = new Cookie(
159
            $session->getName(),
160
            $session->getId()
161
        );
162
        $this
163
            ->client
164
            ->getCookieJar()
165
            ->set($cookie);
166
167
        return $this;
168
    }
169
170
    /**
171
     * Expires the authentication if these has been created
172
     *
173
     * @param Url $url Url
174
     *
175
     * @return $this Self object
176
     */
177
    protected function expireAuthentication(Url $url)
178
    {
179
        $session = $this->session;
180
        $session->remove('_security_' . $url->getOption('firewall'));
181
        $session->save();
182
183
        $this
184
            ->client
185
            ->getCookieJar()
186
            ->expire($session->getName());
187
188
        return $this;
189
    }
190
191
    /**
192
     * Destroy client
193
     *
194
     * @return $this Self object
195
     */
196
    public function destroyClient()
197
    {
198
        $this
199
            ->environmentBuilder
200
            ->tearDown($this->kernel);
201
    }
202
}
203